Package org.rapidcontext.util
Class BinaryUtil
java.lang.Object
org.rapidcontext.util.BinaryUtil
A set of utility methods for handling binary data.
- Version:
- 1.0
-
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[]Performs a digest hash on the specified byte array.static byte[]hashBytes(String alg, InputStream input) Performs 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 UTF-8 encoding of an input file.static StringhashSHA256(String input) Calculates the SHA-256 digest hash on the UTF-8 encoding of an input string.
-
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 UTF-8 encoding 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
-
hashBytes
Performs a digest hash on the specified byte array.- Parameters:
alg- the hash algorithm (e.g. "MD5" or "SHA-256")data- the data to hash- Returns:
- the digest hash of the data
- Throws:
NoSuchAlgorithmException- if the hash algorithm isn't available
-
hashBytes
public static byte[] hashBytes(String alg, InputStream input) throws NoSuchAlgorithmException, IOException Performs a digest hash on the data from an input stream.- Parameters:
alg- the hash algorithm (e.g. "MD5" or "SHA-256")input- 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
-
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
-