forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash.cpp
56 lines (46 loc) · 973 Bytes
/
Hash.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
#include "Hash.h"
#include "octetStream.h"
void hash_update(Hash *ctx, const void *data, unsigned long len)
{
ctx->update(data, len);
}
Hash::Hash()
{
// deal with alignment issues
int error = posix_memalign((void**) &state, 64,
sizeof(crypto_generichash_state));
if (error)
throw runtime_error(
string("failed to allocate hash state: ") + strerror(error));
reset();
}
Hash::~Hash()
{
free(state);
}
void Hash::reset()
{
crypto_generichash_init(state, 0, 0, crypto_generichash_BYTES);
size = 0;
}
void Hash::update(const octetStream& os)
{
update(os.get_data(), os.get_length());
}
void Hash::update(const string& str)
{
update(str.data(), str.size());
}
void Hash::final(octetStream& os)
{
os.resize_precise(hash_length);
os.reset_write_head();
final(os.append(hash_length));
reset();
}
octetStream Hash::final()
{
octetStream res;
final(res);
return res;
}