免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2487 | 回复: 5
打印 上一主题 下一主题

[C] 同样的代码在SCO和AIX算出SHA-1值不一样,什么原因 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-10 23:09 |只看该作者 |倒序浏览
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <errno.h>

  6. #define BIG_ENDIAN_HOST 1
  7. typedef unsigned int u32;

  8. /****************
  9. * Rotate a 32 bit integer by n bytes
  10. ****************/
  11. #if defined(__GNUC__) && defined(__i386__)
  12. static inline u32 rol( u32 x, int n)
  13. {
  14. __asm__("roll %%cl,%0"
  15.             :"=r" (x)
  16.             :"0" (x),"c" (n));
  17. return x;
  18. }
  19. #else
  20. #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
  21. #endif


  22. typedef struct {
  23.     u32 h0,h1,h2,h3,h4;
  24.     u32 nblocks;
  25.     unsigned char buf[64];
  26.     int count;
  27. } SHA1_CONTEXT;



  28. void sha1_init( SHA1_CONTEXT *hd )
  29. {
  30.     hd->h0 = 0x67452301;
  31.     hd->h1 = 0xefcdab89;
  32.     hd->h2 = 0x98badcfe;
  33.     hd->h3 = 0x10325476;
  34.     hd->h4 = 0xc3d2e1f0;
  35.     hd->nblocks = 0;
  36.     hd->count = 0;
  37. }


  38. /*
  39. * Transform the message X which consists of 16 32-bit-words
  40. */
  41. static void
  42. transform( SHA1_CONTEXT *hd, unsigned char *data )
  43. {
  44.     u32 a,b,c,d,e,tm;
  45.     u32 x[16];

  46.     /* get values from the chaining vars */
  47.     a = hd->h0;
  48.     b = hd->h1;
  49.     c = hd->h2;
  50.     d = hd->h3;
  51.     e = hd->h4;

  52. #ifdef BIG_ENDIAN_HOST
  53.     memcpy( x, data, 64 );
  54. #else
  55.     { int i;
  56.         unsigned char *p2;
  57.         for(i=0, p2=(unsigned char*)x; i < 16; i++, p2 += 4 ) {
  58.             p2[3] = *data++;
  59.             p2[2] = *data++;
  60.             p2[1] = *data++;
  61.             p2[0] = *data++;
  62.         }
  63.     }
  64. #endif


  65. #define K1 0x5A827999L
  66. #define K2 0x6ED9EBA1L
  67. #define K3 0x8F1BBCDCL
  68. #define K4 0xCA62C1D6L
  69. #define F1(x,y,z)   ( z ^ ( x & ( y ^ z ) ) )
  70. #define F2(x,y,z)   ( x ^ y ^ z )
  71. #define F3(x,y,z)   ( ( x & y ) | ( z & ( x | y ) ) )
  72. #define F4(x,y,z)   ( x ^ y ^ z )


  73. #define M(i) ( tm =   x[i&0x0f] ^ x[(i-14)&0x0f]    \
  74.                ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f]      \
  75.                , (x[i&0x0f] = rol(tm,1)) )

  76. #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 )   \
  77.             + f( b, c, d )                          \
  78.             + k                                     \
  79.             + m;                                    \
  80.         b = rol( b, 30 );                           \
  81.     } while(0)
  82.     R( a, b, c, d, e, F1, K1, x[ 0] );
  83.     R( e, a, b, c, d, F1, K1, x[ 1] );
  84.     R( d, e, a, b, c, F1, K1, x[ 2] );
  85.     R( c, d, e, a, b, F1, K1, x[ 3] );
  86.     R( b, c, d, e, a, F1, K1, x[ 4] );
  87.     R( a, b, c, d, e, F1, K1, x[ 5] );
  88.     R( e, a, b, c, d, F1, K1, x[ 6] );
  89.     R( d, e, a, b, c, F1, K1, x[ 7] );
  90.     R( c, d, e, a, b, F1, K1, x[ 8] );
  91.     R( b, c, d, e, a, F1, K1, x[ 9] );
  92.     R( a, b, c, d, e, F1, K1, x[10] );
  93.     R( e, a, b, c, d, F1, K1, x[11] );
  94.     R( d, e, a, b, c, F1, K1, x[12] );
  95.     R( c, d, e, a, b, F1, K1, x[13] );
  96.     R( b, c, d, e, a, F1, K1, x[14] );
  97.     R( a, b, c, d, e, F1, K1, x[15] );
  98.     R( e, a, b, c, d, F1, K1, M(16) );
  99.     R( d, e, a, b, c, F1, K1, M(17) );
  100.     R( c, d, e, a, b, F1, K1, M(18) );
  101.     R( b, c, d, e, a, F1, K1, M(19) );
  102.     R( a, b, c, d, e, F2, K2, M(20) );
  103.     R( e, a, b, c, d, F2, K2, M(21) );
  104.     R( d, e, a, b, c, F2, K2, M(22) );
  105.     R( c, d, e, a, b, F2, K2, M(23) );
  106.     R( b, c, d, e, a, F2, K2, M(24) );
  107.     R( a, b, c, d, e, F2, K2, M(25) );
  108.     R( e, a, b, c, d, F2, K2, M(26) );
  109.     R( d, e, a, b, c, F2, K2, M(27) );
  110.     R( c, d, e, a, b, F2, K2, M(28) );
  111.     R( b, c, d, e, a, F2, K2, M(29) );
  112.     R( a, b, c, d, e, F2, K2, M(30) );
  113.     R( e, a, b, c, d, F2, K2, M(31) );
  114.     R( d, e, a, b, c, F2, K2, M(32) );
  115.     R( c, d, e, a, b, F2, K2, M(33) );
  116.     R( b, c, d, e, a, F2, K2, M(34) );
  117.     R( a, b, c, d, e, F2, K2, M(35) );
  118.     R( e, a, b, c, d, F2, K2, M(36) );
  119.     R( d, e, a, b, c, F2, K2, M(37) );
  120.     R( c, d, e, a, b, F2, K2, M(38) );
  121.     R( b, c, d, e, a, F2, K2, M(39) );
  122.     R( a, b, c, d, e, F3, K3, M(40) );
  123.     R( e, a, b, c, d, F3, K3, M(41) );
  124.     R( d, e, a, b, c, F3, K3, M(42) );
  125.     R( c, d, e, a, b, F3, K3, M(43) );
  126.     R( b, c, d, e, a, F3, K3, M(44) );
  127.     R( a, b, c, d, e, F3, K3, M(45) );
  128.     R( e, a, b, c, d, F3, K3, M(46) );
  129.     R( d, e, a, b, c, F3, K3, M(47) );
  130.     R( c, d, e, a, b, F3, K3, M(48) );
  131.     R( b, c, d, e, a, F3, K3, M(49) );
  132.     R( a, b, c, d, e, F3, K3, M(50) );
  133.     R( e, a, b, c, d, F3, K3, M(51) );
  134.     R( d, e, a, b, c, F3, K3, M(52) );
  135.     R( c, d, e, a, b, F3, K3, M(53) );
  136.     R( b, c, d, e, a, F3, K3, M(54) );
  137.     R( a, b, c, d, e, F3, K3, M(55) );
  138.     R( e, a, b, c, d, F3, K3, M(56) );
  139.     R( d, e, a, b, c, F3, K3, M(57) );
  140.     R( c, d, e, a, b, F3, K3, M(58) );
  141.     R( b, c, d, e, a, F3, K3, M(59) );
  142.     R( a, b, c, d, e, F4, K4, M(60) );
  143.     R( e, a, b, c, d, F4, K4, M(61) );
  144.     R( d, e, a, b, c, F4, K4, M(62) );
  145.     R( c, d, e, a, b, F4, K4, M(63) );
  146.     R( b, c, d, e, a, F4, K4, M(64) );
  147.     R( a, b, c, d, e, F4, K4, M(65) );
  148.     R( e, a, b, c, d, F4, K4, M(66) );
  149.     R( d, e, a, b, c, F4, K4, M(67) );
  150.     R( c, d, e, a, b, F4, K4, M(68) );
  151.     R( b, c, d, e, a, F4, K4, M(69) );
  152.     R( a, b, c, d, e, F4, K4, M(70) );
  153.     R( e, a, b, c, d, F4, K4, M(71) );
  154.     R( d, e, a, b, c, F4, K4, M(72) );
  155.     R( c, d, e, a, b, F4, K4, M(73) );
  156.     R( b, c, d, e, a, F4, K4, M(74) );
  157.     R( a, b, c, d, e, F4, K4, M(75) );
  158.     R( e, a, b, c, d, F4, K4, M(76) );
  159.     R( d, e, a, b, c, F4, K4, M(77) );
  160.     R( c, d, e, a, b, F4, K4, M(78) );
  161.     R( b, c, d, e, a, F4, K4, M(79) );

  162.     /* Update chaining vars */
  163.     hd->h0 += a;
  164.     hd->h1 += b;
  165.     hd->h2 += c;
  166.     hd->h3 += d;
  167.     hd->h4 += e;
  168. }


  169. /* Update the message digest with the contents
  170. * of INBUF with length INLEN.
  171. */
  172. static void sha1_write( SHA1_CONTEXT *hd, unsigned char *inbuf, size_t inlen)
  173. {
  174.     if( hd->count == 64 ) { /* flush the buffer */
  175.         transform( hd, hd->buf );
  176.         hd->count = 0;
  177.         hd->nblocks++;
  178.     }
  179.     if( !inbuf )
  180.         return;
  181.     if( hd->count ) {
  182.         for( ; inlen && hd->count < 64; inlen-- )
  183.             hd->buf[hd->count++] = *inbuf++;
  184.         sha1_write( hd, NULL, 0 );
  185.         if( !inlen )
  186.             return;
  187.     }

  188.     while( inlen >= 64 ) {
  189.         transform( hd, inbuf );
  190.         hd->count = 0;
  191.         hd->nblocks++;
  192.         inlen -= 64;
  193.         inbuf += 64;
  194.     }
  195.     for( ; inlen && hd->count < 64; inlen-- )
  196.         hd->buf[hd->count++] = *inbuf++;
  197. }


  198. /* The routine final terminates the computation and
  199. * returns the digest.
  200. * The handle is prepared for a new cycle, but adding bytes to the
  201. * handle will the destroy the returned buffer.
  202. * Returns: 20 bytes representing the digest.
  203. */

  204. static void sha1_final(SHA1_CONTEXT *hd)
  205. {
  206.     u32 t, msb, lsb;
  207.     unsigned char *p;

  208.     sha1_write(hd, NULL, 0); /* flush */;

  209.     t = hd->nblocks;
  210.     /* multiply by 64 to make a byte count */
  211.     lsb = t << 6;
  212.     msb = t >> 26;
  213.     /* add the count */
  214.     t = lsb;
  215.     if( (lsb += hd->count) < t )
  216.         msb++;
  217.     /* multiply by 8 to make a bit count */
  218.     t = lsb;
  219.     lsb <<= 3;
  220.     msb <<= 3;
  221.     msb |= t >> 29;

  222.     if( hd->count < 56 ) { /* enough room */
  223.         hd->buf[hd->count++] = 0x80; /* pad */
  224.         while( hd->count < 56 )
  225.             hd->buf[hd->count++] = 0; /* pad */
  226.     }
  227.     else { /* need one extra block */
  228.         hd->buf[hd->count++] = 0x80; /* pad character */
  229.         while( hd->count < 64 )
  230.             hd->buf[hd->count++] = 0;
  231.         sha1_write(hd, NULL, 0); /* flush */;
  232.         memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
  233.     }
  234.     /* append the 64 bit count */
  235.     hd->buf[56] = msb >> 24;
  236.     hd->buf[57] = msb >> 16;
  237.     hd->buf[58] = msb >> 8;
  238.     hd->buf[59] = msb    ;
  239.     hd->buf[60] = lsb >> 24;
  240.     hd->buf[61] = lsb >> 16;
  241.     hd->buf[62] = lsb >> 8;
  242.     hd->buf[63] = lsb    ;
  243.     transform( hd, hd->buf );

  244.     p = hd->buf;
  245. #ifdef BIG_ENDIAN_HOST
  246. #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
  247. #else /* little endian */
  248. #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
  249.         *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
  250. #endif
  251.     X(0);
  252.     X(1);
  253.     X(2);
  254.     X(3);
  255.     X(4);
  256. #undef X
  257. }

  258. /**
  259. * SHA1_Verify_File - Verify the file
  260. * in:
  261. *    @file: input file
  262. * out:
  263. *    @sha1_value: SHA-1 value
  264. */
  265. int SHA1_Verify_File( const char *file, char *sha1_value )
  266. {
  267.     FILE *fp = NULL;
  268.     unsigned char buffer[4096] = {0};
  269.     size_t n = 0;
  270.     SHA1_CONTEXT ctx;
  271.     int i = 0;

  272.     assert( sizeof(u32) == 4 );
  273.     assert( sha1_value != NULL );

  274.     fp = fopen( file, "rb" );
  275.     if( !fp ) {
  276.         return -1;
  277.     }
  278.     sha1_init( &ctx );
  279.     while( ( n = fread( buffer, 1, sizeof buffer, fp) ) )
  280.         sha1_write( &ctx, buffer, n );
  281.     if( ferror(fp) ) {
  282.         return -2;
  283.     }
  284.     sha1_final( &ctx );
  285.     fclose( fp );

  286.     for( i=0; i < 20; i++ ) {
  287.         sprintf( sha1_value, "%s%02x", sha1_value, ctx.buf[i] );
  288.     }
  289.     return 0;
  290. }

  291. /**
  292. * SHA1_Verify_Buffer - Verify the memory buffer
  293. * in:
  294. *    @buffer: buffer
  295. *    @buflen: buffer len
  296. * out:
  297. *    @sha1_value: SHA-1 value
  298. */
  299. int SHA1_Verify_Buffer( unsigned char *buffer, int buflen, char *sha1_value )
  300. {
  301.     SHA1_CONTEXT ctx;
  302.     size_t iSize = buflen;
  303.     int i = 0;

  304.     assert( sizeof(u32) == 4 );
  305.     assert( sha1_value != NULL );
  306.     assert( buffer != NULL );

  307.     sha1_init( &ctx );
  308.     sha1_write( &ctx, buffer, iSize );
  309.     sha1_final( &ctx );

  310.     for( i=0; i < 20; i++ ) {
  311.         sprintf( sha1_value, "%s%02x", sha1_value, ctx.buf[i] );
  312.     }
  313.     return 0;
  314. }
  315. /*
  316. int AppVerMac( PFS epfsAddr, char *epczBuffer, int eiLen )
  317. {
  318.    char acMac[ 40+1 ] = {0};
  319.    int i = 0;
  320.    SHA1_Verify_Buffer( (unsigned char*)(epczBuffer), eiLen, acMac );

  321.    for( i=0; i<strlen(acMac); i++ ) {
  322.       acMac[i] = toupper( acMac[i] );
  323.    }

  324.    if( strncmp( stIbsHead.gwMac, acMac, 40 ) ) {
  325.       return -1;
  326.    } else  {
  327.    }
  328.    return 0;
  329. }
  330. */

  331. int AppGenMac( char *gwMac, char *epczBuffer, int eiLen )
  332. {
  333.    char acMac[ 40+1 ] = {0};
  334.    int i = 0;

  335.    SHA1_Verify_Buffer( (unsigned char*)(epczBuffer), eiLen, acMac );

  336.    /* convert to upper case */
  337.    for( i=0; i<strlen(acMac); i++ ) {
  338.       acMac[i] = toupper( acMac[i] );
  339.    }

  340.    strncpy( gwMac, acMac, 40 );
  341.    return 0;
  342. }

  343. int main()
  344. {
  345.         char a[5];
  346.         char g[40+1];
  347.         int i=0;

  348.         memset( a, 0x00, sizeof( a ) );
  349.         memset( g, 0x00, sizeof( g ) );

  350.         strncpy(a,"<>",2);
  351.         i=AppGenMac( g, a, 2 );
  352.         printf("%s\n\n",g);
  353.         return 0;
  354. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2014-06-11 08:18 |只看该作者
big endian/little endian, 32/64

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
3 [报告]
发表于 2014-06-11 08:33 |只看该作者
SHA-1算法本身本来就没有要求一样。
实现本来就可以有差异,
正如C语言的很多编译器彼此都有点差异一样。

又不是像MD5这种算法,
算出来结果一定一样。

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
4 [报告]
发表于 2014-06-11 08:41 |只看该作者
在非BIG_ENDIAN的环境,把#define BIG_ENDIAN_HOST 1这一行注释掉试试

论坛徽章:
0
5 [报告]
发表于 2014-06-11 12:37 |只看该作者
回复 4# hellioncu

按你的方法解决了,谢谢


   

论坛徽章:
0
6 [报告]
发表于 2014-06-11 12:38 |只看该作者
hellioncu 发表于 2014-06-11 08:41
在非BIG_ENDIAN的环境,把#define BIG_ENDIAN_HOST 1这一行注释掉试试





按此方法解决了,谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP