kgg-dec/third-party/md5/md5.h

47 lines
1.3 KiB
C
Raw Permalink Normal View History

#pragma once
// Derived from the "RSA Data Security, Inc. MD5 Message-Digest Algorithm":
// src: https://github.com/freebsd/freebsd-src/blob/release/14.2.0/sys/kern/md5c.c
#include <cstdint>
#define MD5_BLOCK_LENGTH 64
#define MD5_DIGEST_LENGTH 16
#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)
/* MD5 context. */
struct MD5_CTX {
uint64_t count; /* number of bits, modulo 2^64 (lsb first) */
uint32_t state[4]; /* state (ABCD) */
unsigned char buffer[64]; /* input buffer */
};
/* MD5 initialization. Begins an MD5 operation, writing a new context. */
inline void md5_init(MD5_CTX* context) {
context->count = 0;
/* Load magic initialization constants. */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
}
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) {
MD5_CTX ctx;
md5_init(&ctx);
md5_update(&ctx, in, len);
md5_final(&ctx, digest);
}
inline void md5(uint8_t* digest, const uint8_t* in, const size_t len, const uint8_t* in2, size_t len2) {
MD5_CTX ctx;
md5_init(&ctx);
md5_update(&ctx, in, len);
md5_update(&ctx, in2, len2);
md5_final(&ctx, digest);
}