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