forked from nayuki/Bitcoin-Cryptography-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSha256Hash.cpp
More file actions
45 lines (35 loc) · 1.01 KB
/
Copy pathSha256Hash.cpp
File metadata and controls
45 lines (35 loc) · 1.01 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
/*
* Bitcoin cryptography library
* Copyright (c) Project Nayuki
*
* https://www.nayuki.io/page/bitcoin-cryptography-library
* https://github.com/nayuki/Bitcoin-Cryptography-Library
*/
#include <cassert>
#include <cstring>
#include "Sha256Hash.hpp"
#include "Utils.hpp"
using std::uint8_t;
using std::size_t;
Sha256Hash::Sha256Hash(const uint8_t hash[HASH_LEN], size_t len) {
assert(hash != nullptr && len == HASH_LEN);
std::memcpy(value, hash, sizeof(value));
}
Sha256Hash::Sha256Hash(const char *str) :
value() {
assert(str != nullptr && std::strlen(str) == HASH_LEN * 2);
for (int i = 0; i < HASH_LEN * 2; i++) {
int digit = Utils::parseHexDigit(str[HASH_LEN * 2 - 1 - i]);
assert(digit != -1);
value[i >> 1] |= digit << ((i & 1) << 2);
}
}
bool Sha256Hash::operator==(const Sha256Hash &other) const {
int diff = 0;
for (int i = 0; i < HASH_LEN; i++)
diff |= value[i] ^ other.value[i];
return diff == 0;
}
bool Sha256Hash::operator!=(const Sha256Hash &other) const {
return !(*this == other);
}