Files
BLV_C1F_Module/BasicCode/Drive/MQTT/utils_hmac.c
caocong 95916b9995 fix:修改UDP通讯中,取电变化上报机制
1、问题点:当RCU网络状态异常的情况下,网络还处于协商状态下,还未进入正常通讯环节时,取电变化不会进行判断。这会导致取电变化上报与实际产生取电状态时间点对不上。
2、将BLV_C1F_Module代码上传至Gitea,之前代码修改记录请查看 .\BasicCode\Readme.txt
2026-01-23 09:23:12 +08:00

163 lines
6.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "utils_hmac.h"
#include "utils_md5.h"
#include "utils_sha1.h"
#define KEY_IOPAD_SIZE 64
#define MD5_DIGEST_SIZE 16
#define SHA1_DIGEST_SIZE 20
//const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/*-------------------------------------------------*/
/*函数名hmacmd5编码 */
/*网上寻找的开源程序 */
/*-------------------------------------------------*/
void utils_hmac_md5(const char *msg, int msg_len, char *digest, const char *key, int key_len)
{
unsigned char k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
unsigned char k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
unsigned char out[MD5_DIGEST_SIZE];
int i;
iot_md5_context context;
if((NULL == msg) || (NULL == digest) || (NULL == key)) {
return;
}
if(key_len > KEY_IOPAD_SIZE) {
return;
}
/* start out by storing key in pads */
memset(k_ipad, 0, sizeof(k_ipad));
memset(k_opad, 0, sizeof(k_opad));
memcpy(k_ipad, key, key_len);
memcpy(k_opad, key, key_len);
/* XOR key with ipad and opad values */
for (i = 0; i < KEY_IOPAD_SIZE; i++) {
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
}
/* perform inner MD5 */
utils_md5_init(&context); /* init context for 1st pass */
utils_md5_starts(&context); /* setup context for 1st pass */
utils_md5_update(&context, k_ipad, KEY_IOPAD_SIZE); /* start with inner pad */
utils_md5_update(&context, (unsigned char *) msg, msg_len); /* then text of datagram */
utils_md5_finish(&context, out); /* finish up 1st pass */
/* perform outer MD5 */
utils_md5_init(&context); /* init context for 2nd pass */
utils_md5_starts(&context); /* setup context for 2nd pass */
utils_md5_update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
utils_md5_update(&context, out, MD5_DIGEST_SIZE); /* then results of 1st hash */
utils_md5_finish(&context, out); /* finish up 2nd pass */
for (i = 0; i < MD5_DIGEST_SIZE; ++i) {
digest[i * 2] = utils_hb2hex(out[i] >> 4);
digest[i * 2 + 1] = utils_hb2hex(out[i]);
}
}
/*-------------------------------------------------*/
/*函数名hmacsha1编码 */
/*网上寻找的开源程序 */
/*-------------------------------------------------*/
void utils_hmac_sha1(const char *msg, int msg_len, char *digest, const char *key, int key_len)
{
iot_sha1_context context;
unsigned char k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
unsigned char k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
unsigned char out[SHA1_DIGEST_SIZE];
int i;
if((NULL == msg) || (NULL == digest) || (NULL == key)) {
return;
}
if(key_len > KEY_IOPAD_SIZE) {
return;
}
/* start out by storing key in pads */
memset(k_ipad, 0, sizeof(k_ipad));
memset(k_opad, 0, sizeof(k_opad));
memcpy(k_ipad, key, key_len);
memcpy(k_opad, key, key_len);
/* XOR key with ipad and opad values */
for (i = 0; i < KEY_IOPAD_SIZE; i++) {
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
}
/* perform inner SHA */
utils_sha1_init(&context); /* init context for 1st pass */
utils_sha1_starts(&context); /* setup context for 1st pass */
utils_sha1_update(&context, k_ipad, KEY_IOPAD_SIZE); /* start with inner pad */
utils_sha1_update(&context, (unsigned char *) msg, msg_len); /* then text of datagram */
utils_sha1_finish(&context, out); /* finish up 1st pass */
/* perform outer SHA */
utils_sha1_init(&context); /* init context for 2nd pass */
utils_sha1_starts(&context); /* setup context for 2nd pass */
utils_sha1_update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
utils_sha1_update(&context, out, SHA1_DIGEST_SIZE); /* then results of 1st hash */
utils_sha1_finish(&context, out); /* finish up 2nd pass */
for (i = 0; i < SHA1_DIGEST_SIZE; ++i) {
digest[i * 2] = utils_hb2hex(out[i] >> 4);
digest[i * 2 + 1] = utils_hb2hex(out[i]);
}
}
///*-------------------------------------------------*/
///*函数名base64解码 */
///*网上寻找的开源程序 */
///*-------------------------------------------------*/
//int base64_decode( const char * base64, unsigned char * bindata )
//{
// int i, j;
// unsigned char k;
// unsigned char temp[4];
// for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 )
// {
// memset( temp, 0xFF, sizeof(temp) );
// for ( k = 0 ; k < 64 ; k ++ )
// {
// if ( base64char[k] == base64[i] )
// temp[0]= k;
// }
// for ( k = 0 ; k < 64 ; k ++ )
// {
// if ( base64char[k] == base64[i+1] )
// temp[1]= k;
// }
// for ( k = 0 ; k < 64 ; k ++ )
// {
// if ( base64char[k] == base64[i+2] )
// temp[2]= k;
// }
// for ( k = 0 ; k < 64 ; k ++ )
// {
// if ( base64char[k] == base64[i+3] )
// temp[3]= k;
// }
// bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |
// ((unsigned char)((unsigned char)(temp[1]>>4)&0x03));
// if ( base64[i+2] == '=' )
// break;
// bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |
// ((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));
// if ( base64[i+3] == '=' )
// break;
// bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |
// ((unsigned char)(temp[3]&0x3F));
// }
// return j;
//}