Crypto++  5.6.3
Free C++ class library of cryptographic schemes
base64.h
Go to the documentation of this file.
1 // base64.h - written and placed in the public domain by Wei Dai
2 
3 //! \file base64.h
4 //! \brief Classes for the Base64Encoder, Base64Decoder, Base64URLEncoder and Base64URLDecoder
5 
6 #ifndef CRYPTOPP_BASE64_H
7 #define CRYPTOPP_BASE64_H
8 
9 #include "cryptlib.h"
10 #include "basecode.h"
11 
12 NAMESPACE_BEGIN(CryptoPP)
13 
14 //! \class Base64Encoder
15 //! \brief Base64 encodes data
16 //! \details Base64 encodes data per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-4)
17 //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter.
19 {
20 public:
21  //! \brief Construct a Base64Encoder
22  //! \param attachment a BufferedTrasformation to attach to this object
23  //! \param insertLineBreaks a BufferedTrasformation to attach to this object
24  //! \param maxLineLength the lenght of a line if line breaks are used
25  //! \details Base64Encoder() constructs a default encoder. The constructor lacks parameters for padding.
26  //! You must use IsolatedInitialize() to modify the Base64Encoder after construction.
27  //! \sa IsolatedInitialize() for an example of modifying a Base64Encoder after construction.
28  Base64Encoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = true, int maxLineLength = 72)
29  : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
30  {
31  IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), insertLineBreaks)(Name::MaxLineLength(), maxLineLength));
32  }
33 
34  //! \brief Initialize or reinitialize this object, without signal propagation
35  //! \param parameters a set of NameValuePairs used to initialize this object
36  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
37  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
38  //! transformations. If initialization should be propagated, then use the Initialize() function.
39  //! \details The following code modifies the padding and line break parameters for an encoder:
40  //! <pre>
41  //! Base64Encoder encoder;
42  //! AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);
43  //! encoder.IsolatedInitialize(params);
44  //! </pre>
45  void IsolatedInitialize(const NameValuePairs &parameters);
46 };
47 
48 //! \class Base64Decoder
49 //! \brief Base64 decodes data
50 //! \details Base64 decodes data per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-4)
51 //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter.
53 {
54 public:
55  //! \brief Construct a Base64Decoder
56  //! \param attachment a BufferedTrasformation to attach to this object
58  : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}
59 
60  //! \brief Initialize or reinitialize this object, without signal propagation
61  //! \param parameters a set of NameValuePairs used to initialize this object
62  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
63  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on
64  //! attached transformations. If initialization should be propagated, then use the Initialize() function.
65  void IsolatedInitialize(const NameValuePairs &parameters)
66  {CRYPTOPP_UNUSED(parameters);}
67 
68 private:
69  static const int * CRYPTOPP_API GetDecodingLookupArray();
70 };
71 
72 //! \class Base64URLEncoder
73 //! \brief Base64 encodes data using a web safe alphabet
74 //! \details Base64 encodes data using a web safe alphabet per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-5)
75 //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter.
77 {
78 public:
79  //! \brief Construct a Base64URLEncoder
80  //! \param attachment a BufferedTrasformation to attach to this object
81  //! \param insertLineBreaks a BufferedTrasformation to attach to this object
82  //! \param maxLineLength the lenght of a line if line breaks are used
83  //! \details Base64URLEncoder() constructs a default encoder. The constructor ignores insertLineBreaks
84  //! and maxLineLength because the web and URL safe specifications don't use them. They are present
85  //! in the constructor for API compatibility with Base64Encoder (drop-in replacement). The
86  //! constructor also disables padding on the encoder for the same reason.
87  //! \details If you need line breaks or padding, then you must use IsolatedInitialize() to set them
88  //! after constructing a Base64URLEncoder.
89  //! \sa IsolatedInitialize() for an example of modifying a Base64URLEncoder after construction.
90  Base64URLEncoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = false, int maxLineLength = -1)
91  : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
92  {
93  CRYPTOPP_UNUSED(insertLineBreaks), CRYPTOPP_UNUSED(maxLineLength);
94  IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), false)(Name::MaxLineLength(), -1)(Name::Pad(),false));
95  }
96 
97  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
98  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
99  //! transformations. If initialization should be propagated, then use the Initialize() function.
100  //! \details The following code modifies the padding and line break parameters for an encoder:
101  //! <pre>
102  //! Base64URLEncoder encoder;
103  //! AlgorithmParameters params = MakeParameters(Name::Pad(), true)(Name::InsertLineBreaks(), true);
104  //! encoder.IsolatedInitialize(params);
105  //! </pre>
106  void IsolatedInitialize(const NameValuePairs &parameters);
107 };
108 
109 //! \class Base64URLDecoder
110 //! \brief Base64 decodes data using a web safe alphabet
111 //! \details Base64 decodes data using a web safe alphabet per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-5)
112 //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter.
114 {
115 public:
116  //! \brief Construct a Base64URLDecoder
117  //! \param attachment a BufferedTrasformation to attach to this object
119  : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}
120 
121  //! \brief Initialize or reinitialize this object, without signal propagation
122  //! \param parameters a set of NameValuePairs used to initialize this object
123  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
124  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on
125  //! attached transformations. If initialization should be propagated, then use the Initialize() function.
126  void IsolatedInitialize(const NameValuePairs &parameters)
127  {CRYPTOPP_UNUSED(parameters);}
128 
129 private:
130  static const int * CRYPTOPP_API GetDecodingLookupArray();
131 };
132 
133 NAMESPACE_END
134 
135 #endif
Base64URLEncoder(BufferedTransformation *attachment=NULL, bool insertLineBreaks=false, int maxLineLength=-1)
Construct a Base64URLEncoder.
Definition: base64.h:90
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base64.h:126
Base64 decodes data using a web safe alphabet.
Definition: base64.h:113
Base64 decodes data.
Definition: base64.h:52
Abstract base classes that provide a uniform interface to this library.
Base64 encodes data using a web safe alphabet.
Definition: base64.h:76
Interface for buffered transformations.
Definition: cryptlib.h:1248
Base64 encodes data.
Definition: base64.h:18
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:487
simple proxy filter that doesn&#39;t modify the underlying filter&#39;s input or output
Definition: filters.h:694
Base64URLDecoder(BufferedTransformation *attachment=NULL)
Construct a Base64URLDecoder.
Definition: base64.h:118
Filter that breaks input stream into groups of fixed size.
Definition: basecode.h:110
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base64.h:65
Crypto++ library namespace.
Encoder for bases that are a power of 2.
Definition: basecode.h:18
Base64Decoder(BufferedTransformation *attachment=NULL)
Construct a Base64Decoder.
Definition: base64.h:57
Base classes for working with encoders and decoders.
Decoder for bases that are a power of 2.
Definition: basecode.h:58
Base64Encoder(BufferedTransformation *attachment=NULL, bool insertLineBreaks=true, int maxLineLength=72)
Construct a Base64Encoder.
Definition: base64.h:28
Interface for retrieving values given their names.
Definition: cryptlib.h:261