c# - Sha256 hash differs from machine to machine -
we have process hashing images , , able deploy same process on other servers. how same hash value image across different servers. here code u
static void main(string[] args) { byte[] imagebytes; string imagepath = @"c:\work\projects\test.jpg"; system.drawing.image image = system.drawing.image.fromfile(imagepath); using (system.io.memorystream ms = new system.io.memorystream()) { image.save(ms, system.drawing.imaging.imageformat.jpeg); imagebytes = ms.toarray(); } string photochecksumsha256 = computesha256checksum(imagebytes); console.writeline(photochecksumsha256); console.readkey(); } static string computesha256checksum(byte[] data) { using (system.io.memorystream stream = new system.io.memorystream(data)) { system.security.cryptography.sha256 sha256 = new system.security.cryptography.sha256cryptoserviceprovider(); byte[] sha256ret = sha256.computehash(stream); stringbuilder sb = new stringbuilder(); (int = 0; < sha256ret.length; i++) { sb.append(sha256ret[i].tostring("x2")); } return sb.tostring(); } }
there isn't guarantee work same way on same machine 1 run another, implementations of jpeg encoding non-deterministic.
your algorithm far is:
- get file.
- do arbitrary stuff isn't used.
- store in memory via memory stream.
- take out of memory via memory stream.
- get hash of output of arbitrary stuff.
your algorithm should be:
- get file.
- get hash of file.
you don't need care whether jpeg file or not. 1 possible advantage code throw exception if .net couldn't handle image (a disadvantage in cases, advantage if it's better fail down line). if that's necessary, call image.fromfile()
separately hashing.
Comments
Post a Comment