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 
3 //! \file
4 //! \brief Classes for Base32Encoder and Base32Decoder
5 
6 #ifndef CRYPTOPP_BASE32_H
7 #define CRYPTOPP_BASE32_H
8 
9 #include "cryptlib.h"
10 #include "basecode.h"
11 
12 NAMESPACE_BEGIN(CryptoPP)
13 
14 //! \class Base32Encoder
15 //! \brief Base32 encodes data
16 //! \details Converts data to base32. 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 {
19 public:
20  //! \brief Construct a Base32Encoder
21  //! \param attachment a BufferedTrasformation to attach to this object
22  //! \param uppercase a flag indicating uppercase output
23  //! \param groupSize the size of the grouping
24  //! \param separator the separator to use between groups
25  //! \param terminator the terminator appeand after processing
26  //! \details Base32Encoder() constructs a default encoder. The constructor lacks fields for padding and
27  //! line breaks. You must use IsolatedInitialize() to change the default padding character or suppress it.
28  //! \sa IsolatedInitialize() for an example of modifying a Base32Encoder after construction.
29  Base32Encoder(BufferedTransformation *attachment = NULL, bool uppercase = true, int groupSize = 0, const std::string &separator = ":", const std::string &terminator = "")
30  : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
31  {
32  IsolatedInitialize(MakeParameters(Name::Uppercase(), uppercase)(Name::GroupSize(), groupSize)(Name::Separator(), ConstByteArrayParameter(separator))(Name::Terminator(), ConstByteArrayParameter(terminator)));
33  }
34 
35  //! \brief Initialize or reinitialize this object, without signal propagation
36  //! \param parameters a set of NameValuePairs used to initialize this object
37  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
38  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
39  //! transformations. If initialization should be propagated, then use the Initialize() function.
40  //! \details The following code modifies the padding and line break parameters for an encoder:
41  //! <pre>
42  //! Base32Encoder encoder;
43  //! AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);
44  //! encoder.IsolatedInitialize(params);</pre>
45  //! \details You can change the encoding to <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base
46  //! 32 Encoding with Extended Hex Alphabet</A> by performing the following:
47  //! <pre>
48  //! Base32Encoder encoder;
49  //! const byte ALPHABET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
50  //! AlgorithmParameters params = MakeParameters(Name::EncodingLookupArray(),(const byte *)ALPHABET);
51  //! encoder.IsolatedInitialize(params);</pre>
52  //! \details If you change the encoding alphabet, then you will need to change the decoding alphabet \a and
53  //! the decoder's lookup table.
54  //! \sa Base32Decoder::IsolatedInitialize() for an example of changing a Base32Decoder's lookup table.
55  void IsolatedInitialize(const NameValuePairs &parameters);
56 };
57 
58 //! \class Base32Decoder
59 //! \brief Base32 decodes data
60 //! \details Decode base32 data. 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>.
62 {
63 public:
64  //! \brief Construct a Base32Decoder
65  //! \param attachment a BufferedTrasformation to attach to this object
66  //! \sa IsolatedInitialize() for an example of modifying a Base32Decoder after construction.
68  : BaseN_Decoder(GetDefaultDecodingLookupArray(), 5, attachment) {}
69 
70  //! \brief Initialize or reinitialize this object, without signal propagation
71  //! \param parameters a set of NameValuePairs used to initialize this object
72  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
73  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
74  //! transformations. If initialization should be propagated, then use the Initialize() function.
75  //! \details You can change the encoding to <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base
76  //! 32 Encoding with Extended Hex Alphabet</A> by performing the following:
77  //! <pre>
78  //! int lookup[256];
79  //! const byte ALPHABET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
80  //! Base32Decoder::InitializeDecodingLookupArray(lookup, ALPHABET, 32, true /*insensitive*/);
81  //!
82  //! Base32Decoder decoder;
83  //! AlgorithmParameters params = MakeParameters(Name::DecodingLookupArray(),(const int *)lookup);
84  //! decoder.IsolatedInitialize(params);</pre>
85  //! \sa Base32Encoder::IsolatedInitialize() for an example of changing a Base32Encoder's alphabet.
86  void IsolatedInitialize(const NameValuePairs &parameters);
87 
88 private:
89  //! \brief Provides the default decoding lookup table
90  //! \return default decoding lookup table
91  static const int * CRYPTOPP_API GetDefaultDecodingLookupArray();
92 };
93 
94 NAMESPACE_END
95 
96 #endif
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
Base32 encodes data.
Definition: base32.h:17
Abstract base classes that provide a uniform interface to this library.
Interface for buffered transformations.
Definition: cryptlib.h:1363
Base32Encoder(BufferedTransformation *attachment=NULL, bool uppercase=true, int groupSize=0, const std::string &separator=":", const std::string &terminator="")
Construct a Base32Encoder.
Definition: base32.h:29
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:563
Proxy filter that doesn't modify the underlying filter's input or output.
Definition: filters.h:963
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:22
Base32Decoder(BufferedTransformation *attachment=NULL)
Construct a Base32Decoder.
Definition: base32.h:67
Filter that breaks input stream into groups of fixed size.
Definition: basecode.h:110
Base32 decodes data.
Definition: base32.h:61
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:278