Files
2026-01-12 18:46:01 +08:00

87 lines
2.3 KiB
C

/**
* @file md5.h
*
* Copyright (c) 2021 Semidrive Semiconductor.
* All rights reserved.
*
* Description:compute the md5 of a data stream
*/
#ifndef __MD5_H
#define __MD5_H
#if defined(__cplusplus)
extern "C" {
#endif
#include <compiler.h>
#define MD5_LEN 16
struct MD5Context {
unsigned int buf[4];
unsigned int bits[2];
union {
unsigned char in[64];
unsigned int in32[16];
};
};
/**
* @brief Initialize the MD5 context.
*
* This function initializes the MD5 context by setting the initial values of
* the buffer and bit count. The buffer values are set to the predefined
* constants of the MD5 algorithm, and the bit count is set to zero.
*
* @param ctx Pointer to the MD5 context structure to be initialized.
*/
void MD5Init(struct MD5Context *ctx);
/**
* @brief Update the MD5 context with new data.
*
* This function updates the MD5 context with the given data buffer.
* It processes the data in chunks of 64 bytes, updating the context's bit count
* and calling the MD5Transform function as necessary.
*
* @param ctx Pointer to the MD5 context structure.
* @param buf Pointer to the input data buffer.
* @param len Length of the input data buffer in bytes.
*/
void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len);
/**
* @brief Finalize the MD5 hash computation.
*
* This function finalizes the MD5 hash computation by padding the remaining
* data, appending the bit count, and performing the final MD5 transformation.
* The resulting hash is stored in the provided digest buffer.
*
* @param digest Pointer to the buffer where the 16-byte MD5 digest will be
* stored.
* @param ctx Pointer to the MD5 context structure containing the current state
* of the hash computation.
*/
void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
/**
* @brief Calculate the MD5 hash of the input data.
*
* This function takes an input buffer and its length, and calculates the MD5
* hash of the data. The resulting 16-byte MD5 hash is stored in the output
* buffer.
*
* @param input Pointer to the input data buffer.
* @param len Length of the input data buffer in bytes.
* @param output Pointer to the buffer where the 16-byte MD5 hash will be
* stored.
*/
void md5(const unsigned char *input, const int len, unsigned char output[16]);
#if defined(__cplusplus)
}
#endif
#endif