|
2 | 2 | #include <trantor/utils/Utilities.h> |
3 | 3 |
|
4 | 4 | #include <cassert> |
| 5 | +#include <string_view> |
5 | 6 |
|
6 | 7 | #include "md5.h" |
7 | 8 | #include "sha1.h" |
8 | 9 | #include "sha3.h" |
9 | 10 | #include "blake2.h" |
10 | 11 |
|
| 12 | +template <typename Hash> |
| 13 | +inline bool attemptHash(const std::string_view& name, |
| 14 | + Hash& hash, |
| 15 | + const void* data, |
| 16 | + size_t len) |
| 17 | +{ |
| 18 | + auto hashFunction = Botan::HashFunction::create(name); |
| 19 | + if (hashFunction == nullptr) |
| 20 | + return false; |
| 21 | + |
| 22 | + hashFunction->update((const unsigned char*)data, len); |
| 23 | + hashFunction->final((unsigned char*)&hash); |
| 24 | + return true; |
| 25 | +} |
| 26 | + |
11 | 27 | namespace trantor |
12 | 28 | { |
13 | 29 | namespace utils |
14 | 30 | { |
15 | 31 | Hash128 md5(const void* data, size_t len) |
16 | 32 | { |
17 | 33 | Hash128 hash; |
18 | | - auto md5 = Botan::HashFunction::create("MD5"); |
19 | | - if (md5 == nullptr) |
20 | | - { |
21 | | - MD5_CTX ctx; |
22 | | - trantor_md5_init(&ctx); |
23 | | - trantor_md5_update(&ctx, (const unsigned char*)data, len); |
24 | | - trantor_md5_final(&ctx, (unsigned char*)&hash); |
| 34 | + if (attemptHash("MD5", hash, data, len)) |
25 | 35 | return hash; |
26 | | - } |
27 | 36 |
|
28 | | - md5->update((const unsigned char*)data, len); |
29 | | - md5->final((unsigned char*)&hash); |
| 37 | + MD5_CTX ctx; |
| 38 | + trantor_md5_init(&ctx); |
| 39 | + trantor_md5_update(&ctx, (const unsigned char*)data, len); |
| 40 | + trantor_md5_final(&ctx, (unsigned char*)&hash); |
30 | 41 | return hash; |
31 | 42 | } |
32 | 43 |
|
33 | 44 | Hash160 sha1(const void* data, size_t len) |
34 | 45 | { |
35 | 46 | Hash160 hash; |
36 | | - auto sha1 = Botan::HashFunction::create("SHA-1"); |
37 | | - if (sha1 == nullptr) |
38 | | - { |
39 | | - SHA1_CTX ctx; |
40 | | - TrantorSHA1Init(&ctx); |
41 | | - TrantorSHA1Update(&ctx, (const unsigned char*)data, len); |
42 | | - TrantorSHA1Final((unsigned char*)&hash, &ctx); |
| 47 | + if (attemptHash("SHA-1", hash, data, len)) |
43 | 48 | return hash; |
44 | | - } |
45 | | - sha1->update((const unsigned char*)data, len); |
46 | | - sha1->final((unsigned char*)&hash); |
| 49 | + |
| 50 | + SHA1_CTX ctx; |
| 51 | + TrantorSHA1Init(&ctx); |
| 52 | + TrantorSHA1Update(&ctx, (const unsigned char*)data, len); |
| 53 | + TrantorSHA1Final((unsigned char*)&hash, &ctx); |
47 | 54 | return hash; |
48 | 55 | } |
49 | 56 |
|
50 | 57 | Hash256 sha256(const void* data, size_t len) |
51 | 58 | { |
52 | 59 | Hash256 hash; |
53 | | - auto sha256 = Botan::HashFunction::create("SHA-256"); |
54 | | - assert(sha256 != nullptr); // Botan guarantees that SHA-256 is available |
55 | | - sha256->update((const unsigned char*)data, len); |
56 | | - sha256->final((unsigned char*)&hash); |
| 60 | + // Botan should guarantees that SHA-256 is available |
| 61 | + bool ok = attemptHash("SHA-256", hash, data, len); |
| 62 | + if (!ok) |
| 63 | + assert(false); |
57 | 64 | return hash; |
58 | 65 | } |
59 | 66 |
|
60 | 67 | Hash256 sha3(const void* data, size_t len) |
61 | 68 | { |
62 | 69 | Hash256 hash; |
63 | | - auto sha3 = Botan::HashFunction::create("SHA-3(256)"); |
64 | | - if (sha3 == nullptr) |
65 | | - { |
66 | | - trantor_sha3((const unsigned char*)data, len, &hash, sizeof(hash)); |
| 70 | + if (attemptHash("SHA-3(256)", hash, data, len)) |
67 | 71 | return hash; |
68 | | - } |
69 | | - assert(sha3 != nullptr); |
70 | | - sha3->update((const unsigned char*)data, len); |
71 | | - sha3->final((unsigned char*)&hash); |
| 72 | + |
| 73 | + trantor_sha3((const unsigned char*)data, len, &hash, sizeof(hash)); |
72 | 74 | return hash; |
73 | 75 | } |
74 | 76 |
|
75 | 77 | Hash256 blake2b(const void* data, size_t len) |
76 | 78 | { |
77 | 79 | Hash256 hash; |
78 | | - auto blake2b = Botan::HashFunction::create("BLAKE2b(256)"); |
79 | | - if (blake2b == nullptr) |
80 | | - { |
81 | | - trantor_blake2b(&hash, sizeof(hash), data, len, NULL, 0); |
| 80 | + if (attemptHash("BLAKE2b(256)", hash, data, len)) |
82 | 81 | return hash; |
83 | | - } |
84 | | - blake2b->update((const unsigned char*)data, len); |
85 | | - blake2b->final((unsigned char*)&hash); |
| 82 | + auto blake2b = Botan::HashFunction::create("BLAKE2b(256)"); |
| 83 | + |
| 84 | + trantor_blake2b(&hash, sizeof(hash), data, len, NULL, 0); |
86 | 85 | return hash; |
87 | 86 | } |
88 | 87 |
|
|
0 commit comments