LibTom 加密之SHA1
下面的C程序实现的功能:
将pInData数组内数据进行hash-sha1运算,得到摘要值hash
首先引入头文件
#include "tomcrypt.h"
#pragma comment (lib,"tomcrypt.lib")
声明变量(hash状态、执行返回结果、hash结果存放数组), 赋初值
hash_state md;
int ret = 0;
int hash_index = 0;
unsigned long hashLen = 0;
unsigned char hash[MAXBLOCKSIZE];
memset(hash,0,MAXBLOCKSIZE);
unsigned char pInData[1024] = {0};
unsigned long InDataLen = 1024;
//待HASH数据
memcpy(pInData,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",52);
注册hash函数
hash_index = register_hash(&sha1_desc);
if (hash_index == -1)
{
printf_s("register_hash err\n"");
return -1;
}
HASH后数据长160bit,20字节
hashLen = hash_descriptor[hash_index].hashsize;
HASH流程(init->process->done)
ret = hash_descriptor[hash_index].init(&md);
if (ret!=CRYPT_OK)
{
printf_s("hash_descriptor.init err\n");
return -1;
}
ret = hash_descriptor[hash_index].process(&md,pInData,InDataLen);
if (ret!=CRYPT_OK)
{
printf_s("hash_descriptor.process err\n");
return -1;
}
ret = hash_descriptor[hash_index].done(&md, hash);
if (ret!=CRYPT_OK)
{
printf_s("hash_descriptor.done err\n");
return -1;
}
打印hash结果
printf_s("消息摘要长度:\n%d\n\n",hashLen);
printf_s("消息摘要:\n");
for (int i=0;i<hashLen;i++)
{
printf_s("%x",hash[i]);
}
printf_s("\n\n");
查看GitHub 完整代码