forked from Novators/libsqrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrlKeySet.cpp
More file actions
59 lines (51 loc) · 1.86 KB
/
SqrlKeySet.cpp
File metadata and controls
59 lines (51 loc) · 1.86 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
/** \file SqrlKeySet.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 "SqrlKeySet.h"
#include <new>
// Total data size is 4096 bytes.
#define KEY_SET_SIZE 4096
// Password can be up to 1024 bytes.
#define PW_LENGTH 1024
// Values are 8 byte aligned.
#define ALIGN 8
namespace libsqrl
{
SqrlKeySet::SqrlKeySet() {
this->myData = (uint8_t*)sqrl_malloc( KEY_SET_SIZE );
uint8_t *ptr = this->myData;
uint8_t classSize = sizeof( class SqrlFixedString );
if( classSize % ALIGN ) classSize += (ALIGN - (classSize % ALIGN));
size_t slotSize = classSize + SQRL_KEY_SIZE + 1;
if( slotSize % ALIGN ) slotSize += (ALIGN - (slotSize % ALIGN));
this->mySlotSize = (uint8_t)slotSize;
for( int keyCount = 0; keyCount < SQRL_KEY_PASSWORD; keyCount++ ) {
new (ptr) SqrlFixedString( SQRL_KEY_SIZE, ptr + classSize, 0 );
ptr += slotSize;
}
slotSize = classSize + PW_LENGTH + 1;
if( slotSize % ALIGN ) slotSize += (ALIGN - (slotSize % ALIGN));
new (ptr) SqrlFixedString( PW_LENGTH, ptr + classSize, 0 );
ptr += slotSize;
this->myScratch = ptr;
slotSize = this->myData + KEY_SET_SIZE - ptr - classSize - 1;
new (ptr) SqrlFixedString( slotSize, ptr + classSize, 0 );
}
SqrlKeySet::~SqrlKeySet() {
sqrl_free( this->myData, KEY_SET_SIZE );
}
SqrlFixedString * SqrlKeySet::operator[] ( size_t keyType ) {
if( keyType < SQRL_KEY_SCRATCH ) {
return (SqrlFixedString*)(this->myData + (keyType * this->mySlotSize));
}
if( keyType == SQRL_KEY_SCRATCH ) {
return (SqrlFixedString*)(this->myScratch);
}
return NULL;
}
}