1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_BASE32_BASE32_H_
6 #define COMPONENTS_BASE32_BASE32_H_
7 
8 #include <string>
9 
10 #include "base/strings/string_piece.h"
11 
12 namespace base32 {
13 
14 constexpr char kEncoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
15 constexpr char kPaddingChar = '=';
16 
17 enum class Base32EncodePolicy {
18   // Include the trailing padding in the output, when necessary.
19   INCLUDE_PADDING,
20   // Omit trailing padding in the output. Such an output will not be decodable
21   // unless |input.size()| is known by the decoder. Its size is guaranteed to be
22   // |ceil(input.size() * 8.0 / 5.0)|.
23   OMIT_PADDING
24 };
25 
26 // Encodes the |input| string in base32, defined in RFC 4648:
27 // https://tools.ietf.org/html/rfc4648#section-5
28 //
29 // The |policy| defines whether padding should be included or omitted from the
30 // encoded output.
31 std::string Base32Encode(
32     base::StringPiece input,
33     Base32EncodePolicy policy = Base32EncodePolicy::INCLUDE_PADDING);
34 
35 // Decodes the |input| string piece from base32. Returns an empty string on
36 // error, including if |input| is empty.
37 std::string Base32Decode(base::StringPiece input);
38 
39 }  // namespace base32
40 
41 #endif  // COMPONENTS_BASE32_BASE32_H_
42