SHA1 - 生成sha1散列值

SHA 是 Secure Hash Algorithm (安全散列算法) 的缩写,它用来产生 20 个字节 (160位) 的散列值,该算法常用于数字签名,以防止数据遭到篡改。

这里介绍 openssl 提供 API 计算数据的 SHA1 值。实际上,该 API 有两套,这里只给出最简单的函数,即  SHA1(),其原型如下:
#include <openssl/sha.h>
unsigned char *SHA1(const unsigned char *d, unsigned long n, unsigned char *md);
函数中的第 1 个参数 d 表示要处理的数据;第 2 个参数 n 表示该数据的长度;第 3 个参数 md 表示计算出的 sha 值的存放地方,如果为 NULL,那么系统会安排一个静态缓冲区对其存储。

测试代码:
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <openssl/sha.h>
  
 #define SHA1LEN SHA_DIGEST_LENGTH
  
 static const char hex_chars[] = "0123456789abcdef";
   
 void convert_hex(unsigned char *md, unsigned char *mdstr)
 {
     int i;
     int j = 0;
     unsigned int c;
   
     for (i = 0; i < 20; i++) {
         c = (md[i] >> 4) & 0x0f;
         mdstr[j++] = hex_chars[c];
         mdstr[j++] = hex_chars[md[i] & 0x0f];
     }
     mdstr[40] = '\0';
 }
  
 int main(int argc, char **argv)
 {
     if (argc != 2) {
        fprintf (stderr, "usage: %s your-string\n", argv[0]);
        exit (EXIT_FAILURE);
     }
      
     char md[SHA_DIGEST_LENGTH];
     char mdstr[40];
  
     bzero(md, SHA_DIGEST_LENGTH);
     bzero(mdstr, 40);
      
     SHA1(argv[1], strlen(argv[1]), md);
  
     convert_hex(md, mdstr);
  
     printf ("Result of SHA1 : %s\n", mdstr);
  
     return 0;
 }

运行输出:

beyes:/home/beyes/c/openssl # ./sha1 groad.net
Result of SHA1 : f6f80b59f1b25c82b64d857594fee53cd0df3604

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值