Reputation: 31
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?
Upvotes: 1
Views: 1944
Reputation: 308001
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.
Upvotes: 3