Crypto++ 8.0 Free C++ class library of cryptographic schemes

base32.cpp
1 // base32.cpp - written and placed in the public domain by Frank Palazzolo, based on hex.cpp by Wei Dai
2 // extended hex alphabet added by JW in November, 2017.
3
4 #include "pch.h"
5 #include "base32.h"
6
7 NAMESPACE_BEGIN(CryptoPP)
8 ANONYMOUS_NAMESPACE_BEGIN
9
10 const byte s_stdUpper[] = "ABCDEFGHIJKMNPQRSTUVWXYZ23456789";
11 const byte s_stdLower[] = "abcdefghijkmnpqrstuvwxyz23456789";
12 const byte s_hexUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
13 const byte s_hexLower[] = "0123456789abcdefghijklmnopqrstuv";
14
15 const int s_array[256] = {
16 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
17 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
18 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
19 -1, -1, 24, 25, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1,
20 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, 11, 12, -1,
21 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, -1, -1, -1, -1, -1,
22 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, 11, 12, -1,
23 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, -1, -1, -1, -1, -1,
24 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
25 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
26 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
27 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
32 };
33
34 const int s_hexArray[256] = {
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
39 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
40 25, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
42 25, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
51 };
52
53 ANONYMOUS_NAMESPACE_END
54
55 void Base32Encoder::IsolatedInitialize(const NameValuePairs &parameters)
56 {
57 bool uppercase = parameters.GetValueWithDefault(Name::Uppercase(), true);
58 m_filter->Initialize(CombinedNameValuePairs(
59 parameters,
60 MakeParameters(Name::EncodingLookupArray(), uppercase ? &s_stdUpper[0] : &s_stdLower[0], false)(Name::Log2Base(), 5, true)));
61 }
62
63 void Base32Decoder::IsolatedInitialize(const NameValuePairs &parameters)
64 {
65 BaseN_Decoder::IsolatedInitialize(CombinedNameValuePairs(
66 parameters,
67 MakeParameters(Name::DecodingLookupArray(), GetDefaultDecodingLookupArray(), false)(Name::Log2Base(), 5, true)));
68 }
69
70 // Unrolled initialization, http://github.com/weidai11/cryptopp/issues/376
71 const int *Base32Decoder::GetDefaultDecodingLookupArray()
72 {
73 return s_array;
74 }
75
76 void Base32HexEncoder::IsolatedInitialize(const NameValuePairs &parameters)
77 {
78 bool uppercase = parameters.GetValueWithDefault(Name::Uppercase(), true);
79 m_filter->Initialize(CombinedNameValuePairs(
80 parameters,
81 MakeParameters(Name::EncodingLookupArray(), uppercase ? &s_hexUpper[0] : &s_hexLower[0], false)(Name::Log2Base(), 5, true)));
82 }
83
84 void Base32HexDecoder::IsolatedInitialize(const NameValuePairs &parameters)
85 {
86 BaseN_Decoder::IsolatedInitialize(CombinedNameValuePairs(
87 parameters,
88 MakeParameters(Name::DecodingLookupArray(), GetDefaultDecodingLookupArray(), false)(Name::Log2Base(), 5, true)));
89 }
90
91 // Unrolled initialization, http://github.com/weidai11/cryptopp/issues/376
92 const int *Base32HexDecoder::GetDefaultDecodingLookupArray()
93 {
94 return s_hexArray;
95 }
96
97 NAMESPACE_END
Name::Uppercase
const char * Uppercase()
bool
Definition: argnames.h:70
Base32HexEncoder::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:76
NameValuePairs::GetValueWithDefault
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:363
Base32HexDecoder::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:84
CombinedNameValuePairs
Combines two sets of NameValuePairs.
Definition: algparam.h:124
base32.h
Classes for Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder.
BaseN_Decoder::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: basecode.cpp:115
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:502
Name::EncodingLookupArray
const char * EncodingLookupArray()
const byte *
Definition: argnames.h:75
pch.h
Precompiled header file.
Name::Log2Base
const char * Log2Base()
int
Definition: argnames.h:74
Base32Decoder::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:63
Name::DecodingLookupArray
const char * DecodingLookupArray()
const byte *
Definition: argnames.h:76
Base32Encoder::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base32.cpp:55
CryptoPP
Crypto++ library namespace.
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:293