c# - How to calculate checksum for 3 GB file but first remove the last 28 bytes -


environment: 3 gb data file 28 byte sha-1 checksum appended end.

is possible use filestream or other type of stream calculate sha-1 checksum hash reading stream position 0 position [x - 28(the length of appended checksum)] without physically removing appended checksum end of file first?

currently opening file using filestream , reading using binaryreader seek , read last 28 bytes of file.

then opening file again filestream , setting length of stream length - 28 bytes , removing checksum file.

finally calculating checksum of data file last 28 bytes removed , comparing checksum removed end of file ensure checksums match.

basically want know if it's possible calculate checksum of data part of file without having remove appended checksum first.

if know how data want read (i.e. length - 28 bytes) when compute hash, put in data. haven't said how you're computing hash, can repeatedly "write" more data hash computation - long need, until last 28 bytes.

sample:

public byte[] sha1exceptend(stream input, int bytestoomit) { long bytestoread = input.length - bytestoomit; byte[] buffer = new byte[16 * 1024]; // hash 16k @ time using (sha1 sha1 = sha1.create()) { while (bytestoread > 0) { int thispass = (int) math.min(buffer.length, bytestoread); int bytesreadthispass = input.read(buffer, 0, thispass); if (bytesreadthispass <= 0) { throw new ioexception("unexpected end of data"); } sha1.transformblock(buffer, 0, bytesreadthispass, buffer, 0); bytestoread -= bytesreadthispass; } // flush hash sha1.transformfinalblock(buffer, 0, 0); return sha1.hash; } } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -