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:

  1. get file.
  2. do arbitrary stuff isn't used.
  3. store in memory via memory stream.
  4. take out of memory via memory stream.
  5. get hash of output of arbitrary stuff.

your algorithm should be:

  1. get file.
  2. 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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -