-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSha256.hpp
66 lines (55 loc) · 1.42 KB
/
Sha256.hpp
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
#pragma once
#include <nstd/Memory.hpp>
class Sha256
{
public:
static const usize blockSize = 64;
static const usize digestSize = 32;
Sha256() {reset();}
void reset();
void update(const byte* data, usize size);
void finalize(byte (&digest)[digestSize]);
static void hash(const byte* data, usize size, byte (&result)[digestSize])
{
Sha256 sha256;
sha256.update(data, size);
sha256.finalize(result);
}
static void hmac(const byte* key, usize keySize, const byte* message, usize messageSize, byte (&result)[digestSize])
{
Sha256 sha256;
byte hashKey[blockSize];
if(keySize > blockSize)
{
sha256.update(key, keySize);
sha256.finalize((byte (&)[digestSize])hashKey);
Memory::zero(hashKey + 32, 32);
}
else
{
Memory::copy(hashKey, key, keySize);
if(keySize < blockSize)
Memory::zero(hashKey + keySize, blockSize - keySize);
}
byte oKeyPad[blockSize];
byte iKeyPad[blockSize];
for(int i = 0; i < 64; ++i)
{
oKeyPad[i] = hashKey[i] ^ 0x5c;
iKeyPad[i] = hashKey[i] ^ 0x36;
}
byte hash[digestSize];
sha256.update(iKeyPad, blockSize);
sha256.update(message, messageSize);
sha256.finalize(hash);
sha256.update(oKeyPad, blockSize);
sha256.update(hash, digestSize);
sha256.finalize(result);
}
private:
uint32 state[8];
uint64 count;
byte buffer[64];
private:
class Private;
};