forked from Novators/libsqrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrlBase56Check.cpp
More file actions
104 lines (94 loc) · 2.66 KB
/
SqrlBase56Check.cpp
File metadata and controls
104 lines (94 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/** \file SqrlBase56Check.cpp
*
* \author Adam Comley
*
* This file is part of libsqrl. It is released under the MIT license.
* For more details, see the LICENSE file included with this package.
**/
#include "sqrl_internal.h"
#include "SqrlBase56Check.h"
#include "SqrlBase56.h"
#include "SqrlBigInt.h"
namespace libsqrl
{
SqrlBase56Check::SqrlBase56Check() : SqrlBase56() {}
SqrlString *SqrlBase56Check::encode( SqrlString *dest, const SqrlString *src, bool append ) {
if( !src || src->length() == 0 ) return NULL;
if( !dest ) {
dest = new SqrlString();
}
if( !append ) {
dest->clear();
}
SqrlString encoded = SqrlString();
if( !this->SqrlBase56::encode( &encoded, src ) ) {
return NULL;
}
uint8_t lineCount = 0;
SqrlString line = SqrlString( 20 );
SqrlBigInt sha = SqrlBigInt( 32 );
while( encoded.substring( &line, lineCount * 19, 19 ) ) {
line.append( lineCount, 1 );
sha.clear();
sha.append( (char)0, 32 );
crypto_hash_sha256( (unsigned char*)sha.data(), (unsigned char*)line.data(), line.length() );
uint8_t rem = sha.divideBy( 56 );
line.popc_back();
line.append( this->alphabet[rem], 1 );
dest->append( &line );
lineCount++;
}
return dest;
}
SqrlString *SqrlBase56Check::decode( SqrlString *dest, const SqrlString *src, bool append ) {
if( !src || src->length() == 0 ) return NULL;
SqrlString toDecode = SqrlString( src->length() );
if( this->preProcess( &toDecode, src, NULL ) ) {
bool didAlloc = false;
if( !dest ) {
dest = new SqrlString();
didAlloc = true;
}
if( this->SqrlBase56::decode( dest, &toDecode, append ) ) {
return dest;
} else {
if( didAlloc ) delete dest;
dest = NULL;
}
}
return dest;
}
bool SqrlBase56Check::validate( const SqrlString * src, size_t * error ) {
SqrlString b56;
return this->preProcess( &b56, src, error );
}
bool SqrlBase56Check::preProcess( SqrlString * base56, const SqrlString * src, size_t * error ) {
bool isError = false;
uint8_t lineCount = 0;
SqrlString line = SqrlString( 20 );
SqrlBigInt sha = SqrlBigInt( 32 );
while( src->substring( &line, lineCount * 20, 20 ) ) {
char checkChar = line.popc_back();
line.append( lineCount, 1 );
sha.clear();
sha.append( (char)0, 32 );
crypto_hash_sha256( (unsigned char*)sha.data(), (unsigned char*)line.data(), line.length() );
uint8_t rem = sha.divideBy( 56 );
if( checkChar != this->alphabet[rem] ) {
isError = true;
break;
}
line.popc_back();
base56->append( &line );
lineCount++;
}
if( isError ) {
printf( "Failed at line %d\n", lineCount );
if( error ) {
*error = lineCount * 20;
}
return false;
}
return true;
}
}