1 | #ifndef BASE_HASH_H |
2 | #define BASE_HASH_H |
3 | |
4 | #include <cstddef> // size_t |
5 | |
6 | enum |
7 | { |
8 | SHA256_DIGEST_LENGTH = 256 / 8, |
9 | SHA256_MAXSTRSIZE = 2 * SHA256_DIGEST_LENGTH + 1, |
10 | MD5_DIGEST_LENGTH = 128 / 8, |
11 | MD5_MAXSTRSIZE = 2 * MD5_DIGEST_LENGTH + 1, |
12 | }; |
13 | |
14 | struct SHA256_DIGEST |
15 | { |
16 | unsigned char data[SHA256_DIGEST_LENGTH]; |
17 | }; |
18 | |
19 | struct MD5_DIGEST |
20 | { |
21 | unsigned char data[MD5_DIGEST_LENGTH]; |
22 | }; |
23 | |
24 | SHA256_DIGEST sha256(const void *message, size_t message_len); |
25 | void sha256_str(SHA256_DIGEST digest, char *str, size_t max_len); |
26 | int sha256_from_str(SHA256_DIGEST *out, const char *str); |
27 | int sha256_comp(SHA256_DIGEST digest1, SHA256_DIGEST digest2); |
28 | |
29 | MD5_DIGEST md5(const void *message, size_t message_len); |
30 | void md5_str(MD5_DIGEST digest, char *str, size_t max_len); |
31 | int md5_from_str(MD5_DIGEST *out, const char *str); |
32 | int md5_comp(MD5_DIGEST digest1, MD5_DIGEST digest2); |
33 | |
34 | extern const SHA256_DIGEST SHA256_ZEROED; |
35 | |
36 | inline bool operator==(const SHA256_DIGEST &that, const SHA256_DIGEST &other) |
37 | { |
38 | return sha256_comp(digest1: that, digest2: other) == 0; |
39 | } |
40 | inline bool operator!=(const SHA256_DIGEST &that, const SHA256_DIGEST &other) |
41 | { |
42 | return !(that == other); |
43 | } |
44 | inline bool operator==(const MD5_DIGEST &that, const MD5_DIGEST &other) |
45 | { |
46 | return md5_comp(digest1: that, digest2: other) == 0; |
47 | } |
48 | inline bool operator!=(const MD5_DIGEST &that, const MD5_DIGEST &other) |
49 | { |
50 | return !(that == other); |
51 | } |
52 | |
53 | #endif // BASE_HASH_H |
54 | |