Calculate SHA1 and MD5 in Java
import java.security.MessageDigest;
import java.util.Formatter;
public class HashFunctionTest {
public static String calculateHash(MessageDigest algorithm,
String message) throws Exception{
algorithm.update(message.getBytes());
byte[] hash = algorithm.digest();
return byteArray2Hex(hash);
}
private static String byteArray2Hex(byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static void main(String[] args) throws Exception {
String message = "elmurod talipov";
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
MessageDigest md5 = MessageDigest.getInstance("MD5");
System.out.println(calculateHash(sha1, message));
System.out.println(calculateHash(md5, message));
}
}