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

t = hd->nblocks;
 /* multiply by 64 to make a byte count */
 lsb = t << 6;
 msb = t >> 26;
 /* add the count */
 t = lsb;
 if( (lsb += hd->count) < t )
  msb++;
 /* multiply by 8 to make a bit count */
 t = lsb;
 lsb <<= 3;
 msb <<= 3;
 msb |= t >> 29;

if( hd->count < 56 ) { /* enough room */
  hd->buf[hd->count++] = 0x80; /* pad */
  while( hd->count < 56 )
   hd->buf[hd->count++] = 0;  /* pad */
 }
 else { /* need one extra block */
  hd->buf[hd->count++] = 0x80; /* pad character */
  while( hd->count < 64 )
   hd->buf[hd->count++] = 0;
  sha1_write(hd, NULL, 0);  /* flush */;
  memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
 }
 /* append the 64 bit count */
 hd->buf[56] = msb >> 24;
 hd->buf[57] = msb >> 16;
 hd->buf[58] = msb >>  8;
 hd->buf[59] = msb    ;
 hd->buf[60] = lsb >> 24;
 hd->buf[61] = lsb >> 16;
 hd->buf[62] = lsb >>  8;
 hd->buf[63] = lsb    ;
 transform( hd, hd->buf );

p = hd->buf;
#ifdef BIG_ENDIAN_HOST
#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
#else /* little endian */
#define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16;  \
 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
#endif
 X(0);
 X(1);
 X(2);
 X(3);
 X(4);
#undef X
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/fb011cba2b6fc4ed19cb3da66eb1bad7.html