/** * Returns the raw {@code byte[]} hash of the specified value. The default charset ({@code StandardCharsets.UTF_8}) is used. * * @param algorithm the hash algorithm to use * @param value the value to hash (cannot be {@code null} but can be an empty String) * @return the hash value in bytes */ public static byte[] hashValueRaw(HashAlgorithm algorithm, String value) { return hashValueRaw(algorithm, value, StandardCharsets.UTF_8); }
/** * Returns the hex-encoded hash of the specified value. * * @param algorithm the hash algorithm to use * @param value the value to hash (cannot be {@code null} but can be an empty String) * @param charset the charset to use * @return the hash value in hex */ public static String hashValue(HashAlgorithm algorithm, String value, Charset charset) { byte[] rawHash = hashValueRaw(algorithm, value, charset); return Hex.encodeHexString(rawHash); }
/** * Returns the raw {@code byte[]} hash of the specified value. * * @param algorithm the hash algorithm to use * @param value the value to hash (cannot be {@code null} but can be an empty String) * @param charset the charset to use * @return the hash value in bytes */ public static byte[] hashValueRaw(HashAlgorithm algorithm, String value, Charset charset) { if (value == null) { throw new IllegalArgumentException("The value cannot be null"); } /** See the note on {@link HashServiceTest#testHashValueShouldHandleUTF16BOMIssue()} */ if (charset == StandardCharsets.UTF_16) { logger.warn("The charset provided was UTF-16, but Java will insert a Big Endian BOM in the decoded message before hashing, so switching to UTF-16BE"); charset = StandardCharsets.UTF_16BE; } return hashValueRaw(algorithm, value.getBytes(charset)); }
/** * Returns the raw {@code byte[]} hash of the specified value. The default charset ({@code StandardCharsets.UTF_8}) is used. * * @param algorithm the hash algorithm to use * @param value the value to hash (cannot be {@code null} but can be an empty String) * @return the hash value in bytes */ public static byte[] hashValueRaw(HashAlgorithm algorithm, String value) { return hashValueRaw(algorithm, value, StandardCharsets.UTF_8); }
/** * Returns the hex-encoded hash of the specified value. * * @param algorithm the hash algorithm to use * @param value the value to hash (cannot be {@code null} but can be an empty String) * @param charset the charset to use * @return the hash value in hex */ public static String hashValue(HashAlgorithm algorithm, String value, Charset charset) { byte[] rawHash = hashValueRaw(algorithm, value, charset); return Hex.encodeHexString(rawHash); }
/** * Returns the raw {@code byte[]} hash of the specified value. * * @param algorithm the hash algorithm to use * @param value the value to hash (cannot be {@code null} but can be an empty String) * @param charset the charset to use * @return the hash value in bytes */ public static byte[] hashValueRaw(HashAlgorithm algorithm, String value, Charset charset) { if (value == null) { throw new IllegalArgumentException("The value cannot be null"); } /** See the note on {@link HashServiceTest#testHashValueShouldHandleUTF16BOMIssue()} */ if (charset == StandardCharsets.UTF_16) { logger.warn("The charset provided was UTF-16, but Java will insert a Big Endian BOM in the decoded message before hashing, so switching to UTF-16BE"); charset = StandardCharsets.UTF_16BE; } return hashValueRaw(algorithm, value.getBytes(charset)); }