Crypto++  8.2
Free C++ class library of cryptographic schemes
base32.h
Go to the documentation of this file.
1 // base32.h - written and placed in the public domain by Frank Palazzolo, based on hex.cpp by Wei Dai
2 // extended hex alphabet added by JW in November, 2017.
3 
4 /// \file base32.h
5 /// \brief Classes for Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
6 
7 #ifndef CRYPTOPP_BASE32_H
8 #define CRYPTOPP_BASE32_H
9 
10 #include "cryptlib.h"
11 #include "basecode.h"
12 
13 NAMESPACE_BEGIN(CryptoPP)
14 
15 /// \brief Base32 encodes data using DUDE encoding
16 /// \details Converts data to base32 using DUDE encoding. The default code is based on <A HREF="http://www.ietf.org/proceedings/51/I-D/draft-ietf-idn-dude-02.txt">Differential Unicode Domain Encoding (DUDE) (draft-ietf-idn-dude-02.txt)</A>.
17 /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
19 {
20 public:
21  /// \brief Construct a Base32Encoder
22  /// \param attachment a BufferedTrasformation to attach to this object
23  /// \param uppercase a flag indicating uppercase output
24  /// \param groupSize the size of the grouping
25  /// \param separator the separator to use between groups
26  /// \param terminator the terminator appeand after processing
27  /// \details Base32Encoder() constructs a default encoder. The constructor lacks fields for padding and
28  /// line breaks. You must use IsolatedInitialize() to change the default padding character or suppress it.
29  /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
30  Base32Encoder(BufferedTransformation *attachment = NULLPTR, bool uppercase = true, int groupSize = 0, const std::string &separator = ":", const std::string &terminator = "")
31  : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
32  {
33  IsolatedInitialize(MakeParameters(Name::Uppercase(), uppercase)(Name::GroupSize(), groupSize)(Name::Separator(), ConstByteArrayParameter(separator))(Name::Terminator(), ConstByteArrayParameter(terminator)));
34  }
35 
36  /// \brief Initialize or reinitialize this object, without signal propagation
37  /// \param parameters a set of NameValuePairs used to initialize this object
38  /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
39  /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
40  /// transformations. If initialization should be propagated, then use the Initialize() function.
41  /// \details The following code modifies the padding and line break parameters for an encoder:
42  /// <pre>
43  /// Base32Encoder encoder;
44  /// AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);
45  /// encoder.IsolatedInitialize(params);</pre>
46  /// \details You can change the encoding to <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base
47  /// 32 Encoding with Extended Hex Alphabet</A> by performing the following:
48  /// <pre>
49  /// Base32Encoder encoder;
50  /// const byte ALPHABET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
51  /// AlgorithmParameters params = MakeParameters(Name::EncodingLookupArray(),(const byte *)ALPHABET);
52  /// encoder.IsolatedInitialize(params);</pre>
53  /// \details If you change the encoding alphabet, then you will need to change the decoding alphabet \a and
54  /// the decoder's lookup table.
55  /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
56  void IsolatedInitialize(const NameValuePairs &parameters);
57 };
58 
59 /// \brief Base32 decodes data using DUDE encoding
60 /// \details Converts data from base32 using DUDE encoding. The default code is based on <A HREF="http://www.ietf.org/proceedings/51/I-D/draft-ietf-idn-dude-02.txt">Differential Unicode Domain Encoding (DUDE) (draft-ietf-idn-dude-02.txt)</A>.
61 /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
63 {
64 public:
65  /// \brief Construct a Base32Decoder
66  /// \param attachment a BufferedTrasformation to attach to this object
67  /// \sa IsolatedInitialize() for an example of modifying a Base32Decoder after construction.
68  Base32Decoder(BufferedTransformation *attachment = NULLPTR)
69  : BaseN_Decoder(GetDefaultDecodingLookupArray(), 5, attachment) {}
70 
71  /// \brief Initialize or reinitialize this object, without signal propagation
72  /// \param parameters a set of NameValuePairs used to initialize this object
73  /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
74  /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
75  /// transformations. If initialization should be propagated, then use the Initialize() function.
76  /// \details You can change the encoding to <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base
77  /// 32 Encoding with Extended Hex Alphabet</A> by performing the following:
78  /// <pre>
79  /// int lookup[256];
80  /// const byte ALPHABET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
81  /// Base32Decoder::InitializeDecodingLookupArray(lookup, ALPHABET, 32, true /*insensitive*/);
82  ///
83  /// Base32Decoder decoder;
84  /// AlgorithmParameters params = MakeParameters(Name::DecodingLookupArray(),(const int *)lookup);
85  /// decoder.IsolatedInitialize(params);</pre>
86  /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
87  void IsolatedInitialize(const NameValuePairs &parameters);
88 
89 private:
90  /// \brief Provides the default decoding lookup table
91  /// \return default decoding lookup table
92  static const int * CRYPTOPP_API GetDefaultDecodingLookupArray();
93 };
94 
95 /// \brief Base32 encodes data using extended hex
96 /// \details Converts data to base32 using extended hex alphabet. The alphabet is different than Base32Encoder.
97 /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder, <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base 32 Encoding with Extended Hex Alphabet</A>.
98 /// \since Crypto++ 6.0
100 {
101 public:
102  /// \brief Construct a Base32HexEncoder
103  /// \param attachment a BufferedTrasformation to attach to this object
104  /// \param uppercase a flag indicating uppercase output
105  /// \param groupSize the size of the grouping
106  /// \param separator the separator to use between groups
107  /// \param terminator the terminator appeand after processing
108  /// \details Base32HexEncoder() constructs a default encoder. The constructor lacks fields for padding and
109  /// line breaks. You must use IsolatedInitialize() to change the default padding character or suppress it.
110  /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
111  Base32HexEncoder(BufferedTransformation *attachment = NULLPTR, bool uppercase = true, int groupSize = 0, const std::string &separator = ":", const std::string &terminator = "")
112  : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
113  {
115  }
116 
117  /// \brief Initialize or reinitialize this object, without signal propagation
118  /// \param parameters a set of NameValuePairs used to initialize this object
119  /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
120  /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
121  /// transformations. If initialization should be propagated, then use the Initialize() function.
122  /// \details The following code modifies the padding and line break parameters for an encoder:
123  /// <pre>
124  /// Base32HexEncoder encoder;
125  /// AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);
126  /// encoder.IsolatedInitialize(params);</pre>
127  void IsolatedInitialize(const NameValuePairs &parameters);
128 };
129 
130 /// \brief Base32 decodes data using extended hex
131 /// \details Converts data from base32 using extended hex alphabet. The alphabet is different than Base32Decoder.
132 /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder, <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base 32 Encoding with Extended Hex Alphabet</A>.
133 /// \since Crypto++ 6.0
135 {
136 public:
137  /// \brief Construct a Base32HexDecoder
138  /// \param attachment a BufferedTrasformation to attach to this object
139  /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
141  : BaseN_Decoder(GetDefaultDecodingLookupArray(), 5, attachment) {}
142 
143  /// \brief Initialize or reinitialize this object, without signal propagation
144  /// \param parameters a set of NameValuePairs used to initialize this object
145  /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
146  /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
147  /// transformations. If initialization should be propagated, then use the Initialize() function.
148  void IsolatedInitialize(const NameValuePairs &parameters);
149 
150 private:
151  /// \brief Provides the default decoding lookup table
152  /// \return default decoding lookup table
153  static const int * CRYPTOPP_API GetDefaultDecodingLookupArray();
154 };
155 
156 NAMESPACE_END
157 
158 #endif
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:20
Base32 encodes data using DUDE encoding.
Definition: base32.h:18
const char * Uppercase()
bool
Definition: argnames.h:70
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:76
Base32 encodes data using extended hex.
Definition: base32.h:99
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:84
Abstract base classes that provide a uniform interface to this library.
Interface for buffered transformations.
Definition: cryptlib.h:1598
const char * Separator()
ConstByteArrayParameter.
Definition: argnames.h:68
Base32Encoder(BufferedTransformation *attachment=NULL, bool uppercase=true, int groupSize=0, const std::string &separator=":", const std::string &terminator="")
Construct a Base32Encoder.
Definition: base32.h:30
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:504
Base32HexEncoder(BufferedTransformation *attachment=NULL, bool uppercase=true, int groupSize=0, const std::string &separator=":", const std::string &terminator="")
Construct a Base32HexEncoder.
Definition: base32.h:111
Proxy filter that doesn&#39;t modify the underlying filter&#39;s input or output.
Definition: filters.h:1002
const char * GroupSize()
int
Definition: argnames.h:71
const char * Terminator()
ConstByteArrayParameter.
Definition: argnames.h:69
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:63
Base32Decoder(BufferedTransformation *attachment=NULL)
Construct a Base32Decoder.
Definition: base32.h:68
Base32HexDecoder(BufferedTransformation *attachment=NULL)
Construct a Base32HexDecoder.
Definition: base32.h:140
Base32 decodes data using extended hex.
Definition: base32.h:134
Filter that breaks input stream into groups of fixed size.
Definition: basecode.h:107
Base32 decodes data using DUDE encoding.
Definition: base32.h:62
Crypto++ library namespace.
Encoder for bases that are a power of 2.
Definition: basecode.h:17
Base classes for working with encoders and decoders.
Decoder for bases that are a power of 2.
Definition: basecode.h:56
Interface for retrieving values given their names.
Definition: cryptlib.h:293