主题明确说明了什么,一种内存有效的方法来计算大小可变的文件的sha256哈希?我也愿意妥协使用更多的内存来加快计算速度。
我使用了Christophe Devine的一个简单的独立实现-尽管他的网站似乎不在网上,但Google Code Search 在此找到了它 。
使用这些sha256.c和sha256.h,他main()功能的核心就是
if( ! ( f = fopen( argv[1], "rb" ) ) )
{
perror( "fopen" );
return( 1 );
}
sha256_starts( &ctx );
while( ( i = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
{
sha256_update( &ctx, buf, i );
}
sha256_finish( &ctx, sha256sum );
for( j = 0; j < 32; j++ )
{
printf( "%02x", sha256sum[j] );
}
printf( " %s\n", argv[1] );
}
Run Code Online (Sandbox Code Playgroud)
该main()功能的其余部分验证了FIPS-180-2测试向量,因此您也可以获得那种温暖而模糊的感觉/ ;-)