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


/* Update the message digest with the contents
* of INBUF with length INLEN.
*/
static void
 sha1_write( SHA1_CONTEXT *hd, unsigned char *inbuf, size_t inlen)
{
 if( hd->count == 64 ) { /* flush the buffer */
  transform( hd, hd->buf );
  hd->count = 0;
  hd->nblocks++;
 }
 if( !inbuf )
  return;
 if( hd->count ) {
  for( ; inlen && hd->count < 64; inlen-- )
   hd->buf[hd->count++] = *inbuf++;
  sha1_write( hd, NULL, 0 );
  if( !inlen )
   return;
 }

while( inlen >= 64 ) {
  transform( hd, inbuf );
  hd->count = 0;
  hd->nblocks++;
  inlen -= 64;
  inbuf += 64;
 }
 for( ; inlen && hd->count < 64; inlen-- )
  hd->buf[hd->count++] = *inbuf++;
}


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

static void
 sha1_final(SHA1_CONTEXT *hd)
{
 u32 t, msb, lsb;
 unsigned char *p;

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

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

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