Saturday, May 9, 2009

Hashing

This method will give u the out put according to the input string and the algorithm
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Method getHashText.
* @param plainText
* @param algorithm The algorithm to use like MD2, MD5, SHA-1, etc.
* @return String
* @throws NoSuchAlgorithmException
*/

public static String getHashText(String plainText, String algorithm)
throws NoSuchAlgorithmException {
MessageDigest mdAlgorithm = MessageDigest.getInstance(algorithm);

mdAlgorithm.update(plainText.getBytes());

byte[] digest = mdAlgorithm.digest();
StringBuffer hexString = new StringBuffer();

for (int i = 0; i < digest.length; i++) {
plainText = Integer.toHexString(0xFF & digest[i]);

if (plainText.length() < 2) {
plainText = "0" + plainText;
}

hexString.append(plainText);
}

return hexString.toString();
}

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails