Reputation: 7014
I have created MD5 hash.Its working fine now.I want output in 16 character.Current Code is returning 32 characters.
This is my code:
try {
String text = "Hello World";
MessageDigest msg = MessageDigest.getInstance("MD5");
msg.update(text.getBytes(), 0, text.length());
String digest1 = new BigInteger(1, msg.digest()).toString(16);
System.out.println("MD5: " + digest1.length());
System.out.println("MD5: " + digest1);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(AndroidActivationView.class.getName()).log(Level.SEVERE, null, ex);
}
Where I want to change it.
How can i get the 16 character output.?
Thanks in advance;
Upvotes: 2
Views: 13646
Reputation: 269707
Cut the string to length: digest1 = digest1.substring(0, 16);
MD5 outputs 16 bytes. If you encode it in hexadecimal, its 32 characters. If you encode it in base-64 it's 24 characters. Base-85 will squeeze it into 20 characters. There's no well-known encoding that is one character per byte.
Upvotes: 8