博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux C 下使用openssl 进行SHA1加密
阅读量:6920 次
发布时间:2019-06-27

本文共 843 字,大约阅读时间需要 2 分钟。

How to replace hash functions from openssl with gcrypt. I wondered how to do it, and hacked around. git source uses ssl, and I wanted that to change.
/*
 Code snippet to calculate SHA1sum using openssl libs.
 Copyright 2005 Junichi Uekawa, given to public domain.
$ gcc openssltest.c -lssl
$ ./a.out  < ./a.out
eae8189278303caaa78f2d89e6a6ebeb7d37b554
$ sha1sum ./a.out
eae8189278303caaa78f2d89e6a6ebeb7d37b554  ./a.out
*/
#include 
<
stdio.h
>
#include 
<
openssl
/
sha.h
>
main ()
{
  SHA_CTX s;
  
int
 i, size;
  
char
 c[
512
];
  unsigned 
char
 hash[
20
];
  
  SHA1_Init(
&
s);
  
while
 ((size
=
read (
0
, c, 
512
)) 
>
 
0
)
    SHA1_Update(
&
s, c, size);
  SHA1_Final(hash, 
&
s);
  
  
for
 (i
=
0
; i 
<
 
20
; i
++
)
    printf (
"
%.2x
"
, (
int
)hash[i]);
  printf (
"
\n
"
);
}
    本文转自 OldHawk  博客园博客,原文链接:http://www.cnblogs.com/taobataoma/archive/2007/05/11/743257.html
,如需转载请自行联系原作者
你可能感兴趣的文章