DDNet documentation
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1#ifndef BASE_HASH_H
2#define BASE_HASH_H
3
4#include <cstddef> // size_t
5
6static constexpr size_t SHA256_DIGEST_LENGTH = 256 / 8;
7static constexpr size_t SHA256_MAXSTRSIZE = 2 * SHA256_DIGEST_LENGTH + 1;
8static constexpr size_t MD5_DIGEST_LENGTH = 128 / 8;
9static constexpr size_t MD5_MAXSTRSIZE = 2 * MD5_DIGEST_LENGTH + 1;
10
12{
13 unsigned char data[SHA256_DIGEST_LENGTH];
14};
15
17{
18 unsigned char data[MD5_DIGEST_LENGTH];
19};
20
21SHA256_DIGEST sha256(const void *message, size_t message_len);
22void sha256_str(SHA256_DIGEST digest, char *str, size_t max_len);
23int sha256_from_str(SHA256_DIGEST *out, const char *str);
24int sha256_comp(SHA256_DIGEST digest1, SHA256_DIGEST digest2);
25
26MD5_DIGEST md5(const void *message, size_t message_len);
27void md5_str(MD5_DIGEST digest, char *str, size_t max_len);
28int md5_from_str(MD5_DIGEST *out, const char *str);
29int md5_comp(MD5_DIGEST digest1, MD5_DIGEST digest2);
30
31inline bool operator==(const SHA256_DIGEST &that, const SHA256_DIGEST &other)
32{
33 return sha256_comp(that, other) == 0;
34}
35inline bool operator!=(const SHA256_DIGEST &that, const SHA256_DIGEST &other)
36{
37 return !(that == other);
38}
39inline bool operator==(const MD5_DIGEST &that, const MD5_DIGEST &other)
40{
41 return md5_comp(that, other) == 0;
42}
43inline bool operator!=(const MD5_DIGEST &that, const MD5_DIGEST &other)
44{
45 return !(that == other);
46}
47
48#endif // BASE_HASH_H
MD5_DIGEST md5(const void *message, size_t message_len)
Definition hash.cpp:53
bool operator==(const SHA256_DIGEST &that, const SHA256_DIGEST &other)
Definition hash.h:31
static constexpr size_t MD5_MAXSTRSIZE
Definition hash.h:9
int sha256_from_str(SHA256_DIGEST *out, const char *str)
Definition hash.cpp:43
int md5_comp(MD5_DIGEST digest1, MD5_DIGEST digest2)
Definition hash.cpp:71
static constexpr size_t SHA256_DIGEST_LENGTH
Definition hash.h:6
SHA256_DIGEST sha256(const void *message, size_t message_len)
Definition hash.cpp:30
bool operator!=(const SHA256_DIGEST &that, const SHA256_DIGEST &other)
Definition hash.h:35
void sha256_str(SHA256_DIGEST digest, char *str, size_t max_len)
Definition hash.cpp:38
int md5_from_str(MD5_DIGEST *out, const char *str)
Definition hash.cpp:66
static constexpr size_t SHA256_MAXSTRSIZE
Definition hash.h:7
void md5_str(MD5_DIGEST digest, char *str, size_t max_len)
Definition hash.cpp:61
int sha256_comp(SHA256_DIGEST digest1, SHA256_DIGEST digest2)
Definition hash.cpp:48
static constexpr size_t MD5_DIGEST_LENGTH
Definition hash.h:8
Definition hash.h:17
unsigned char data[MD5_DIGEST_LENGTH]
Definition hash.h:18
Definition hash.h:12
unsigned char data[SHA256_DIGEST_LENGTH]
Definition hash.h:13