public static String hmacDigest(String message, String secretKey, String algorithm) { String digest = null; try { SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(key); byte[] bytes = mac.doFinal(message.getBytes(StandardCharsets.US_ASCII)); digest = toHex(bytes); } catch (InvalidKeyException e) { } catch (NoSuchAlgorithmException e) { } return digest; }
/** * Calculates the SHA1 hash of the byte array. * * @param bytes * @return sha1 hash of the byte array */ public static String getHashSHA1(byte[] bytes) { try { MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(bytes, 0, bytes.length); byte[] digest = md.digest(); return toHex(digest); } catch (NoSuchAlgorithmException t) { throw new RuntimeException(t); } }
/** * Calculates the SHA256 hash of the byte array. * * @param bytes * @return sha256 hash of the byte array */ public static String getHashSHA256(byte[] bytes) { try { MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(bytes, 0, bytes.length); byte[] digest = md.digest(); return toHex(digest); } catch (NoSuchAlgorithmException t) { throw new RuntimeException(t); } }
/** * Calculates the MD5 hash of the byte array. * * @param bytes * @return md5 hash of the byte array */ public static String getHashMD5(byte[] bytes) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(bytes, 0, bytes.length); byte[] digest = md.digest(); return toHex(digest); } catch (NoSuchAlgorithmException t) { throw new RuntimeException(t); } }