Crypto++ 8.6 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
18 class Base32Encoder : public SimpleProxyFilter
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
62 class Base32Decoder : public BaseN_Decoder
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
99 class Base32HexEncoder : public SimpleProxyFilter
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 {
114 IsolatedInitialize(MakeParameters(Name::Uppercase(), uppercase)(Name::GroupSize(), groupSize)(Name::Separator(), ConstByteArrayParameter(separator))(Name::Terminator(), ConstByteArrayParameter(terminator)));
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
134 class Base32HexDecoder : public BaseN_Decoder
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
140 Base32HexDecoder(BufferedTransformation *attachment = NULLPTR)
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
Base32Decoder::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:63
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:508
Base32HexDecoder::Base32HexDecoder
Base32HexDecoder(BufferedTransformation *attachment=NULL)
Construct a Base32HexDecoder.
Definition: base32.h:140
Base32Encoder
Base32 encodes data using DUDE encoding.
Definition: base32.h:18
Base32HexEncoder::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:76
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1651
SimpleProxyFilter
Proxy filter that doesn't modify the underlying filter's input or output.
Definition: filters.h:1064
Grouper
Filter that breaks input stream into groups of fixed size.
Definition: basecode.h:112
Base32Encoder::Base32Encoder
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
basecode.h
Base classes for working with encoders and decoders.
Base32HexDecoder::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:84
BaseN_Encoder
Encoder for bases that are a power of 2.
Definition: basecode.h:17
Base32HexEncoder
Base32 encodes data using extended hex.
Definition: base32.h:99
CryptoPP
Crypto++ library namespace.
BaseN_Decoder
Decoder for bases that are a power of 2.
Definition: basecode.h:58
CRYPTOPP_API
#define CRYPTOPP_API
Win32 calling convention.
Definition: config_dll.h:119
Base32HexDecoder
Base32 decodes data using extended hex.
Definition: base32.h:134
Base32Decoder::Base32Decoder
Base32Decoder(BufferedTransformation *attachment=NULL)
Construct a Base32Decoder.
Definition: base32.h:68
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:321
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
ConstByteArrayParameter
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:24
Base32Decoder
Base32 decodes data using DUDE encoding.
Definition: base32.h:62
Base32HexEncoder::Base32HexEncoder
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