67 lines
1.7 KiB
C
67 lines
1.7 KiB
C
#pragma once
|
|
#include"StdAfx.h"
|
|
#include "hmac_sha2.h"
|
|
|
|
|
|
#define MY_HMAC_KEY "kie89453"
|
|
#define MY_HMAC_KEY_LEN 8
|
|
void printf_data3(char* data, int len)
|
|
{
|
|
#define BUFF_LEN 2048*4
|
|
if (len >= BUFF_LEN)
|
|
{
|
|
char buf[255] = { 0 };
|
|
sprintf_s(buf, sizeof(buf), "data out of buff len:%d\n", len);
|
|
OutputDebugStringA(buf);
|
|
return;
|
|
}
|
|
char str[BUFF_LEN * 3] = { 0 };
|
|
for (int i = 0; i < len; ++i) {
|
|
sprintf_s(str + i * 3, sizeof(str), "%02X ", (unsigned char)(*(data + i)));
|
|
}
|
|
printf(str);
|
|
printf("\n");
|
|
//MyDbgPrint("data addr:%p len:%d %s\n", data, len, str);
|
|
}
|
|
int hmac_(char* key, int key_len, unsigned char* in_data, int in_data_len, unsigned char* out_md, int out_md_len)
|
|
{
|
|
if (out_md_len != SHA256_DIGESTLEN)
|
|
{
|
|
return -1;
|
|
}
|
|
ZeroMemory(out_md, out_md_len);
|
|
|
|
char* tmp_data = new char[in_data_len + key_len];
|
|
memcpy(tmp_data, in_data, in_data_len);
|
|
memcpy(tmp_data + in_data_len, key, key_len);
|
|
|
|
unsigned char md[SHA256_DIGESTLEN] = { 0 };
|
|
hmac_sha256_ctx hmac;
|
|
hmac_sha256_init(&hmac, (const unsigned char*)key, key_len);
|
|
hmac_sha256_update(&hmac, (const unsigned char*)tmp_data, in_data_len + key_len);
|
|
hmac_sha256_final(&hmac, md, SHA256_DIGESTLEN);
|
|
memcpy(out_md, md, SHA256_DIGESTLEN);
|
|
delete[] tmp_data;
|
|
//printf_data2((char*)md, SHA256_DIGESTLEN);
|
|
return 0;
|
|
}
|
|
|
|
BOOL hmac_comp(unsigned char* hmacA, unsigned char* hmacB)
|
|
{
|
|
printf("comp hmacA:");
|
|
printf_data3((char*)hmacA, SHA256_DIGESTLEN);
|
|
|
|
printf("comp hmacB:");
|
|
printf_data3((char*)hmacB, SHA256_DIGESTLEN);
|
|
for (int i = 0; i < SHA256_DIGESTLEN; i++)
|
|
{
|
|
if (hmacA[i] != hmacB[i])
|
|
{
|
|
printf("check faild\n");
|
|
return FALSE;
|
|
}
|
|
}
|
|
printf("check OK\n");
|
|
return TRUE;
|
|
}
|