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).
86  //! \details If you need line breaks and padding, then you must use IsolatedInitialize() to set them
87  //! after constructing a Base64URLEncoder.
88  //! \sa IsolatedInitialize() for an example of modifying a Base64URLEncoder after construction.
89  Base64URLEncoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = false, int maxLineLength = -1)
90  : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
91  {
92  IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), false)(Name::MaxLineLength(), -1)(Name::Pad(),false));
93  }
94 
95  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
96  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
97  //! transformations. If initialization should be propagated, then use the Initialize() function.
98  //! \details The following code modifies the padding and line break parameters for an encoder:
99  //! <pre>
100  //! Base64URLEncoder encoder;
101  //! AlgorithmParameters params = MakeParameters(Name::Pad(), true)(Name::InsertLineBreaks(), true);
102  //! encoder.IsolatedInitialize(params);
103  //! </pre>
104  void IsolatedInitialize(const NameValuePairs &parameters);
105 };
106 
107 //! \class Base64URLDecoder
108 //! \brief Base64 decodes data using a web safe alphabet
109 //! \details Base64 decodes data using a web safe alphabet per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-5)
110 //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter.
112 {
113 public:
114  //! \brief Construct a Base64URLDecoder
115  //! \param attachment a BufferedTrasformation to attach to this object
117  : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}
118 
119  //! \brief Initialize or reinitialize this object, without signal propagation
120  //! \param parameters a set of NameValuePairs used to initialize this object
121  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
122  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on
123  //! attached transformations. If initialization should be propagated, then use the Initialize() function.
124  void IsolatedInitialize(const NameValuePairs &parameters)
125  {CRYPTOPP_UNUSED(parameters);}
126 
127 private:
128  static const int * CRYPTOPP_API GetDecodingLookupArray();
129 };
130 
131 NAMESPACE_END
132 
133 #endif
Base64URLEncoder(BufferedTransformation *attachment=NULL, bool insertLineBreaks=false, int maxLineLength=-1)
Construct a Base64URLEncoder.
Definition: base64.h:89
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base64.h:124
Base64 decodes data using a web safe alphabet.
Definition: base64.h:111
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:1247
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:116
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