Reputation: 89
I am trying to hash strings using MD5. I need the hashed value as a 128 bit unsigned integer in Java.
MessageDigest md = MessageDigest.getInstance("MD5");
String toHash = "HashThis";
md.update(toHash.getBytes());
byte[] isHashed = md.digest();
How do I convert isHashed to an integer?
Upvotes: 0
Views: 1564
Reputation: 73568
Use BigInteger
.
BigInteger value = new BigInteger(isHashed);
To ensure that the resulting value is positive (as the byte array is expected to be in 2's complement), make sure the most significant bit is zero. This can be easily achieved by making the array 1 element bigger, with the first byte being 0.
Upvotes: 1
Reputation: 2383
Java does not have a 128 bit int type. But it has a BigInteger class, which have a constructor that takes a signum and a magnitude expressed as a byte[], as you require.
BigInteger value=new BigInteger(1,isHashed);
Upvotes: 1