MessageDigest hashes differently on different machines

前端 未结 2 1267
栀梦
栀梦 2021-02-02 01:56

I\'m having a problem with MessageDigest returning different hash values on different computers.

One computer is running 32-bit Java on Windows Vista and the other is ru

2条回答
  •  梦谈多话
    2021-02-02 02:14

    Encodings are causing you problems. First here:

    saltPlusPlainTextPassword.getBytes()
    

    That will use the default encoding for the machine. Bad idea. Specify "UTF-8" as a simple solution. (It's guaranteed to be present.)

    Next this causes issues:

    String hashed = new String(hashedByteArray);
    

    hashedByteArray is arbitrary binary data. To safely convert it to text, either use a base-64 encoding or just hex. Again, you're currently using the default encoding, which will vary from machine to machine. There are loads of 3rd party libraries for base64 encoding in Java.

提交回复
热议问题