简体   繁体   中英

The results of sha1 encryption are different between python and java

An application, i need sha1 encryption,but the results are different between python and java, java is correct. Because there is no byte object in python, and java byte is used for hash calculation. How to get the correct results with python?

As usual, the difference is not in the digest implementation (those are well-documented and implemented correctly in all major libraries). The difference is in how you represent the resulting data.

md.digest() returns a byte[] with the binary data produced by the digest.

new String(md.digest()) tries to interpret those bytes as text in the platform default encoding which is almost certainly not what you want.

You probably want the digest to be represented in a hex- or Base64 encoding.

Try this (make sure to import javax.xml.bind.DatatypeConverter ):

String result = DatatypeConverter.printHexBinary(md.digest());

Alternatively, if you need Base64, use printBase65Binary() instead.

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