Skip to content

Commit 354803a

Browse files
committed
cleanup botan codebase
1 parent fc2a704 commit 354803a

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

trantor/utils/crypto/botan.cc

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,86 @@
22
#include <trantor/utils/Utilities.h>
33

44
#include <cassert>
5+
#include <string_view>
56

67
#include "md5.h"
78
#include "sha1.h"
89
#include "sha3.h"
910
#include "blake2.h"
1011

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+
1127
namespace trantor
1228
{
1329
namespace utils
1430
{
1531
Hash128 md5(const void* data, size_t len)
1632
{
1733
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))
2535
return hash;
26-
}
2736

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);
3041
return hash;
3142
}
3243

3344
Hash160 sha1(const void* data, size_t len)
3445
{
3546
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))
4348
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);
4754
return hash;
4855
}
4956

5057
Hash256 sha256(const void* data, size_t len)
5158
{
5259
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);
5764
return hash;
5865
}
5966

6067
Hash256 sha3(const void* data, size_t len)
6168
{
6269
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))
6771
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));
7274
return hash;
7375
}
7476

7577
Hash256 blake2b(const void* data, size_t len)
7678
{
7779
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))
8281
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);
8685
return hash;
8786
}
8887

0 commit comments

Comments
 (0)