This commit is contained in:
Shengliang Guan 2022-02-28 14:07:11 +08:00
parent 17dfbffb41
commit 9735dcf9f8
2 changed files with 22 additions and 12 deletions

View File

@ -25,6 +25,12 @@
#ifndef _TD_UTIL_MD5_H #ifndef _TD_UTIL_MD5_H
#define _TD_UTIL_MD5_H #define _TD_UTIL_MD5_H
#include "os.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct { typedef struct {
uint32_t i[2]; /* number of _bits_ handled mod 2^64 */ uint32_t i[2]; /* number of _bits_ handled mod 2^64 */
uint32_t buf[4]; /* scratch buffer */ uint32_t buf[4]; /* scratch buffer */
@ -33,7 +39,11 @@ typedef struct {
} T_MD5_CTX; } T_MD5_CTX;
void tMD5Init(T_MD5_CTX *mdContext); void tMD5Init(T_MD5_CTX *mdContext);
void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, unsigned int inLen); void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, uint32_t inLen);
void tMD5Final(T_MD5_CTX *mdContext); void tMD5Final(T_MD5_CTX *mdContext);
#ifdef __cplusplus
}
#endif
#endif /*_TD_UTIL_MD5_H*/ #endif /*_TD_UTIL_MD5_H*/

View File

@ -33,7 +33,7 @@
*********************************************************************** ***********************************************************************
*/ */
#include "os.h" #define _DEFAULT_SOURCE
#include "tmd5.h" #include "tmd5.h"
/* forward declaration */ /* forward declaration */
@ -98,13 +98,13 @@ void tMD5Init(T_MD5_CTX *mdContext) {
account for the presence of each of the characters inBuf[0..inLen-1] account for the presence of each of the characters inBuf[0..inLen-1]
in the message whose digest is being computed. in the message whose digest is being computed.
*/ */
void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, unsigned int inLen) { void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, uint32_t inLen) {
uint32_t in[16]; uint32_t in[16];
int mdi; uint32_t mdi;
unsigned int i, ii; uint32_t i, ii;
/* compute number of bytes mod 64 */ /* compute number of bytes mod 64 */
mdi = (int)((mdContext->i[0] >> 3) & 0x3F); mdi = (uint32_t)((mdContext->i[0] >> 3) & 0x3F);
/* update number of bits */ /* update number of bits */
if ((mdContext->i[0] + ((uint32_t)inLen << 3)) < mdContext->i[0]) mdContext->i[1]++; if ((mdContext->i[0] + ((uint32_t)inLen << 3)) < mdContext->i[0]) mdContext->i[1]++;
@ -130,17 +130,17 @@ void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, unsigned int inLen) {
ends with the desired message digest in mdContext->digest[0...15]. ends with the desired message digest in mdContext->digest[0...15].
*/ */
void tMD5Final(T_MD5_CTX *mdContext) { void tMD5Final(T_MD5_CTX *mdContext) {
uint32_t in[16]; uint32_t in[16];
int mdi; uint32_t mdi;
unsigned int i, ii; uint32_t i, ii;
unsigned int padLen; uint32_t padLen;
/* save number of bits */ /* save number of bits */
in[14] = mdContext->i[0]; in[14] = mdContext->i[0];
in[15] = mdContext->i[1]; in[15] = mdContext->i[1];
/* compute number of bytes mod 64 */ /* compute number of bytes mod 64 */
mdi = (int)((mdContext->i[0] >> 3) & 0x3F); mdi = (uint32_t)((mdContext->i[0] >> 3) & 0x3F);
/* pad out to 56 mod 64 */ /* pad out to 56 mod 64 */
padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi); padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);