zl程序教程

您现在的位置是:首页 >  系统

当前栏目

嵌入式linux开发,HmacSHA256下载与使用

2023-09-14 09:09:33 时间

HmacSHA256,散列哈希HASH加密

一、C语言,HMACSHA256-master

GitHub - yingzh2020/HMACSHA256: hmacsha256,HMACSHA256-master,选此

GitHub - megrimm/pd-hmacsha256: puredata library for creating hmac sha 256

例程

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "hmac_sha256.h"
#include <string.h>

#ifdef  __cplusplus
extern "C" {
#endif

int main(int argc, char **argv)
{
	for (int i = 0; i < argc; i++)
	{
		printf("argv[%d]=%s\n", i, argv[i]);
	}

	if (argc < 2)
	{
		printf("atgc < 2\n");
		
		return -1;
	}
	
	//sha256
	uint8_t *text = argv[1];
	uint8_t buf[SHA256_BLOCK_SIZE] = {0};
	SHA256_CTX ctx;
	SHA256_Init(&ctx);
	SHA256_Update(&ctx, text, strlen((const char *)text));//无法比对成功,是否编码问题
	SHA256_Final(&ctx, buf);
	printf("sha256=\n");
	for (int i = 0; i < SHA256_BLOCK_SIZE; i++)
	{
		printf("%02x", buf[i]);
	}
	printf("\n");

	//Hmacsha256
	unsigned char *enkey = "";
	unsigned char keylen = 0;
	unsigned char *encdata = argv[1];
	unsigned char datalen = strlen(encdata);
	//unsigned char output[SHA256_BLOCK_SIZE] = {0};
	Hmacsha256_enc(enkey, keylen, encdata, datalen, buf);
	printf("Hmacsha256=\n");
	for (int i = 0; i < SHA256_BLOCK_SIZE; i++)
	{
		printf("%02x", buf[i]);
	}
	printf("\n");
	
	return 0;
}

#ifdef  __cplusplus
}
#endif

二、C++,只有sha256,无Hmacsha256

GitHub - FreeCodeCampXYG/HmacSHA256

https://codeload.github.com/dcariotti/hmacsha256/zip/refs/heads/main,hmacsha256-main,选此