/** * Returns the raw {@code byte[]} hash of the specified value. * * @param algorithm the hash algorithm to use * @param value the value to hash * @return the hash value in bytes */ public static byte[] hashValueRaw(HashAlgorithm algorithm, byte[] value) { if (algorithm == null) { throw new IllegalArgumentException("The hash algorithm cannot be null"); } if (value == null) { throw new IllegalArgumentException("The value cannot be null"); } if (algorithm.isBlake2()) { return blake2Hash(algorithm, value); } else { return traditionalHash(algorithm, value); } }
/** * Returns the hash of the specified value. This method uses an {@link java.io.InputStream} to perform the operation in a streaming manner for large inputs. * * @param algorithm the hash algorithm to use * @param value the value to hash (cannot be {@code null} but can be an empty stream) * @return the hash value in hex */ public static String hashValueStreaming(HashAlgorithm algorithm, InputStream value) throws IOException { if (algorithm == null) { throw new IllegalArgumentException("The hash algorithm cannot be null"); } if (value == null) { throw new IllegalArgumentException("The value cannot be null"); } // The Blake2 algorithms are instantiated differently and rely on BouncyCastle if (algorithm.isBlake2()) { return Hex.encodeHexString(blake2HashStreaming(algorithm, value)); } else { return Hex.encodeHexString(traditionalHashStreaming(algorithm, value)); } }
/** * Returns an array of {@link AllowableValue} elements for each {@link Charset}. Only the charsets in {@link StandardCharsets} are returned to be consistent across JVM instances. * * @return an ordered {@code AllowableValue[]} containing the values */ public static AllowableValue[] buildCharacterSetAllowableValues() { final List<Charset> charsets = getSupportedCharsets(); return charsets.stream().map(cs -> new AllowableValue(cs.name(), cs.displayName(), cs == StandardCharsets.UTF_16 ? UTF_16_DESCRIPTION : cs.displayName()) ).toArray(AllowableValue[]::new); }
/** * 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. 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 hex */ public static String hashValue(HashAlgorithm algorithm, String value) { return hashValue(algorithm, value, StandardCharsets.UTF_8); }
session.read(flowFile, in -> hashValueHolder.set(HashService.hashValueStreaming(algorithm, in)));
/** * 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); }
private String hashValue(HashAlgorithm algorithm, String value, Charset charset) { if (value == null) { getLogger().warn("Tried to calculate {} hash of null value; returning empty string", new Object[]{algorithm.getName()}); return ""; } return HashService.hashValue(algorithm, value, charset); }
session.read(flowFile, in -> hashValueHolder.set(HashService.hashValueStreaming(algorithm, in)));
/** * Returns the hash of the specified value. This method uses an {@link java.io.InputStream} to perform the operation in a streaming manner for large inputs. * * @param algorithm the hash algorithm to use * @param value the value to hash (cannot be {@code null} but can be an empty stream) * @return the hash value in hex */ public static String hashValueStreaming(HashAlgorithm algorithm, InputStream value) throws IOException { if (algorithm == null) { throw new IllegalArgumentException("The hash algorithm cannot be null"); } if (value == null) { throw new IllegalArgumentException("The value cannot be null"); } // The Blake2 algorithms are instantiated differently and rely on BouncyCastle if (algorithm.isBlake2()) { return Hex.encodeHexString(blake2HashStreaming(algorithm, value)); } else { return Hex.encodeHexString(traditionalHashStreaming(algorithm, value)); } }
/** * Returns the raw {@code byte[]} hash of the specified value. * * @param algorithm the hash algorithm to use * @param value the value to hash * @return the hash value in bytes */ public static byte[] hashValueRaw(HashAlgorithm algorithm, byte[] value) { if (algorithm == null) { throw new IllegalArgumentException("The hash algorithm cannot be null"); } if (value == null) { throw new IllegalArgumentException("The value cannot be null"); } if (algorithm.isBlake2()) { return blake2Hash(algorithm, value); } else { return traditionalHash(algorithm, value); } }
/** * 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 hex-encoded 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 hex */ public static String hashValue(HashAlgorithm algorithm, String value) { return hashValue(algorithm, value, StandardCharsets.UTF_8); }
/** * Returns an array of {@link AllowableValue} elements for each {@link Charset}. Only the charsets in {@link StandardCharsets} are returned to be consistent across JVM instances. * * @return an ordered {@code AllowableValue[]} containing the values */ public static AllowableValue[] buildCharacterSetAllowableValues() { final List<Charset> charsets = getSupportedCharsets(); return charsets.stream().map(cs -> new AllowableValue(cs.name(), cs.displayName(), cs == StandardCharsets.UTF_16 ? UTF_16_DESCRIPTION : cs.displayName()) ).toArray(AllowableValue[]::new); }
/** * 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); }
private String hashValue(HashAlgorithm algorithm, String value, Charset charset) { if (value == null) { getLogger().warn("Tried to calculate {} hash of null value; returning empty string", new Object[]{algorithm.getName()}); return ""; } return HashService.hashValue(algorithm, value, charset); }
/** * 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)); }