base32.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /// \file base32.h
  4. /// \brief Classes for Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
  5. #ifndef CRYPTOPP_BASE32_H
  6. #define CRYPTOPP_BASE32_H
  7. #include "cryptlib.h"
  8. #include "basecode.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. /// \brief Base32 encodes data using DUDE encoding
  11. /// \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>.
  12. /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
  13. class Base32Encoder : public SimpleProxyFilter
  14. {
  15. public:
  16. /// \brief Construct a Base32Encoder
  17. /// \param attachment a BufferedTrasformation to attach to this object
  18. /// \param uppercase a flag indicating uppercase output
  19. /// \param groupSize the size of the grouping
  20. /// \param separator the separator to use between groups
  21. /// \param terminator the terminator appeand after processing
  22. /// \details Base32Encoder() constructs a default encoder. The constructor lacks fields for padding and
  23. /// line breaks. You must use IsolatedInitialize() to change the default padding character or suppress it.
  24. /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
  25. Base32Encoder(BufferedTransformation *attachment = NULLPTR, bool uppercase = true, int groupSize = 0, const std::string &separator = ":", const std::string &terminator = "")
  26. : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
  27. {
  28. IsolatedInitialize(MakeParameters(Name::Uppercase(), uppercase)(Name::GroupSize(), groupSize)(Name::Separator(), ConstByteArrayParameter(separator))(Name::Terminator(), ConstByteArrayParameter(terminator)));
  29. }
  30. /// \brief Initialize or reinitialize this object, without signal propagation
  31. /// \param parameters a set of NameValuePairs used to initialize this object
  32. /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  33. /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
  34. /// transformations. If initialization should be propagated, then use the Initialize() function.
  35. /// \details The following code modifies the padding and line break parameters for an encoder:
  36. /// <pre>
  37. /// Base32Encoder encoder;
  38. /// AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);
  39. /// encoder.IsolatedInitialize(params);</pre>
  40. /// \details You can change the encoding to <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base
  41. /// 32 Encoding with Extended Hex Alphabet</A> by performing the following:
  42. /// <pre>
  43. /// Base32Encoder encoder;
  44. /// const byte ALPHABET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
  45. /// AlgorithmParameters params = MakeParameters(Name::EncodingLookupArray(),(const byte *)ALPHABET);
  46. /// encoder.IsolatedInitialize(params);</pre>
  47. /// \details If you change the encoding alphabet, then you will need to change the decoding alphabet \a and
  48. /// the decoder's lookup table.
  49. /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
  50. void IsolatedInitialize(const NameValuePairs &parameters);
  51. };
  52. /// \brief Base32 decodes data using DUDE encoding
  53. /// \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>.
  54. /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
  55. class Base32Decoder : public BaseN_Decoder
  56. {
  57. public:
  58. /// \brief Construct a Base32Decoder
  59. /// \param attachment a BufferedTrasformation to attach to this object
  60. /// \sa IsolatedInitialize() for an example of modifying a Base32Decoder after construction.
  61. Base32Decoder(BufferedTransformation *attachment = NULLPTR)
  62. : BaseN_Decoder(GetDefaultDecodingLookupArray(), 5, attachment) {}
  63. /// \brief Initialize or reinitialize this object, without signal propagation
  64. /// \param parameters a set of NameValuePairs used to initialize this object
  65. /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  66. /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
  67. /// transformations. If initialization should be propagated, then use the Initialize() function.
  68. /// \details You can change the encoding to <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base
  69. /// 32 Encoding with Extended Hex Alphabet</A> by performing the following:
  70. /// <pre>
  71. /// int lookup[256];
  72. /// const byte ALPHABET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
  73. /// Base32Decoder::InitializeDecodingLookupArray(lookup, ALPHABET, 32, true /*insensitive*/);
  74. ///
  75. /// Base32Decoder decoder;
  76. /// AlgorithmParameters params = MakeParameters(Name::DecodingLookupArray(),(const int *)lookup);
  77. /// decoder.IsolatedInitialize(params);</pre>
  78. /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
  79. void IsolatedInitialize(const NameValuePairs &parameters);
  80. private:
  81. /// \brief Provides the default decoding lookup table
  82. /// \return default decoding lookup table
  83. static const int * CRYPTOPP_API GetDefaultDecodingLookupArray();
  84. };
  85. /// \brief Base32 encodes data using extended hex
  86. /// \details Converts data to base32 using extended hex alphabet. The alphabet is different than Base32Encoder.
  87. /// \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>.
  88. /// \since Crypto++ 6.0
  89. class Base32HexEncoder : public SimpleProxyFilter
  90. {
  91. public:
  92. /// \brief Construct a Base32HexEncoder
  93. /// \param attachment a BufferedTrasformation to attach to this object
  94. /// \param uppercase a flag indicating uppercase output
  95. /// \param groupSize the size of the grouping
  96. /// \param separator the separator to use between groups
  97. /// \param terminator the terminator appeand after processing
  98. /// \details Base32HexEncoder() constructs a default encoder. The constructor lacks fields for padding and
  99. /// line breaks. You must use IsolatedInitialize() to change the default padding character or suppress it.
  100. /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
  101. Base32HexEncoder(BufferedTransformation *attachment = NULLPTR, bool uppercase = true, int groupSize = 0, const std::string &separator = ":", const std::string &terminator = "")
  102. : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
  103. {
  104. IsolatedInitialize(MakeParameters(Name::Uppercase(), uppercase)(Name::GroupSize(), groupSize)(Name::Separator(), ConstByteArrayParameter(separator))(Name::Terminator(), ConstByteArrayParameter(terminator)));
  105. }
  106. /// \brief Initialize or reinitialize this object, without signal propagation
  107. /// \param parameters a set of NameValuePairs used to initialize this object
  108. /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  109. /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
  110. /// transformations. If initialization should be propagated, then use the Initialize() function.
  111. /// \details The following code modifies the padding and line break parameters for an encoder:
  112. /// <pre>
  113. /// Base32HexEncoder encoder;
  114. /// AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);
  115. /// encoder.IsolatedInitialize(params);</pre>
  116. void IsolatedInitialize(const NameValuePairs &parameters);
  117. };
  118. /// \brief Base32 decodes data using extended hex
  119. /// \details Converts data from base32 using extended hex alphabet. The alphabet is different than Base32Decoder.
  120. /// \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>.
  121. /// \since Crypto++ 6.0
  122. class Base32HexDecoder : public BaseN_Decoder
  123. {
  124. public:
  125. /// \brief Construct a Base32HexDecoder
  126. /// \param attachment a BufferedTrasformation to attach to this object
  127. /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder
  128. Base32HexDecoder(BufferedTransformation *attachment = NULLPTR)
  129. : BaseN_Decoder(GetDefaultDecodingLookupArray(), 5, attachment) {}
  130. /// \brief Initialize or reinitialize this object, without signal propagation
  131. /// \param parameters a set of NameValuePairs used to initialize this object
  132. /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  133. /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
  134. /// transformations. If initialization should be propagated, then use the Initialize() function.
  135. void IsolatedInitialize(const NameValuePairs &parameters);
  136. private:
  137. /// \brief Provides the default decoding lookup table
  138. /// \return default decoding lookup table
  139. static const int * CRYPTOPP_API GetDefaultDecodingLookupArray();
  140. };
  141. NAMESPACE_END
  142. #endif