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 <A HREF="http://tools.ietf.org/html/rfc4648#section-4">RFC 4648, Base 64 Encoding</A>.
18 {
19 public:
20  //! \brief Construct a Base64Encoder
21  //! \param attachment a BufferedTrasformation to attach to this object
22  //! \param insertLineBreaks a BufferedTrasformation to attach to this object
23  //! \param maxLineLength the lenght of a line if line breaks are used
24  //! \details Base64Encoder constructs a default encoder. The constructor lacks a parameter for padding, and you must
25  //! use IsolatedInitialize() to modify the Base64Encoder after construction.
26  //! \sa IsolatedInitialize() for an example of modifying an encoder after construction.
27  Base64Encoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = true, int maxLineLength = 72)
28  : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
29  {
30  IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), insertLineBreaks)(Name::MaxLineLength(), maxLineLength));
31  }
32 
33  //! \brief Initialize or reinitialize this object, without signal propagation
34  //! \param parameters a set of NameValuePairs used to initialize this object
35  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
36  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
37  //! transformations. If initialization should be propagated, then use the Initialize() function.
38  //! \details The following code modifies the padding and line break parameters for an encoder:
39  //! <pre>
40  //! Base64Encoder encoder;
41  //! AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);
42  //! encoder.IsolatedInitialize(params);</pre>
43  //! \details You can change the encoding to RFC 4648 web safe alphabet by performing the following:
44  //! <pre>
45  //! Base64Encoder encoder;
46  //! const byte ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
47  //! AlgorithmParameters params = MakeParameters(Name::EncodingLookupArray(),(const byte *)ALPHABET);
48  //! encoder.IsolatedInitialize(params);</pre>
49  //! \details If you change the encoding alphabet, then you will need to change the decoding alphabet \a and
50  //! the decoder's lookup table.
51  //! \sa Base64URLEncoder for an encoder that provides the web safe alphabet, and Base64Decoder::IsolatedInitialize()
52  //! for an example of modifying a decoder's lookup table after construction.
53  void IsolatedInitialize(const NameValuePairs &parameters);
54 };
55 
56 //! \class Base64Decoder
57 //! \brief Base64 decodes data
58 //! \details Base64 encodes data per <A HREF="http://tools.ietf.org/html/rfc4648#section-4">RFC 4648, Base 64 Encoding</A>.
60 {
61 public:
62  //! \brief Construct a Base64Decoder
63  //! \param attachment a BufferedTrasformation to attach to this object
64  //! \sa IsolatedInitialize() for an example of modifying an encoder after construction.
66  : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}
67 
68  //! \brief Initialize or reinitialize this object, without signal propagation
69  //! \param parameters a set of NameValuePairs used to initialize this object
70  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
71  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
72  //! transformations. If initialization should be propagated, then use the Initialize() function.
73  //! \details The default decoding alpahbet is RFC 4868. You can change the to RFC 4868 web safe alphabet
74  //! by performing the following:
75  //! <pre>
76  //! int lookup[256];
77  //! const byte ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
78  //! Base64Decoder::InitializeDecodingLookupArray(lookup, ALPHABET, 64, false);
79  //!
80  //! Base64Decoder decoder;
81  //! AlgorithmParameters params = MakeParameters(Name::DecodingLookupArray(),(const int *)lookup);
82  //! decoder.IsolatedInitialize(params);</pre>
83  //! \sa Base64URLDecoder for a decoder that provides the web safe alphabet, and Base64Encoder::IsolatedInitialize()
84  //! for an example of modifying an encoder's alphabet after construction.
85  void IsolatedInitialize(const NameValuePairs &parameters);
86 
87 private:
88  //! \brief Provides the default decoding lookup table
89  //! \return default decoding lookup table
90  static const int * CRYPTOPP_API GetDecodingLookupArray();
91 };
92 
93 //! \class Base64URLEncoder
94 //! \brief Base64 encodes data using a web safe alphabet
95 //! \details Base64 encodes data per <A HREF="http://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Base 64 Encoding
96 //! with URL and Filename Safe Alphabet</A>.
98 {
99 public:
100  //! \brief Construct a Base64URLEncoder
101  //! \param attachment a BufferedTrasformation to attach to this object
102  //! \param insertLineBreaks a BufferedTrasformation to attach to this object
103  //! \param maxLineLength the lenght of a line if line breaks are used
104  //! \details Base64URLEncoder() constructs a default encoder using a web safe alphabet. The constructor ignores
105  //! insertLineBreaks and maxLineLength because the web and URL safe specifications don't use them. They are
106  //! present in the constructor for API compatibility with Base64Encoder so it is a drop-in replacement. The
107  //! constructor also disables padding on the encoder for the same reason.
108  //! \details If you need line breaks or padding, then you must use IsolatedInitialize() to set them
109  //! after constructing a Base64URLEncoder.
110  //! \sa Base64Encoder for an encoder that provides a classic alphabet, and Base64URLEncoder::IsolatedInitialize
111  //! for an example of modifying an encoder after construction.
112  Base64URLEncoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = false, int maxLineLength = -1)
113  : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
114  {
115  CRYPTOPP_UNUSED(insertLineBreaks), CRYPTOPP_UNUSED(maxLineLength);
116  IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), false)(Name::MaxLineLength(), -1)(Name::Pad(),false));
117  }
118 
119  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
120  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
121  //! transformations. If initialization should be propagated, then use the Initialize() function.
122  //! \details The following code modifies the padding and line break parameters for an encoder:
123  //! <pre>
124  //! Base64URLEncoder encoder;
125  //! AlgorithmParameters params = MakeParameters(Name::Pad(), true)(Name::InsertLineBreaks(), true);
126  //! encoder.IsolatedInitialize(params);</pre>
127  //! \sa Base64Encoder for an encoder that provides a classic alphabet.
128  void IsolatedInitialize(const NameValuePairs &parameters);
129 };
130 
131 //! \class Base64URLDecoder
132 //! \brief Base64 decodes data using a web safe alphabet
133 //! \details Base64 encodes data per <A HREF="http://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Base 64 Encoding
134 //! with URL and Filename Safe Alphabet</A>.
136 {
137 public:
138  //! \brief Construct a Base64URLDecoder
139  //! \param attachment a BufferedTrasformation to attach to this object
140  //! \details Base64URLDecoder() constructs a default decoder using a web safe alphabet.
141  //! \sa Base64Decoder for a decoder that provides a classic alphabet.
143  : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}
144 
145  //! \brief Initialize or reinitialize this object, without signal propagation
146  //! \param parameters a set of NameValuePairs used to initialize this object
147  //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
148  //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on
149  //! attached transformations. If initialization should be propagated, then use the Initialize() function.
150  //! \sa Base64Decoder for a decoder that provides a classic alphabet, and Base64URLEncoder::IsolatedInitialize
151  //! for an example of modifying an encoder after construction.
152  void IsolatedInitialize(const NameValuePairs &parameters);
153 
154 private:
155  //! \brief Provides the default decoding lookup table
156  //! \return default decoding lookup table
157  static const int * CRYPTOPP_API GetDecodingLookupArray();
158 };
159 
160 NAMESPACE_END
161 
162 #endif
Base64URLEncoder(BufferedTransformation *attachment=NULL, bool insertLineBreaks=false, int maxLineLength=-1)
Construct a Base64URLEncoder.
Definition: base64.h:112
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base64.cpp:70
Base64 decodes data using a web safe alphabet.
Definition: base64.h:135
Base64 decodes data.
Definition: base64.h:59
Abstract base classes that provide a uniform interface to this library.
Base64 encodes data using a web safe alphabet.
Definition: base64.h:97
Interface for buffered transformations.
Definition: cryptlib.h:1342
Base64 encodes data.
Definition: base64.h:17
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:554
Proxy filter that doesn't modify the underlying filter's input or output.
Definition: filters.h:894
void IsolatedInitialize(const NameValuePairs &parameters)
Definition: base64.cpp:33
Base64URLDecoder(BufferedTransformation *attachment=NULL)
Construct a Base64URLDecoder.
Definition: base64.h:142
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.cpp:50
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:65
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:27
Interface for retrieving values given their names.
Definition: cryptlib.h:277