Skip to content

Instantly share code, notes, and snippets.

@eytanbiala
Created November 22, 2013 23:36
Show Gist options
  • Save eytanbiala/7608702 to your computer and use it in GitHub Desktop.
Save eytanbiala/7608702 to your computer and use it in GitHub Desktop.
Calculates the MD5 hash of str using the RSA MD5 Message-Digest Algorithm, and returns that hash.
import java.security.*;
public class Secure {
public static void main(String[] args) {
final String pass = "password";
String encrypted = MD5(pass);
System.out.println("Encrypted password: " + encrypted);
}
public static String MD5(String md5) {
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(md5.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; i++) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
}
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment