base64.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // base64.h - originally written and placed in the public domain by Wei Dai
  2. /// \file base64.h
  3. /// \brief Classes for the Base64Encoder, Base64Decoder, Base64URLEncoder and Base64URLDecoder
  4. #ifndef CRYPTOPP_BASE64_H
  5. #define CRYPTOPP_BASE64_H
  6. #include "cryptlib.h"
  7. #include "basecode.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. /// \brief Base64 encodes data using DUDE
  10. /// \details Base64 encodes data per <A HREF="http://tools.ietf.org/html/rfc4648#section-4">RFC 4648, Base 64 Encoding</A>.
  11. class Base64Encoder : public SimpleProxyFilter
  12. {
  13. public:
  14. /// \brief Construct a Base64Encoder
  15. /// \param attachment a BufferedTrasformation to attach to this object
  16. /// \param insertLineBreaks a BufferedTrasformation to attach to this object
  17. /// \param maxLineLength the length of a line if line breaks are used
  18. /// \details Base64Encoder constructs a default encoder. The constructor lacks a parameter for padding, and you must
  19. /// use IsolatedInitialize() to modify the Base64Encoder after construction.
  20. /// \sa IsolatedInitialize() for an example of modifying an encoder after construction.
  21. Base64Encoder(BufferedTransformation *attachment = NULLPTR, bool insertLineBreaks = true, int maxLineLength = 72)
  22. : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
  23. {
  24. IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), insertLineBreaks)(Name::MaxLineLength(), maxLineLength));
  25. }
  26. /// \brief Initialize or reinitialize this object, without signal propagation
  27. /// \param parameters a set of NameValuePairs used to initialize this object
  28. /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  29. /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
  30. /// transformations. If initialization should be propagated, then use the Initialize() function.
  31. /// \details The following code modifies the padding and line break parameters for an encoder:
  32. /// <pre>
  33. /// Base64Encoder encoder;
  34. /// AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);
  35. /// encoder.IsolatedInitialize(params);</pre>
  36. /// \details You can change the encoding to RFC 4648 web safe alphabet by performing the following:
  37. /// <pre>
  38. /// Base64Encoder encoder;
  39. /// const byte ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  40. /// AlgorithmParameters params = MakeParameters(Name::EncodingLookupArray(),(const byte *)ALPHABET);
  41. /// encoder.IsolatedInitialize(params);</pre>
  42. /// \details If you change the encoding alphabet, then you will need to change the decoding alphabet \a and
  43. /// the decoder's lookup table.
  44. /// \sa Base64URLEncoder for an encoder that provides the web safe alphabet, and Base64Decoder::IsolatedInitialize()
  45. /// for an example of modifying a decoder's lookup table after construction.
  46. void IsolatedInitialize(const NameValuePairs &parameters);
  47. };
  48. /// \brief Base64 decodes data using DUDE
  49. /// \details Base64 encodes data per <A HREF="http://tools.ietf.org/html/rfc4648#section-4">RFC 4648, Base 64 Encoding</A>.
  50. class Base64Decoder : public BaseN_Decoder
  51. {
  52. public:
  53. /// \brief Construct a Base64Decoder
  54. /// \param attachment a BufferedTrasformation to attach to this object
  55. /// \sa IsolatedInitialize() for an example of modifying an encoder after construction.
  56. Base64Decoder(BufferedTransformation *attachment = NULLPTR)
  57. : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}
  58. /// \brief Initialize or reinitialize this object, without signal propagation
  59. /// \param parameters a set of NameValuePairs used to initialize this object
  60. /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  61. /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
  62. /// transformations. If initialization should be propagated, then use the Initialize() function.
  63. /// \details The default decoding alpahbet is RFC 4868. You can change the to RFC 4868 web safe alphabet
  64. /// by performing the following:
  65. /// <pre>
  66. /// int lookup[256];
  67. /// const byte ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  68. /// Base64Decoder::InitializeDecodingLookupArray(lookup, ALPHABET, 64, false);
  69. ///
  70. /// Base64Decoder decoder;
  71. /// AlgorithmParameters params = MakeParameters(Name::DecodingLookupArray(),(const int *)lookup);
  72. /// decoder.IsolatedInitialize(params);</pre>
  73. /// \sa Base64URLDecoder for a decoder that provides the web safe alphabet, and Base64Encoder::IsolatedInitialize()
  74. /// for an example of modifying an encoder's alphabet after construction.
  75. void IsolatedInitialize(const NameValuePairs &parameters);
  76. private:
  77. /// \brief Provides the default decoding lookup table
  78. /// \return default decoding lookup table
  79. static const int * CRYPTOPP_API GetDecodingLookupArray();
  80. };
  81. /// \brief Base64 encodes data using a web safe alphabet
  82. /// \details Base64 encodes data per <A HREF="http://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Base 64 Encoding
  83. /// with URL and Filename Safe Alphabet</A>.
  84. class Base64URLEncoder : public SimpleProxyFilter
  85. {
  86. public:
  87. /// \brief Construct a Base64URLEncoder
  88. /// \param attachment a BufferedTrasformation to attach to this object
  89. /// \param insertLineBreaks a BufferedTrasformation to attach to this object
  90. /// \param maxLineLength the length of a line if line breaks are used
  91. /// \details Base64URLEncoder() constructs a default encoder using a web safe alphabet. The constructor ignores
  92. /// insertLineBreaks and maxLineLength because the web and URL safe specifications don't use them. They are
  93. /// present in the constructor for API compatibility with Base64Encoder so it is a drop-in replacement. The
  94. /// constructor also disables padding on the encoder for the same reason.
  95. /// \details If you need line breaks or padding, then you must use IsolatedInitialize() to set them
  96. /// after constructing a Base64URLEncoder.
  97. /// \sa Base64Encoder for an encoder that provides a classic alphabet, and Base64URLEncoder::IsolatedInitialize
  98. /// for an example of modifying an encoder after construction.
  99. Base64URLEncoder(BufferedTransformation *attachment = NULLPTR, bool insertLineBreaks = false, int maxLineLength = -1)
  100. : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
  101. {
  102. CRYPTOPP_UNUSED(insertLineBreaks), CRYPTOPP_UNUSED(maxLineLength);
  103. IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), false)(Name::MaxLineLength(), -1)(Name::Pad(),false));
  104. }
  105. /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  106. /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
  107. /// transformations. If initialization should be propagated, then use the Initialize() function.
  108. /// \details The following code modifies the padding and line break parameters for an encoder:
  109. /// <pre>
  110. /// Base64URLEncoder encoder;
  111. /// AlgorithmParameters params = MakeParameters(Name::Pad(), true)(Name::InsertLineBreaks(), true);
  112. /// encoder.IsolatedInitialize(params);</pre>
  113. /// \sa Base64Encoder for an encoder that provides a classic alphabet.
  114. void IsolatedInitialize(const NameValuePairs &parameters);
  115. };
  116. /// \brief Base64 decodes data using a web safe alphabet
  117. /// \details Base64 encodes data per <A HREF="http://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Base 64 Encoding
  118. /// with URL and Filename Safe Alphabet</A>.
  119. class Base64URLDecoder : public BaseN_Decoder
  120. {
  121. public:
  122. /// \brief Construct a Base64URLDecoder
  123. /// \param attachment a BufferedTrasformation to attach to this object
  124. /// \details Base64URLDecoder() constructs a default decoder using a web safe alphabet.
  125. /// \sa Base64Decoder for a decoder that provides a classic alphabet.
  126. Base64URLDecoder(BufferedTransformation *attachment = NULLPTR)
  127. : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}
  128. /// \brief Initialize or reinitialize this object, without signal propagation
  129. /// \param parameters a set of NameValuePairs used to initialize this object
  130. /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  131. /// number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on
  132. /// attached transformations. If initialization should be propagated, then use the Initialize() function.
  133. /// \sa Base64Decoder for a decoder that provides a classic alphabet, and Base64URLEncoder::IsolatedInitialize
  134. /// for an example of modifying an encoder after construction.
  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 GetDecodingLookupArray();
  140. };
  141. NAMESPACE_END
  142. #endif