2024-12-20 02:03:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2024-12-20 21:02:40 +00:00
|
|
|
#if USE_WIN_CRYPTO
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <wincrypt.h>
|
|
|
|
#endif
|
2024-12-20 02:03:16 +00:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#define MD5_BLOCK_LENGTH 64
|
|
|
|
#define MD5_DIGEST_LENGTH 16
|
|
|
|
#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)
|
|
|
|
|
|
|
|
struct MD5_CTX {
|
2024-12-20 21:02:40 +00:00
|
|
|
#if USE_WIN_CRYPTO
|
|
|
|
HCRYPTPROV hProv;
|
|
|
|
HCRYPTHASH hHash;
|
|
|
|
#else
|
2024-12-20 02:03:16 +00:00
|
|
|
uint64_t count; /* number of bits, modulo 2^64 (lsb first) */
|
|
|
|
uint32_t state[4]; /* state (ABCD) */
|
|
|
|
unsigned char buffer[64]; /* input buffer */
|
2024-12-20 21:02:40 +00:00
|
|
|
#endif
|
2024-12-20 02:03:16 +00:00
|
|
|
};
|
|
|
|
|
2024-12-20 21:02:40 +00:00
|
|
|
#if USE_WIN_CRYPTO
|
|
|
|
bool md5_init(MD5_CTX* context);
|
|
|
|
bool md5_cleanup(MD5_CTX* ctx);
|
|
|
|
#else
|
2024-12-20 02:03:16 +00:00
|
|
|
/* MD5 initialization. Begins an MD5 operation, writing a new context. */
|
2024-12-20 21:02:40 +00:00
|
|
|
inline bool md5_init(MD5_CTX* context) {
|
2024-12-20 02:03:16 +00:00
|
|
|
context->count = 0;
|
|
|
|
|
|
|
|
/* Load magic initialization constants. */
|
|
|
|
context->state[0] = 0x67452301;
|
|
|
|
context->state[1] = 0xefcdab89;
|
|
|
|
context->state[2] = 0x98badcfe;
|
|
|
|
context->state[3] = 0x10325476;
|
2024-12-20 21:02:40 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool md5_cleanup(MD5_CTX* ctx) {
|
|
|
|
return true;
|
2024-12-20 02:03:16 +00:00
|
|
|
}
|
2024-12-20 21:02:40 +00:00
|
|
|
#endif
|
2024-12-20 02:03:16 +00:00
|
|
|
|
|
|
|
void md5_update(MD5_CTX* ctx, const uint8_t* in, size_t len);
|
|
|
|
void md5_final(MD5_CTX* ctx, uint8_t* digest);
|
|
|
|
|
|
|
|
inline void md5(uint8_t* digest, const uint8_t* in, const size_t len) {
|
2024-12-20 21:02:40 +00:00
|
|
|
MD5_CTX ctx{};
|
2024-12-20 02:03:16 +00:00
|
|
|
md5_init(&ctx);
|
|
|
|
md5_update(&ctx, in, len);
|
|
|
|
md5_final(&ctx, digest);
|
2024-12-20 21:02:40 +00:00
|
|
|
md5_cleanup(&ctx);
|
2024-12-20 02:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void md5(uint8_t* digest, const uint8_t* in, const size_t len, const uint8_t* in2, size_t len2) {
|
2024-12-20 21:02:40 +00:00
|
|
|
MD5_CTX ctx{};
|
2024-12-20 02:03:16 +00:00
|
|
|
md5_init(&ctx);
|
|
|
|
md5_update(&ctx, in, len);
|
|
|
|
md5_update(&ctx, in2, len2);
|
|
|
|
md5_final(&ctx, digest);
|
2024-12-20 21:02:40 +00:00
|
|
|
md5_cleanup(&ctx);
|
2024-12-20 02:03:16 +00:00
|
|
|
}
|