Package org.rapidcontext.util
Class BinaryUtil
java.lang.Object
org.rapidcontext.util.BinaryUtil
A set of utility methods for handling binary data.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe currently supported hash algorithms. -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]decodeBase64(String data) Decodes a Base64 string back to the original byte array.static StringencodeBase64(byte[] data) Encodes a byte array to a string with Base64 characters (websafe).static StringencodeHexString(byte[] data) Encodes a byte array to a string with hexadecimal numbers.static byte[]Calculates a digest hash on the specified byte array.static byte[]hashBytes(String alg, InputStream input) Calculates a digest hash on the data from an input stream.static StringCalculates the MD5 digest hash on the UTF-8 encoding of an input string.static StringhashSHA256(InputStream input) Calculates the SHA-256 digest hash on the bytes of an input file.static StringhashSHA256(String input) Calculates the SHA-256 digest hash on the UTF-8 encoding of an input string.static StringhashSHA3(InputStream input) Calculates the SHA3-256 digest hash on the bytes of an input file.static StringCalculates the SHA3-256 digest hash on the UTF-8 encoding of an input string.static byte[]hmacSHA256(byte[] secret, byte[] data) Calculates the HMAC-SHA256 hash of the specified data using the provided secret key.static byte[]hmacSHA256(String secret, String data) Calculates the HMAC-SHA256 hash of the specified data using the provided secret key.
-
Method Details
-
hashMD5
Calculates the MD5 digest hash on the UTF-8 encoding of an input string. The result will be returned as an hexadecimal string.- Parameters:
input- the input string- Returns:
- the hexadecimal string with the MD5 hash
- Throws:
NoSuchAlgorithmException- if the MD5 algorithm isn't available (should be RuntimeException)
-
hashSHA256
Calculates the SHA-256 digest hash on the UTF-8 encoding of an input string. The result will be returned as an hexadecimal string.- Parameters:
input- the input string- Returns:
- the hexadecimal string with the SHA-256 hash
- Throws:
NoSuchAlgorithmException- if the SHA-256 algorithm isn't available (should be RuntimeException)
-
hashSHA256
Calculates the SHA-256 digest hash on the bytes of an input file. The result will be returned as an hexadecimal string.- Parameters:
input- the input stream to read- Returns:
- the hexadecimal string with the SHA-256 hash
- Throws:
NoSuchAlgorithmException- if the SHA-256 algorithm isn't available (should be RuntimeException)IOException- if the file couldn't be found or read- See Also:
-
hashSHA3
Calculates the SHA3-256 digest hash on the UTF-8 encoding of an input string. The result will be returned as an hexadecimal string.- Parameters:
input- the input string- Returns:
- the hexadecimal string with the SHA3-256 hash
- Throws:
NoSuchAlgorithmException- if the SHA3-256 algorithm isn't available (should be RuntimeException)- See Also:
-
hashSHA3
Calculates the SHA3-256 digest hash on the bytes of an input file. The result will be returned as an hexadecimal string.- Parameters:
input- the input stream to read- Returns:
- the hexadecimal string with the SHA-256 hash
- Throws:
NoSuchAlgorithmException- if the SHA-256 algorithm isn't available (should be RuntimeException)IOException- if the file couldn't be found or read- See Also:
-
hashBytes
Calculates a digest hash on the specified byte array.- Parameters:
alg- the supported hash algorithmdata- the data to hash- Returns:
- the digest hash of the data
- Throws:
NoSuchAlgorithmException- if the hash algorithm isn't available- See Also:
-
hashBytes
public static byte[] hashBytes(String alg, InputStream input) throws NoSuchAlgorithmException, IOException Calculates a digest hash on the data from an input stream.- Parameters:
alg- the supported hash algorithminput- the input stream to read- Returns:
- the digest hash of the data
- Throws:
NoSuchAlgorithmException- if the hash algorithm isn't availableIOException- if the input stream couldn't be read- See Also:
-
hmacSHA256
Calculates the HMAC-SHA256 hash of the specified data using the provided secret key.- Parameters:
secret- the secret keydata- the data to hash- Returns:
- the HMAC-SHA256 hash
- Throws:
SecurityException- if the mac calculation failed
-
hmacSHA256
Calculates the HMAC-SHA256 hash of the specified data using the provided secret key.- Parameters:
secret- the secret keydata- the data to hash- Returns:
- the HMAC-SHA256 hash
- Throws:
SecurityException- if the mac calculation failed
-
encodeHexString
Encodes a byte array to a string with hexadecimal numbers.- Parameters:
data- the byte array- Returns:
- the hexadecimal string with the converted data
-
encodeBase64
Encodes a byte array to a string with Base64 characters (websafe).- Parameters:
data- the byte array- Returns:
- the Base64 string with the converted data
-
decodeBase64
Decodes a Base64 string back to the original byte array.- Parameters:
data- the Base64-encoded string- Returns:
- the decoded original byte array
-