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