Skip to content

Instantly share code, notes, and snippets.

@gtomek
Created September 3, 2018 15:52
Show Gist options
  • Select an option

  • Save gtomek/c7d0439ec7a975b4bcf43af57d6742f0 to your computer and use it in GitHub Desktop.

Select an option

Save gtomek/c7d0439ec7a975b4bcf43af57d6742f0 to your computer and use it in GitHub Desktop.
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class JavaMD5 {
public static void main(String[] args) {
String passwordToHash = "MyPassword123";
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(passwordToHash.getBytes());
byte[] bytes = md.digest();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
System.out.println(generatedPassword);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment