C语言获取文件的SHA1哈希值(文件校验)

210 篇文章 0 订阅
17 篇文章 0 订阅

安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准 (Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,那么这时候就会产生不同的消息摘要。 SHA1有如下特性:不可以从消息摘要中复原信息;两个不同的消息不会产生同样的消息摘要。

SHA1 C语言实现

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <assert.h>  
  5. #include <errno.h>  
  6.   
  7. #undef BIG_ENDIAN_HOST  
  8. typedef unsigned int u32;  
  9.   
  10. /**************** 
  11. * Rotate a 32 bit integer by n bytes 
  12. */  
  13. #if defined(__GNUC__) && defined(__i386__)  
  14. static inline u32  
  15.     rol( u32 x, int n)  
  16. {  
  17.     __asm__("roll %%cl,%0"  
  18.         :"=r" (x)  
  19.         :"0" (x),"c" (n));  
  20.     return x;  
  21. }  
  22. #else  
  23. #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )  
  24. #endif  
  25.   
  26.   
  27. typedef struct {  
  28.     u32  h0,h1,h2,h3,h4;  
  29.     u32  nblocks;  
  30.     unsigned char buf[64];  
  31.     int  count;  
  32. } SHA1_CONTEXT;  
  33.   
  34.   
  35.   
  36. void  
  37.     sha1_init( SHA1_CONTEXT *hd )  
  38. {  
  39.     hd->h0 = 0x67452301;  
  40.     hd->h1 = 0xefcdab89;  
  41.     hd->h2 = 0x98badcfe;  
  42.     hd->h3 = 0x10325476;  
  43.     hd->h4 = 0xc3d2e1f0;  
  44.     hd->nblocks = 0;  
  45.     hd->count = 0;  
  46. }  
  47.   
  48.   
  49. /**************** 
  50. * Transform the message X which consists of 16 32-bit-words 
  51. */  
  52. static void  
  53.     transform( SHA1_CONTEXT *hd, unsigned char *data )  
  54. {  
  55.     u32 a,b,c,d,e,tm;  
  56.     u32 x[16];  
  57.   
  58.     /* get values from the chaining vars */  
  59.     a = hd->h0;  
  60.     b = hd->h1;  
  61.     c = hd->h2;  
  62.     d = hd->h3;  
  63.     e = hd->h4;  
  64.   
  65. #ifdef BIG_ENDIAN_HOST  
  66.     memcpy( x, data, 64 );  
  67. #else  
  68.     {  
  69.         int i;  
  70.         unsigned char *p2;  
  71.         for(i=0, p2=(unsigned char*)x; i < 16; i++, p2 += 4 )   
  72.         {  
  73.             p2[3] = *data++;  
  74.             p2[2] = *data++;  
  75.             p2[1] = *data++;  
  76.             p2[0] = *data++;  
  77.         }  
  78.     }  
  79. #endif  
  80.   
  81.   
  82. #define K1  0x5A827999L  
  83. #define K2  0x6ED9EBA1L  
  84. #define K3  0x8F1BBCDCL  
  85. #define K4  0xCA62C1D6L  
  86. #define F1(x,y,z)   ( z ^ ( x & ( y ^ z ) ) )  
  87. #define F2(x,y,z)   ( x ^ y ^ z )  
  88. #define F3(x,y,z)   ( ( x & y ) | ( z & ( x | y ) ) )  
  89. #define F4(x,y,z)   ( x ^ y ^ z )  
  90.   
  91.   
  92. #define M(i) ( tm =   x[i&0x0f] ^ x[(i-14)&0x0f] \  
  93.     ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \  
  94.     , (x[i&0x0f] = rol(tm,1)) )  
  95.   
  96. #define R(a,b,c,d,e,f,k,m)  do { e += rol( a, 5 )     \  
  97.     + f( b, c, d )  \  
  98.     + k       \  
  99.     + m;          \  
  100.     b = rol( b, 30 );    \  
  101.     } while(0)  
  102.     R( a, b, c, d, e, F1, K1, x[ 0] );  
  103.     R( e, a, b, c, d, F1, K1, x[ 1] );  
  104.     R( d, e, a, b, c, F1, K1, x[ 2] );  
  105.     R( c, d, e, a, b, F1, K1, x[ 3] );  
  106.     R( b, c, d, e, a, F1, K1, x[ 4] );  
  107.     R( a, b, c, d, e, F1, K1, x[ 5] );  
  108.     R( e, a, b, c, d, F1, K1, x[ 6] );  
  109.     R( d, e, a, b, c, F1, K1, x[ 7] );  
  110.     R( c, d, e, a, b, F1, K1, x[ 8] );  
  111.     R( b, c, d, e, a, F1, K1, x[ 9] );  
  112.     R( a, b, c, d, e, F1, K1, x[10] );  
  113.     R( e, a, b, c, d, F1, K1, x[11] );  
  114.     R( d, e, a, b, c, F1, K1, x[12] );  
  115.     R( c, d, e, a, b, F1, K1, x[13] );  
  116.     R( b, c, d, e, a, F1, K1, x[14] );  
  117.     R( a, b, c, d, e, F1, K1, x[15] );  
  118.     R( e, a, b, c, d, F1, K1, M(16) );  
  119.     R( d, e, a, b, c, F1, K1, M(17) );  
  120.     R( c, d, e, a, b, F1, K1, M(18) );  
  121.     R( b, c, d, e, a, F1, K1, M(19) );  
  122.     R( a, b, c, d, e, F2, K2, M(20) );  
  123.     R( e, a, b, c, d, F2, K2, M(21) );  
  124.     R( d, e, a, b, c, F2, K2, M(22) );  
  125.     R( c, d, e, a, b, F2, K2, M(23) );  
  126.     R( b, c, d, e, a, F2, K2, M(24) );  
  127.     R( a, b, c, d, e, F2, K2, M(25) );  
  128.     R( e, a, b, c, d, F2, K2, M(26) );  
  129.     R( d, e, a, b, c, F2, K2, M(27) );  
  130.     R( c, d, e, a, b, F2, K2, M(28) );  
  131.     R( b, c, d, e, a, F2, K2, M(29) );  
  132.     R( a, b, c, d, e, F2, K2, M(30) );  
  133.     R( e, a, b, c, d, F2, K2, M(31) );  
  134.     R( d, e, a, b, c, F2, K2, M(32) );  
  135.     R( c, d, e, a, b, F2, K2, M(33) );  
  136.     R( b, c, d, e, a, F2, K2, M(34) );  
  137.     R( a, b, c, d, e, F2, K2, M(35) );  
  138.     R( e, a, b, c, d, F2, K2, M(36) );  
  139.     R( d, e, a, b, c, F2, K2, M(37) );  
  140.     R( c, d, e, a, b, F2, K2, M(38) );  
  141.     R( b, c, d, e, a, F2, K2, M(39) );  
  142.     R( a, b, c, d, e, F3, K3, M(40) );  
  143.     R( e, a, b, c, d, F3, K3, M(41) );  
  144.     R( d, e, a, b, c, F3, K3, M(42) );  
  145.     R( c, d, e, a, b, F3, K3, M(43) );  
  146.     R( b, c, d, e, a, F3, K3, M(44) );  
  147.     R( a, b, c, d, e, F3, K3, M(45) );  
  148.     R( e, a, b, c, d, F3, K3, M(46) );  
  149.     R( d, e, a, b, c, F3, K3, M(47) );  
  150.     R( c, d, e, a, b, F3, K3, M(48) );  
  151.     R( b, c, d, e, a, F3, K3, M(49) );  
  152.     R( a, b, c, d, e, F3, K3, M(50) );  
  153.     R( e, a, b, c, d, F3, K3, M(51) );  
  154.     R( d, e, a, b, c, F3, K3, M(52) );  
  155.     R( c, d, e, a, b, F3, K3, M(53) );  
  156.     R( b, c, d, e, a, F3, K3, M(54) );  
  157.     R( a, b, c, d, e, F3, K3, M(55) );  
  158.     R( e, a, b, c, d, F3, K3, M(56) );  
  159.     R( d, e, a, b, c, F3, K3, M(57) );  
  160.     R( c, d, e, a, b, F3, K3, M(58) );  
  161.     R( b, c, d, e, a, F3, K3, M(59) );  
  162.     R( a, b, c, d, e, F4, K4, M(60) );  
  163.     R( e, a, b, c, d, F4, K4, M(61) );  
  164.     R( d, e, a, b, c, F4, K4, M(62) );  
  165.     R( c, d, e, a, b, F4, K4, M(63) );  
  166.     R( b, c, d, e, a, F4, K4, M(64) );  
  167.     R( a, b, c, d, e, F4, K4, M(65) );  
  168.     R( e, a, b, c, d, F4, K4, M(66) );  
  169.     R( d, e, a, b, c, F4, K4, M(67) );  
  170.     R( c, d, e, a, b, F4, K4, M(68) );  
  171.     R( b, c, d, e, a, F4, K4, M(69) );  
  172.     R( a, b, c, d, e, F4, K4, M(70) );  
  173.     R( e, a, b, c, d, F4, K4, M(71) );  
  174.     R( d, e, a, b, c, F4, K4, M(72) );  
  175.     R( c, d, e, a, b, F4, K4, M(73) );  
  176.     R( b, c, d, e, a, F4, K4, M(74) );  
  177.     R( a, b, c, d, e, F4, K4, M(75) );  
  178.     R( e, a, b, c, d, F4, K4, M(76) );  
  179.     R( d, e, a, b, c, F4, K4, M(77) );  
  180.     R( c, d, e, a, b, F4, K4, M(78) );  
  181.     R( b, c, d, e, a, F4, K4, M(79) );  
  182.   
  183.     /* Update chaining vars */  
  184.     hd->h0 += a;  
  185.     hd->h1 += b;  
  186.     hd->h2 += c;  
  187.     hd->h3 += d;  
  188.     hd->h4 += e;  
  189. }  
  190.   
  191.   
  192. /* Update the message digest with the contents 
  193. * of INBUF with length INLEN. 
  194. */  
  195. static void  
  196.     sha1_write( SHA1_CONTEXT *hd, unsigned char *inbuf, size_t inlen)  
  197. {  
  198.     if( hd->count == 64 ) { /* flush the buffer */  
  199.         transform( hd, hd->buf );  
  200.         hd->count = 0;  
  201.         hd->nblocks++;  
  202.     }  
  203.     if( !inbuf )  
  204.         return;  
  205.     if( hd->count ) {  
  206.         for( ; inlen && hd->count < 64; inlen-- )  
  207.             hd->buf[hd->count++] = *inbuf++;  
  208.         sha1_write( hd, NULL, 0 );  
  209.         if( !inlen )  
  210.             return;  
  211.     }  
  212.   
  213.     while( inlen >= 64 ) {  
  214.         transform( hd, inbuf );  
  215.         hd->count = 0;  
  216.         hd->nblocks++;  
  217.         inlen -= 64;  
  218.         inbuf += 64;  
  219.     }  
  220.     for( ; inlen && hd->count < 64; inlen-- )  
  221.         hd->buf[hd->count++] = *inbuf++;  
  222. }  
  223.   
  224.   
  225. /* The routine final terminates the computation and 
  226. * returns the digest. 
  227. * The handle is prepared for a new cycle, but adding bytes to the 
  228. * handle will the destroy the returned buffer. 
  229. * Returns: 20 bytes representing the digest. 
  230. */  
  231.   
  232. static void  
  233.     sha1_final(SHA1_CONTEXT *hd)  
  234. {  
  235.     u32 t, msb, lsb;  
  236.     unsigned char *p;  
  237.   
  238.     sha1_write(hd, NULL, 0); /* flush */;  
  239.   
  240.     t = hd->nblocks;  
  241.     /* multiply by 64 to make a byte count */  
  242.     lsb = t << 6;  
  243.     msb = t >> 26;  
  244.     /* add the count */  
  245.     t = lsb;  
  246.     if( (lsb += hd->count) < t )  
  247.         msb++;  
  248.     /* multiply by 8 to make a bit count */  
  249.     t = lsb;  
  250.     lsb <<= 3;  
  251.     msb <<= 3;  
  252.     msb |= t >> 29;  
  253.   
  254.     if( hd->count < 56 ) { /* enough room */  
  255.         hd->buf[hd->count++] = 0x80; /* pad */  
  256.         while( hd->count < 56 )  
  257.             hd->buf[hd->count++] = 0;  /* pad */  
  258.     }  
  259.     else { /* need one extra block */  
  260.         hd->buf[hd->count++] = 0x80; /* pad character */  
  261.         while( hd->count < 64 )  
  262.             hd->buf[hd->count++] = 0;  
  263.         sha1_write(hd, NULL, 0);  /* flush */;  
  264.         memset(hd->buf, 0, 56 ); /* fill next block with zeroes */  
  265.     }  
  266.     /* append the 64 bit count */  
  267.     hd->buf[56] = msb >> 24;  
  268.     hd->buf[57] = msb >> 16;  
  269.     hd->buf[58] = msb >>  8;  
  270.     hd->buf[59] = msb       ;  
  271.     hd->buf[60] = lsb >> 24;  
  272.     hd->buf[61] = lsb >> 16;  
  273.     hd->buf[62] = lsb >>  8;  
  274.     hd->buf[63] = lsb       ;  
  275.     transform( hd, hd->buf );  
  276.   
  277.     p = hd->buf;  
  278. #ifdef BIG_ENDIAN_HOST  
  279. #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)  
  280. #else /* little endian */  
  281. #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16;    \  
  282.     *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)  
  283. #endif  
  284.     X(0);  
  285.     X(1);  
  286.     X(2);  
  287.     X(3);  
  288.     X(4);  
  289. #undef X  
  290. }  

控制台调用函数:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*输出文件的SHA1值 
  2. * FileNameInPut:文件路径 
  3. */  
  4. void GetFileSHA1(char *FileNameInPut)  
  5. {  
  6.     if(FileNameInPut==NULL)   
  7.     {  
  8.         printf("\nUsage:\n     <EXEFILE> <FILENAME>\n ");  
  9.         return;  
  10.     }  
  11.     FILE *fp;  
  12.     char buffer[4096];  
  13.     size_t n;  
  14.     SHA1_CONTEXT ctx;  
  15.     int i;  
  16.   
  17.     fopen_s (&fp, FileNameInPut, "rb");  
  18.     if (!fp)              
  19.     {  
  20.         printf("打开文件“%s”失败\n", FileNameInPut);  
  21.         return;  
  22.     }  
  23.     sha1_init (&ctx);  
  24.     while ( (n = fread (buffer, 1, sizeof buffer, fp)))     sha1_write (&ctx, (unsigned char *)buffer, n);  
  25.     if (ferror (fp))  
  26.     {  
  27.         printf("读取文件“%s”失败\n", FileNameInPut);  
  28.         return;  
  29.     }  
  30.     sha1_final (&ctx);  
  31.     fclose (fp);  
  32.   
  33.     for ( i=0; i < 20; i++)  
  34.     {  
  35.         printf("%02x",ctx.buf[i]);  
  36.     }  
  37. }  

适合程序中调用的返回值方式:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*获取文件的SHA1值,如果发生错误则将错误信息写入outError 
  2. * FileNameInPut:文件路径 
  3. * outSHA1:SHA1输出变量 
  4. * outError:错误信息输出变量 
  5. * returns:outSHA1 
  6. */  
  7. char *GetFileSHA1(char *FileNameInPut, char *outSHA1, char *outError)  
  8. {  
  9.     if(FileNameInPut==NULL)   
  10.     {  
  11.         if (outError != NULL)  
  12.         {  
  13.             sprintf(outError, "%s""FileNameInPut Is NULL");  
  14.         }  
  15.         return outSHA1;  
  16.     }  
  17.     FILE *fp;  
  18.     char buffer[4096];  
  19.     size_t n;  
  20.     SHA1_CONTEXT ctx;  
  21.     int i;  
  22.   
  23.     fopen_s (&fp, FileNameInPut, "rb");  
  24.     if (!fp)              
  25.     {  
  26.         if (outError != NULL)  
  27.         {  
  28.             sprintf(outError, "打开文件“%s”失败\n", FileNameInPut);  
  29.         }  
  30.         return outSHA1;  
  31.     }  
  32.     sha1_init (&ctx);  
  33.     while ( (n = fread (buffer, 1, sizeof buffer, fp)))     sha1_write (&ctx, (unsigned char *)buffer, n);  
  34.     if (ferror (fp))  
  35.     {  
  36.         if (outError != NULL)  
  37.         {  
  38.             sprintf(outError, "读取文件“%s”失败\n", FileNameInPut);  
  39.         }  
  40.         return outSHA1;  
  41.     }  
  42.     sha1_final (&ctx);  
  43.     fclose (fp);  
  44.   
  45.     for ( i=0; i < 20; i++)  
  46.     {  
  47.         sprintf(outSHA1 + 2*i, "%02x", (unsigned char)ctx.buf[i]);  
  48.     }  
  49.     outSHA1[2*i] = '\0';  
  50.     return outSHA1;  
  51. }  
水平有限,此方法只是简单的实现,还有些问题没有解决,希望高手指点一二,小弟不胜感激!

用法示例:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //用法实例:  
  2. int main (int argc, char **argv)  
  3. {  
  4.     GetFileSHA1(*(argv+1));  
  5.   
  6.     printf("\r\n");  
  7.     char sha1[41] = { 0 };  
  8.     char eror[256] = { 0 };  
  9.     printf("%s\r\n", GetFileSHA1(*(argv+1), sha1, NULL));  
  10.     if (strlen(eror) != 0)  
  11.     {  
  12.         printf("获取SHA1发生错误:%s\r\n", eror);  
  13.     }  
  14.   
  15.     printf("%s\r\n", GetFileSHA1(*(argv+1), sha1, eror));  
  16.     if (strlen(eror) != 0)  
  17.     {  
  18.         printf("获取SHA1发生错误:%s\r\n", eror);  
  19.     }  
  20.     getchar();  
  21.     return 0;  
  22. }  

命令提示符下用法:



源码下载:http://download.csdn.net/detail/testcs_dn/7332933


附上作者原文地址:http://blog.csdn.net/testcs_dn/article/details/25569975

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值