openssl -- sha1函数进行消息摘要值计算 -- 代码

#include <openssl/sha.h>
#include <string.h>
#include <stdio.h>

int main()
{
SHA_CTX stx;
unsigned char outmd[20]; //注意这里的字符个数是20

FILE * file = NULL;
int len = 0;
char filename[32];
char buffer[1024];
memset(filename, 0, sizeof(filename));
memset(buffer, 0, sizeof(buffer));

printf("Enter the file name, for SHA1\n");
scanf("%s", filename);

if((file = fopen(filename, "r")) == NULL)
{


printf("Can't open file\n");
return 0;
}
SHA1_Init(&stx);
while((len = fread(buffer, 1, 1024, file)) > 0)
{
SHA1_Update(&stx, buffer, len);
memset(buffer, 0, sizeof(buffer));
}
SHA1_Final(outmd, &stx);

for(int i = 0; i < 20; i++)
{
printf("%02x", outmd[i]);
}
printf("\n");
return 0;

}

猜你喜欢

转载自www.cnblogs.com/ruigelwang/p/12750432.html
今日推荐