There's already a Base32 coder in CVS. By default it uses the alphabet
from draft-ietf-idn-dude-02.txt (which is different from what you use),
but you can pass in your own alphabet to it as a parameter.

On Fri, Mar 19, 2004 at 12:40:50PM +0000, Lists wrote:
> Sorry, I've never had any luck with CVS to contribute stuff.  This is a modified 
> version of the HEX encoder which encodes in base 32 without the characters people 
> most often confuse when written: ILO0.  It is suitable for when people need to enter 
> encrypted data by hand. Unlike Base64, it is case insensitive, and is 25% smaller 
> than hex encoding.  It is NOT the offical Base32 encoding, as that uses confusible 
> characters.  
> 
> If you are going to use this, please further format the data so that it is in blocks 
> of around 5 characters so that it is easier for people to enter correctly.
> 
> I hope someone finds this useful.
> 
> This is for Crypto++ Library 5.1.
> 
> 
> 
> #ifndef CRYPTOPP_BASE32H_H
> #define CRYPTOPP_BASE32H_H
> 
> #include "basecode.h"
> 
> NAMESPACE_BEGIN(CryptoPP)
> 
> //! Converts given data to base 32 using human readable case-insensitive characters 
> and numbers minus 
> //! ILO0 which are easily confusible in most fonts.  Suitable for human entry of 
> encrypted data.
> class Base32HumanEncoder : public SimpleProxyFilter
> {
> public:
>       Base32HumanEncoder(BufferedTransformation *attachment = NULL, bool uppercase = 
> true, int outputGroupSize = 0, const std::string &separator = ":", const std::string 
> &terminator = "")
>               : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
>       {
>               IsolatedInitialize(MakeParameters("Uppercase", uppercase)("GroupSize", 
> outputGroupSize)("Separator", ConstByteArrayParameter(separator)));
>       }
> 
>       void IsolatedInitialize(const NameValuePairs &parameters);
> };
> 
> //! Decode base 32 human readable data back to bytes
> class Base32HumanDecoder : public BaseN_Decoder
> {
> public:
>       Base32HumanDecoder(BufferedTransformation *attachment = NULL)
>               : BaseN_Decoder(GetDecodingLookupArray(), 5, attachment) {}
> 
>       void IsolatedInitialize(const NameValuePairs &parameters) {}
> 
> private:
>       static const int *GetDecodingLookupArray();
> };
> 
> NAMESPACE_END
> 
> #endif
> 
> 
> 
> 
> 
> // base32H.cpp - modified version of hex.cpp written and placed in the public domain 
> by Wei Dai
> // Modifications by Robert Basler
> 
> #include "pch.h"
> #include "base32H.h"
> 
> NAMESPACE_BEGIN(CryptoPP)
> 
> static const byte s_vecUpper[] = "ABCDEFGHJKMNPQRSTUVWXYZ123456789";
> static const byte s_vecLower[] = "abcdefghjkmnpqrstuvwxyz123456789";
> 
> void Base32HumanEncoder::IsolatedInitialize(const NameValuePairs &parameters)
> {
>       bool uppercase = parameters.GetValueWithDefault("Uppercase", true);
>       m_filter->Initialize(CombinedNameValuePairs(
>               parameters,
>               MakeParameters("EncodingLookupArray", uppercase ? &s_vecUpper[0] : 
> &s_vecLower[0])("Log2Base", 5)));
> }
> 
> const int *Base32HumanDecoder::GetDecodingLookupArray()
> {
>       static bool s_initialized = false;
>       static int s_array[256];
> 
>       if (!s_initialized)
>       {
>               InitializeDecodingLookupArray(s_array, s_vecUpper, 32, true);
>               s_initialized = true;
>       }
>       return s_array;
> }
> 
> NAMESPACE_END
> 

Reply via email to