Java MD5 Hash Sample
MD5 (Message Digest) is an encryption algorithm that creates a unique hash from a given data which is completely reverseble. This means you cannot recalculate original data using its hash , and hash is universally unique.
Here is a sample code that produces MD5 hash of a given string :
Here is a sample code that produces MD5 hash of a given string :
- package javacream;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- public class MD5HashTest {
- try {
- byte[] buffer = str.getBytes();
- byte[] result = null;
- //allocate room for the hash result = new byte[md5.getDigestLength()];
- //calculate hash md5.reset();
- md5.update(buffer);
- result = md5.digest();
- //create hex string from the 16-byte hash buf = new StringBuffer(result.length * 2);
- for (int i = 0;
- i <
- result.length;
- i++) {
- int intVal = result[i] & 0xff;
- if (intVal <
- 0x10) {
- buf.append("0");
- }
- }
- return buf.toString();
- }
- e.printStackTrace();
- }
- return null;
- }
- }
- }
Comments