Skip to content

Instantly share code, notes, and snippets.

@queerzard
Created January 5, 2023 13:28
Show Gist options
  • Save queerzard/5304cc28080cbc6bdc0933a7a73de659 to your computer and use it in GitHub Desktop.
Save queerzard/5304cc28080cbc6bdc0933a7a73de659 to your computer and use it in GitHub Desktop.
Hash Methods
private static String hash(String hashType, String inputHash) throws NoSuchAlgorithmException, IOException {
MessageDigest messageDigest = MessageDigest.getInstance(hashType);
messageDigest.update(inputHash.getBytes());
byte[] digest = messageDigest.digest();
StringBuffer stringBuffer = new StringBuffer();
for (byte byt : digest)
stringBuffer.append(String.format("%02x", byt & 0xff));
return stringBuffer.toString();
}
@SneakyThrows public static String md5(String input){return hash("MD5", input);}
@SneakyThrows public static String sha1(String input){return hash("SHA-1", input);}
@SneakyThrows public static String sha256(String input){return hash("SHA-256", input);}
@SneakyThrows public static String sha384(String input){return hash("SHA-384", input);}
@SneakyThrows public static String sha512(String input){return hash("SHA-512", input);}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment