forked from nasdf/godot-web3-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keccak.cpp
38 lines (31 loc) · 974 Bytes
/
keccak.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
#include "keccak.h"
#include "thirdparty/trezor-crypto/sha3.h"
void Keccak::hash(const unsigned char* p_data, int p_len, unsigned char r_digest[32]) {
keccak_256(p_data, p_len, r_digest);
}
Error Keccak::update(PackedByteArray p_chunk) {
ERR_FAIL_COND_V(ctx == nullptr, ERR_UNCONFIGURED);
size_t len = p_chunk.size();
ERR_FAIL_COND_V(len == 0, FAILED);
PackedByteArray r = p_chunk;
keccak_Update((SHA3_CTX *)ctx, &r[0], len);
return OK;
}
PackedByteArray Keccak::finish() {
ERR_FAIL_COND_V(ctx == nullptr, PackedByteArray());
PackedByteArray out;
out.resize(32);
keccak_Final((SHA3_CTX *)ctx, (unsigned char*) out.ptr());
return out;
}
void Keccak::_bind_methods() {
ClassDB::bind_method(D_METHOD("update", "chunk"), &Keccak::update);
ClassDB::bind_method(D_METHOD("finish"), &Keccak::finish);
}
Keccak::Keccak() {
ctx = memalloc(sizeof(SHA3_CTX));
keccak_256_Init((SHA3_CTX *)ctx);
}
Keccak::~Keccak() {
memfree((SHA3_CTX *)ctx);
}