Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:54

0001 // base32.h - written and placed in the public domain by Frank Palazzolo, based on hex.cpp by Wei Dai

0002 //              extended hex alphabet added by JW in November, 2017.

0003 
0004 /// \file base32.h

0005 /// \brief Classes for Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder

0006 
0007 #ifndef CRYPTOPP_BASE32_H
0008 #define CRYPTOPP_BASE32_H
0009 
0010 #include "cryptlib.h"
0011 #include "basecode.h"
0012 
0013 NAMESPACE_BEGIN(CryptoPP)
0014 
0015 /// \brief Base32 encodes data using DUDE encoding

0016 /// \details Converts data to base32 using DUDE encoding. The default code is based on <A HREF="http://www.ietf.org/proceedings/51/I-D/draft-ietf-idn-dude-02.txt">Differential Unicode Domain Encoding (DUDE) (draft-ietf-idn-dude-02.txt)</A>.

0017 /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder

0018 class Base32Encoder : public SimpleProxyFilter
0019 {
0020 public:
0021     /// \brief Construct a Base32Encoder

0022     /// \param attachment a BufferedTrasformation to attach to this object

0023     /// \param uppercase a flag indicating uppercase output

0024     /// \param groupSize the size of the grouping

0025     /// \param separator the separator to use between groups

0026     /// \param terminator the terminator appeand after processing

0027     /// \details Base32Encoder() constructs a default encoder. The constructor lacks fields for padding and

0028     ///   line breaks. You must use IsolatedInitialize() to change the default padding character or suppress it.

0029     /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder

0030     Base32Encoder(BufferedTransformation *attachment = NULLPTR, bool uppercase = true, int groupSize = 0, const std::string &separator = ":", const std::string &terminator = "")
0031         : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
0032     {
0033         IsolatedInitialize(MakeParameters(Name::Uppercase(), uppercase)(Name::GroupSize(), groupSize)(Name::Separator(), ConstByteArrayParameter(separator))(Name::Terminator(), ConstByteArrayParameter(terminator)));
0034     }
0035 
0036     /// \brief Initialize or reinitialize this object, without signal propagation

0037     /// \param parameters a set of NameValuePairs used to initialize this object

0038     /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable

0039     ///   number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached

0040     ///   transformations. If initialization should be propagated, then use the Initialize() function.

0041     /// \details The following code modifies the padding and line break parameters for an encoder:

0042     ///   <pre>

0043     ///     Base32Encoder encoder;

0044     ///     AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);

0045     ///     encoder.IsolatedInitialize(params);</pre>

0046     /// \details You can change the encoding to <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base

0047     ///   32 Encoding with Extended Hex Alphabet</A> by performing the following:

0048     ///   <pre>

0049     ///     Base32Encoder encoder;

0050     ///     const byte ALPHABET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";

0051     ///     AlgorithmParameters params = MakeParameters(Name::EncodingLookupArray(),(const byte *)ALPHABET);

0052     ///     encoder.IsolatedInitialize(params);</pre>

0053     /// \details If you change the encoding alphabet, then you will need to change the decoding alphabet \a and

0054     ///   the decoder's lookup table.

0055     /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder

0056     void IsolatedInitialize(const NameValuePairs &parameters);
0057 };
0058 
0059 /// \brief Base32 decodes data using DUDE encoding

0060 /// \details Converts data from base32 using DUDE encoding. The default code is based on <A HREF="http://www.ietf.org/proceedings/51/I-D/draft-ietf-idn-dude-02.txt">Differential Unicode Domain Encoding (DUDE) (draft-ietf-idn-dude-02.txt)</A>.

0061 /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder

0062 class Base32Decoder : public BaseN_Decoder
0063 {
0064 public:
0065     /// \brief Construct a Base32Decoder

0066     /// \param attachment a BufferedTrasformation to attach to this object

0067     /// \sa IsolatedInitialize() for an example of modifying a Base32Decoder after construction.

0068     Base32Decoder(BufferedTransformation *attachment = NULLPTR)
0069         : BaseN_Decoder(GetDefaultDecodingLookupArray(), 5, attachment) {}
0070 
0071     /// \brief Initialize or reinitialize this object, without signal propagation

0072     /// \param parameters a set of NameValuePairs used to initialize this object

0073     /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable

0074     ///   number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached

0075     ///   transformations. If initialization should be propagated, then use the Initialize() function.

0076     /// \details You can change the encoding to <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base

0077     ///   32 Encoding with Extended Hex Alphabet</A> by performing the following:

0078     ///   <pre>

0079     ///     int lookup[256];

0080     ///     const byte ALPHABET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";

0081     ///     Base32Decoder::InitializeDecodingLookupArray(lookup, ALPHABET, 32, true /*insensitive*/);

0082     ///

0083     ///     Base32Decoder decoder;

0084     ///     AlgorithmParameters params = MakeParameters(Name::DecodingLookupArray(),(const int *)lookup);

0085     ///     decoder.IsolatedInitialize(params);</pre>

0086     /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder

0087     void IsolatedInitialize(const NameValuePairs &parameters);
0088 
0089 private:
0090     /// \brief Provides the default decoding lookup table

0091     /// \return default decoding lookup table

0092     static const int * CRYPTOPP_API GetDefaultDecodingLookupArray();
0093 };
0094 
0095 /// \brief Base32 encodes data using extended hex

0096 /// \details Converts data to base32 using extended hex alphabet. The alphabet is different than Base32Encoder.

0097 /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder, <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base 32 Encoding with Extended Hex Alphabet</A>.

0098 /// \since Crypto++ 6.0

0099 class Base32HexEncoder : public SimpleProxyFilter
0100 {
0101 public:
0102     /// \brief Construct a Base32HexEncoder

0103     /// \param attachment a BufferedTrasformation to attach to this object

0104     /// \param uppercase a flag indicating uppercase output

0105     /// \param groupSize the size of the grouping

0106     /// \param separator the separator to use between groups

0107     /// \param terminator the terminator appeand after processing

0108     /// \details Base32HexEncoder() constructs a default encoder. The constructor lacks fields for padding and

0109     ///   line breaks. You must use IsolatedInitialize() to change the default padding character or suppress it.

0110     /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder

0111     Base32HexEncoder(BufferedTransformation *attachment = NULLPTR, bool uppercase = true, int groupSize = 0, const std::string &separator = ":", const std::string &terminator = "")
0112         : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
0113     {
0114         IsolatedInitialize(MakeParameters(Name::Uppercase(), uppercase)(Name::GroupSize(), groupSize)(Name::Separator(), ConstByteArrayParameter(separator))(Name::Terminator(), ConstByteArrayParameter(terminator)));
0115     }
0116 
0117     /// \brief Initialize or reinitialize this object, without signal propagation

0118     /// \param parameters a set of NameValuePairs used to initialize this object

0119     /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable

0120     ///   number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached

0121     ///   transformations. If initialization should be propagated, then use the Initialize() function.

0122     /// \details The following code modifies the padding and line break parameters for an encoder:

0123     ///   <pre>

0124     ///     Base32HexEncoder encoder;

0125     ///     AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);

0126     ///     encoder.IsolatedInitialize(params);</pre>

0127     void IsolatedInitialize(const NameValuePairs &parameters);
0128 };
0129 
0130 /// \brief Base32 decodes data using extended hex

0131 /// \details Converts data from base32 using extended hex alphabet. The alphabet is different than Base32Decoder.

0132 /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder, <A HREF="http://tools.ietf.org/html/rfc4648#page-10">RFC 4648, Base 32 Encoding with Extended Hex Alphabet</A>.

0133 /// \since Crypto++ 6.0

0134 class Base32HexDecoder : public BaseN_Decoder
0135 {
0136 public:
0137     /// \brief Construct a Base32HexDecoder

0138     /// \param attachment a BufferedTrasformation to attach to this object

0139     /// \sa Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder

0140     Base32HexDecoder(BufferedTransformation *attachment = NULLPTR)
0141         : BaseN_Decoder(GetDefaultDecodingLookupArray(), 5, attachment) {}
0142 
0143     /// \brief Initialize or reinitialize this object, without signal propagation

0144     /// \param parameters a set of NameValuePairs used to initialize this object

0145     /// \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable

0146     ///   number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached

0147     ///   transformations. If initialization should be propagated, then use the Initialize() function.

0148     void IsolatedInitialize(const NameValuePairs &parameters);
0149 
0150 private:
0151     /// \brief Provides the default decoding lookup table

0152     /// \return default decoding lookup table

0153     static const int * CRYPTOPP_API GetDefaultDecodingLookupArray();
0154 };
0155 
0156 NAMESPACE_END
0157 
0158 #endif