Crypto++ 8.9
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
7NAMESPACE_BEGIN(CryptoPP)
8ANONYMOUS_NAMESPACE_BEGIN
9
10const byte s_stdUpper[] = "ABCDEFGHIJKMNPQRSTUVWXYZ23456789";
11const byte s_stdLower[] = "abcdefghijkmnpqrstuvwxyz23456789";
12const byte s_hexUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
13const byte s_hexLower[] = "0123456789abcdefghijklmnopqrstuv";
14
15const 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
34const 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
53ANONYMOUS_NAMESPACE_END
54
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
64{
66 parameters,
67 MakeParameters(Name::DecodingLookupArray(), GetDefaultDecodingLookupArray(), false)(Name::Log2Base(), 5, true)));
68}
69
70// Unrolled initialization, http://github.com/weidai11/cryptopp/issues/376
71const int *Base32Decoder::GetDefaultDecodingLookupArray()
72{
73 return s_array;
74}
75
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
85{
87 parameters,
88 MakeParameters(Name::DecodingLookupArray(), GetDefaultDecodingLookupArray(), false)(Name::Log2Base(), 5, true)));
89}
90
91// Unrolled initialization, http://github.com/weidai11/cryptopp/issues/376
92const int *Base32HexDecoder::GetDefaultDecodingLookupArray()
93{
94 return s_hexArray;
95}
96
97NAMESPACE_END
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition algparam.h:508
Classes for Base32Encoder, Base32Decoder, Base32HexEncoder and Base32HexDecoder.
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition base32.cpp:63
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition base32.cpp:55
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition base32.cpp:84
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition base32.cpp:76
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Combines two sets of NameValuePairs.
Definition algparam.h:129
Interface for retrieving values given their names.
Definition cryptlib.h:327
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition cryptlib.h:397
Crypto++ library namespace.
const char * DecodingLookupArray()
const byte *
Definition argnames.h:76
const char * EncodingLookupArray()
const byte *
Definition argnames.h:75
const char * Uppercase()
bool
Definition argnames.h:70
const char * Log2Base()
int
Definition argnames.h:74
Precompiled header file.