简体   繁体   中英

Getting different hashes from two identical strings

I'm making some experiments with hashing. I'm getting a problem while doing a simple test.

This is my code:

String newWord = readFile("C:\\Users\\javip\\Desktop\\Workspace SSII\\listado-general.txt").get(5);
System.out.println(newWord);
String qwerty = "qwerty2";
System.out.println(qwerty);
System.out.println(newWord.equals(qwerty));
String sha256hex = DigestUtils.sha256Hex(newWord); 
System.out.println(DigestUtils.sha256Hex(qwerty));
System.out.println(DigestUtils.sha256Hex(sha256hex));

And here it is what my console prints:

qwerty2
qwerty2
true
42ad28944380f770cf17432c3494c07c32f680173b42c3562888f096e738ef7a
ebd11cf2e1a82248edff75899ba331ffa35787c070767da0c695bba8e2be5355

What am I doing wrong? I know by comprobation in some SHA256 encrypters of Intenet that

42ad28944380f770cf17432c3494c07c32f680173b42c3562888f096e738ef7a

is the correct hash for "qwerty2" using SHA256.

Your last line:

System.out.println(DigestUtils.sha256Hex(sha256hex));

is effectively:

System.out.println(DigestUtils.sha256Hex(DigestUtils.sha256Hex(newWord)));

ie you're printing the hash of the hash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM