00001 // base64.h - written and placed in the public domain by Wei Dai 00002 00003 //! \file base64.h 00004 //! \brief Classes for the Base64Encoder, Base64Decoder, Base64URLEncoder and Base64URLDecoder 00005 00006 #ifndef CRYPTOPP_BASE64_H 00007 #define CRYPTOPP_BASE64_H 00008 00009 #include "cryptlib.h" 00010 #include "basecode.h" 00011 00012 NAMESPACE_BEGIN(CryptoPP) 00013 00014 //! \class Base64Encoder 00015 //! \brief Base64 encodes data 00016 //! \details Base64 encodes data per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-4) 00017 //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter. 00018 class Base64Encoder : public SimpleProxyFilter 00019 { 00020 public: 00021 //! \brief Construct a Base64Encoder 00022 //! \param attachment a BufferedTrasformation to attach to this object 00023 //! \param insertLineBreaks a BufferedTrasformation to attach to this object 00024 //! \param maxLineLength the lenght of a line if line breaks are used 00025 //! \details Base64Encoder() constructs a default encoder. The constructor lacks parameters for padding. 00026 //! You must use IsolatedInitialize() to modify the Base64Encoder after construction. 00027 //! \sa IsolatedInitialize() for an example of modifying a Base64Encoder after construction. 00028 Base64Encoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = true, int maxLineLength = 72) 00029 : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment) 00030 { 00031 IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), insertLineBreaks)(Name::MaxLineLength(), maxLineLength)); 00032 } 00033 00034 //! \brief Initialize or reinitialize this object, without signal propagation 00035 //! \param parameters a set of NameValuePairs used to initialize this object 00036 //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable 00037 //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached 00038 //! transformations. If initialization should be propagated, then use the Initialize() function. 00039 //! \details The following code modifies the padding and line break parameters for an encoder: 00040 //! <pre> 00041 //! Base64Encoder encoder; 00042 //! AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false); 00043 //! encoder.IsolatedInitialize(params); 00044 //! </pre> 00045 void IsolatedInitialize(const NameValuePairs ¶meters); 00046 }; 00047 00048 //! \class Base64Decoder 00049 //! \brief Base64 decodes data 00050 //! \details Base64 decodes data per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-4) 00051 //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter. 00052 class Base64Decoder : public BaseN_Decoder 00053 { 00054 public: 00055 //! \brief Construct a Base64Decoder 00056 //! \param attachment a BufferedTrasformation to attach to this object 00057 Base64Decoder(BufferedTransformation *attachment = NULL) 00058 : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {} 00059 00060 //! \brief Initialize or reinitialize this object, without signal propagation 00061 //! \param parameters a set of NameValuePairs used to initialize this object 00062 //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable 00063 //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on 00064 //! attached transformations. If initialization should be propagated, then use the Initialize() function. 00065 void IsolatedInitialize(const NameValuePairs ¶meters) 00066 {CRYPTOPP_UNUSED(parameters);} 00067 00068 private: 00069 static const int * CRYPTOPP_API GetDecodingLookupArray(); 00070 }; 00071 00072 //! \class Base64URLEncoder 00073 //! \brief Base64 encodes data using a web safe alphabet 00074 //! \details Base64 encodes data using a web safe alphabet per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-5) 00075 //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter. 00076 class Base64URLEncoder : public SimpleProxyFilter 00077 { 00078 public: 00079 //! \brief Construct a Base64URLEncoder 00080 //! \param attachment a BufferedTrasformation to attach to this object 00081 //! \param insertLineBreaks a BufferedTrasformation to attach to this object 00082 //! \param maxLineLength the lenght of a line if line breaks are used 00083 //! \details Base64URLEncoder() constructs a default encoder. The constructor ignores insertLineBreaks 00084 //! and maxLineLength because the web and URL safe specifications don't use them. They are present 00085 //! in the constructor for API compatibility with Base64Encoder (drop-in replacement). The 00086 //! constructor also disables padding on the encoder for the same reason. 00087 //! \details If you need line breaks or padding, then you must use IsolatedInitialize() to set them 00088 //! after constructing a Base64URLEncoder. 00089 //! \sa IsolatedInitialize() for an example of modifying a Base64URLEncoder after construction. 00090 Base64URLEncoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = false, int maxLineLength = -1) 00091 : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment) 00092 { 00093 CRYPTOPP_UNUSED(insertLineBreaks), CRYPTOPP_UNUSED(maxLineLength); 00094 IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), false)(Name::MaxLineLength(), -1)(Name::Pad(),false)); 00095 } 00096 00097 //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable 00098 //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached 00099 //! transformations. If initialization should be propagated, then use the Initialize() function. 00100 //! \details The following code modifies the padding and line break parameters for an encoder: 00101 //! <pre> 00102 //! Base64URLEncoder encoder; 00103 //! AlgorithmParameters params = MakeParameters(Name::Pad(), true)(Name::InsertLineBreaks(), true); 00104 //! encoder.IsolatedInitialize(params); 00105 //! </pre> 00106 void IsolatedInitialize(const NameValuePairs ¶meters); 00107 }; 00108 00109 //! \class Base64URLDecoder 00110 //! \brief Base64 decodes data using a web safe alphabet 00111 //! \details Base64 decodes data using a web safe alphabet per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-5) 00112 //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter. 00113 class Base64URLDecoder : public BaseN_Decoder 00114 { 00115 public: 00116 //! \brief Construct a Base64URLDecoder 00117 //! \param attachment a BufferedTrasformation to attach to this object 00118 Base64URLDecoder(BufferedTransformation *attachment = NULL) 00119 : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {} 00120 00121 //! \brief Initialize or reinitialize this object, without signal propagation 00122 //! \param parameters a set of NameValuePairs used to initialize this object 00123 //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable 00124 //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on 00125 //! attached transformations. If initialization should be propagated, then use the Initialize() function. 00126 void IsolatedInitialize(const NameValuePairs ¶meters) 00127 {CRYPTOPP_UNUSED(parameters);} 00128 00129 private: 00130 static const int * CRYPTOPP_API GetDecodingLookupArray(); 00131 }; 00132 00133 NAMESPACE_END 00134 00135 #endif