/
memoryaccess.cpp
61 lines (51 loc) · 1.24 KB
/
memoryaccess.cpp
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
#include "memoryaccess.h"
#include "utils.h"
#include <EEPROM.h>
void readHash(char* hash) {
for( int i = 0; i < HASH_SIZE; ++i ) {
hash[i] = EEPROM.read(i + START_HASH);
}
}
void writeHash(char* hash) {
for ( int i = 0; i < HASH_SIZE; ++i ) {
EEPROM.write(i + START_HASH, hash[i]);
}
}
void readLastTimeUsed(char* lastTimeUsed) {
for (int i = 0; i < LAST_TIME_USED_SIZE; ++i) {
lastTimeUsed[i] = EEPROM.read(i + START_LAST_TIME_USED);
}
}
void writeLastTimeUsed(char* lastTimeUsed) {
for (int i = 0; i < LAST_TIME_USED_SIZE; ++i) {
if (lastTimeUsed[i] != '\0')
EEPROM.write(i + START_LAST_TIME_USED, lastTimeUsed[i]);
else
EEPROM.write(i + START_LAST_TIME_USED, '\t');
}
}
void swap(char* a, char* b) {
char t = *a;
*a = *b;
*b = t;
}
void deshuffle(char* message, byte l, long n) {
int i = l-1;
while ( i > 0) {
if (n & 0x40000000) { //the SIZE bit is set
swap(&message[0], &message[i]);
}
--i;
n <<= 1;
}
}
void readKey(char* key, byte l) {
for ( int i = 0; i < l; ++i ) {
key[i] = EEPROM.read(i + START_KEY);
}
}
void readKey(char* key, byte keySize, const char* salt) {
long value = getLong(salt);
readKey(key, keySize);
deshuffle(key, keySize, value);
}