Package io.github.connellite.util
Class DigestUtils
java.lang.Object
io.github.connellite.util.DigestUtils
Digest helpers for streaming input.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]base64(byte[] data) Encodesdataas Base64.static byte[]base64(InputStream in) Encodes all bytes frominas Base64.static Stringbase64String(byte[] data) Encodesdataas a Base64 string.static StringEncodes all bytes frominas a Base64 string.static byte[]Computes a message digest fordatausing the given algorithm.static byte[]digest(InputStream in, String algorithm) Computes a message digest for all bytes frominusing the given algorithm.static StringComputes a hex-encoded message digest fordatausing the given algorithm.static StringdigestHex(InputStream in, String algorithm) Computes a hex-encoded message digest for all bytes frominusing the given algorithm.static byte[]fromBase64(byte[] base64) Decodes Base64-encoded bytes into raw bytes.static byte[]fromBase64(String base64) Decodes a Base64-encoded string into bytes.static byte[]Decodes a lowercase/uppercase hex string into bytes.static byte[]md5(byte[] data) Convenience method for computing an MD5 digest.static byte[]md5(InputStream in) Convenience method for computing an MD5 digest.static Stringmd5Hex(byte[] data) Convenience method for computing an MD5 digest as lowercase hex string.static Stringmd5Hex(InputStream in) Convenience method for computing an MD5 digest as lowercase hex string.static byte[]sha256(byte[] data) Convenience method for computing a SHA-256 digest.static byte[]sha256(InputStream in) Convenience method for computing a SHA-256 digest.static Stringsha256Hex(byte[] data) Convenience method for computing a SHA-256 digest as lowercase hex string.static Stringsha256Hex(InputStream in) Convenience method for computing a SHA-256 digest as lowercase hex string.
-
Constructor Details
-
DigestUtils
public DigestUtils()
-
-
Method Details
-
digest
public static byte[] digest(InputStream in, String algorithm) throws IOException, NoSuchAlgorithmException Computes a message digest for all bytes frominusing the given algorithm.Does not close
in.- Parameters:
in- source stream; must not benullalgorithm- algorithm name understood byMessageDigest(e.g.MD5,SHA-256)- Returns:
- digest bytes
- Throws:
IOException- if reading failsNoSuchAlgorithmException- if the algorithm is not available
-
digest
Computes a message digest fordatausing the given algorithm.- Parameters:
data- raw bytes; must not benullalgorithm- algorithm name understood byMessageDigest(e.g.MD5,SHA-256)- Returns:
- digest bytes
- Throws:
NoSuchAlgorithmException- if the algorithm is not available
-
digestHex
public static String digestHex(InputStream in, String algorithm) throws IOException, NoSuchAlgorithmException Computes a hex-encoded message digest for all bytes frominusing the given algorithm.Does not close
in.- Parameters:
in- source stream; must not benullalgorithm- algorithm name understood byMessageDigest(e.g.MD5,SHA-256)- Returns:
- lowercase hex string of the digest
- Throws:
IOException- if reading failsNoSuchAlgorithmException- if the algorithm is not available
-
digestHex
Computes a hex-encoded message digest fordatausing the given algorithm.- Parameters:
data- raw bytes; must not benullalgorithm- algorithm name understood byMessageDigest(e.g.MD5,SHA-256)- Returns:
- lowercase hex string of the digest
- Throws:
NoSuchAlgorithmException- if the algorithm is not available
-
md5
Convenience method for computing an MD5 digest.- Throws:
IOExceptionNoSuchAlgorithmException
-
md5
Convenience method for computing an MD5 digest.- Throws:
NoSuchAlgorithmException
-
md5Hex
Convenience method for computing an MD5 digest as lowercase hex string.- Throws:
IOExceptionNoSuchAlgorithmException
-
md5Hex
Convenience method for computing an MD5 digest as lowercase hex string.- Throws:
NoSuchAlgorithmException
-
sha256
Convenience method for computing a SHA-256 digest.- Throws:
IOExceptionNoSuchAlgorithmException
-
sha256
Convenience method for computing a SHA-256 digest.- Throws:
NoSuchAlgorithmException
-
sha256Hex
Convenience method for computing a SHA-256 digest as lowercase hex string.- Throws:
IOExceptionNoSuchAlgorithmException
-
sha256Hex
Convenience method for computing a SHA-256 digest as lowercase hex string.- Throws:
NoSuchAlgorithmException
-
base64
Encodes all bytes frominas Base64.Does not close
in.- Parameters:
in- source stream; must not benull- Returns:
- Base64-encoded bytes
- Throws:
IOException- if reading fails
-
base64
public static byte[] base64(byte[] data) Encodesdataas Base64.- Parameters:
data- raw bytes; must not benull- Returns:
- Base64-encoded bytes
-
base64String
Encodes all bytes frominas a Base64 string.Does not close
in.- Parameters:
in- source stream; must not benull- Returns:
- Base64 string
- Throws:
IOException- if reading fails
-
base64String
Encodesdataas a Base64 string.- Parameters:
data- raw bytes; must not benull- Returns:
- Base64 string
-
fromHex
Decodes a lowercase/uppercase hex string into bytes.- Parameters:
hex- hex string; length must be even- Returns:
- decoded bytes
- Throws:
IllegalArgumentException- if the string has odd length or contains non-hex characters
-
fromBase64
Decodes a Base64-encoded string into bytes.- Parameters:
base64- Base64 string- Returns:
- decoded bytes
- Throws:
IllegalArgumentException- if the input is not valid Base64
-
fromBase64
public static byte[] fromBase64(byte[] base64) Decodes Base64-encoded bytes into raw bytes.- Parameters:
base64- Base64-encoded data (typically ASCII)- Returns:
- decoded bytes
- Throws:
IllegalArgumentException- if the input is not valid Base64
-