public class DigestUtils extends Object
MessageDigest
tasks.
This class is immutable and thread-safe.
However the MessageDigest instances it creates generally won't be.
The MessageDigestAlgorithms
class provides constants for standard
digest algorithms that can be used with the getDigest(String)
method
and other methods that require the Digest algorithm name.
Note: the class has short-hand methods for all the algorithms present as standard in Java 6. This approach requires lots of methods for each algorithm, and quickly becomes unwieldy. The following code works with all algorithms:
import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224; ... byte [] digest = new DigestUtils(SHA_224).digest(dataToDigest); String hdigest = new DigestUtils(SHA_224).digestAsHex(new File("pom.xml"));
MessageDigestAlgorithms
Constructor and Description |
---|
DigestUtils(MessageDigest digest)
Creates an instance using the provided
MessageDigest parameter. |
DigestUtils(String name)
Creates an instance using the provided
MessageDigest parameter. |
Modifier and Type | Method and Description |
---|---|
byte[] |
digest(byte[] data)
Reads through a byte array and returns the digest for the data.
|
byte[] |
digest(ByteBuffer data)
Reads through a ByteBuffer and returns the digest for the data
|
byte[] |
digest(File data)
Reads through a File and returns the digest for the data
|
byte[] |
digest(InputStream data)
Reads through an InputStream and returns the digest for the data
|
static byte[] |
digest(MessageDigest messageDigest,
byte[] data)
Reads through a byte array and returns the digest for the data.
|
static byte[] |
digest(MessageDigest messageDigest,
ByteBuffer data)
Reads through a ByteBuffer and returns the digest for the data
|
static byte[] |
digest(MessageDigest messageDigest,
File data)
Reads through a File and returns the digest for the data
|
static byte[] |
digest(MessageDigest messageDigest,
InputStream data)
Reads through an InputStream and returns the digest for the data
|
byte[] |
digest(String data)
Reads through a byte array and returns the digest for the data.
|
String |
digestAsHex(byte[] data)
Reads through a byte array and returns the digest for the data.
|
String |
digestAsHex(ByteBuffer data)
Reads through a ByteBuffer and returns the digest for the data
|
String |
digestAsHex(File data)
Reads through a File and returns the digest for the data
|
String |
digestAsHex(InputStream data)
Reads through an InputStream and returns the digest for the data
|
String |
digestAsHex(String data)
Reads through a byte array and returns the digest for the data.
|
static MessageDigest |
getDigest(String algorithm)
Returns a
MessageDigest for the given algorithm . |
static MessageDigest |
getDigest(String algorithm,
MessageDigest defaultMessageDigest)
Returns a
MessageDigest for the given algorithm or a default if there is a problem
getting the algorithm. |
static MessageDigest |
getMd2Digest()
Returns an MD2 MessageDigest.
|
static MessageDigest |
getMd5Digest()
Returns an MD5 MessageDigest.
|
MessageDigest |
getMessageDigest()
Returns the message digest instance.
|
static MessageDigest |
getSha1Digest()
Returns an SHA-1 digest.
|
static MessageDigest |
getSha256Digest()
Returns an SHA-256 digest.
|
static MessageDigest |
getSha3_224Digest()
Returns an SHA3-224 digest.
|
static MessageDigest |
getSha3_256Digest()
Returns an SHA3-256 digest.
|
static MessageDigest |
getSha3_384Digest()
Returns an SHA3-384 digest.
|
static MessageDigest |
getSha3_512Digest()
Returns an SHA3-512 digest.
|
static MessageDigest |
getSha384Digest()
Returns an SHA-384 digest.
|
static MessageDigest |
getSha512Digest()
Returns an SHA-512 digest.
|
static boolean |
isAvailable(String messageDigestAlgorithm)
Test whether the algorithm is supported.
|
static byte[] |
md2(byte[] data)
Calculates the MD2 digest and returns the value as a 16 element
byte[] . |
static byte[] |
md2(InputStream data)
Calculates the MD2 digest and returns the value as a 16 element
byte[] . |
static byte[] |
md2(String data)
Calculates the MD2 digest and returns the value as a 16 element
byte[] . |
static String |
md2Hex(byte[] data)
Calculates the MD2 digest and returns the value as a 32 character hex string.
|
static String |
md2Hex(InputStream data)
Calculates the MD2 digest and returns the value as a 32 character hex string.
|
static String |
md2Hex(String data)
Calculates the MD2 digest and returns the value as a 32 character hex string.
|
static byte[] |
md5(byte[] data)
Calculates the MD5 digest and returns the value as a 16 element
byte[] . |
static byte[] |
md5(InputStream data)
Calculates the MD5 digest and returns the value as a 16 element
byte[] . |
static byte[] |
md5(String data)
Calculates the MD5 digest and returns the value as a 16 element
byte[] . |
static String |
md5Hex(byte[] data)
Calculates the MD5 digest and returns the value as a 32 character hex string.
|
static String |
md5Hex(InputStream data)
Calculates the MD5 digest and returns the value as a 32 character hex string.
|
static String |
md5Hex(String data)
Calculates the MD5 digest and returns the value as a 32 character hex string.
|
static byte[] |
sha1(byte[] data)
Calculates the SHA-1 digest and returns the value as a
byte[] . |
static byte[] |
sha1(InputStream data)
Calculates the SHA-1 digest and returns the value as a
byte[] . |
static byte[] |
sha1(String data)
Calculates the SHA-1 digest and returns the value as a
byte[] . |
static String |
sha1Hex(byte[] data)
Calculates the SHA-1 digest and returns the value as a hex string.
|
static String |
sha1Hex(InputStream data)
Calculates the SHA-1 digest and returns the value as a hex string.
|
static String |
sha1Hex(String data)
Calculates the SHA-1 digest and returns the value as a hex string.
|
static byte[] |
sha256(byte[] data)
Calculates the SHA-256 digest and returns the value as a
byte[] . |
static byte[] |
sha256(InputStream data)
Calculates the SHA-256 digest and returns the value as a
byte[] . |
static byte[] |
sha256(String data)
Calculates the SHA-256 digest and returns the value as a
byte[] . |
static String |
sha256Hex(byte[] data)
Calculates the SHA-256 digest and returns the value as a hex string.
|
static String |
sha256Hex(InputStream data)
Calculates the SHA-256 digest and returns the value as a hex string.
|
static String |
sha256Hex(String data)
Calculates the SHA-256 digest and returns the value as a hex string.
|
static byte[] |
sha3_224(byte[] data)
Calculates the SHA3-224 digest and returns the value as a
byte[] . |
static byte[] |
sha3_224(InputStream data)
Calculates the SHA3-224 digest and returns the value as a
byte[] . |
static byte[] |
sha3_224(String data)
Calculates the SHA3-224 digest and returns the value as a
byte[] . |
static String |
sha3_224Hex(byte[] data)
Calculates the SHA3-224 digest and returns the value as a hex string.
|
static String |
sha3_224Hex(InputStream data)
Calculates the SHA3-224 digest and returns the value as a hex string.
|
static String |
sha3_224Hex(String data)
Calculates the SHA3-224 digest and returns the value as a hex string.
|
static byte[] |
sha3_256(byte[] data)
Calculates the SHA3-256 digest and returns the value as a
byte[] . |
static byte[] |
sha3_256(InputStream data)
Calculates the SHA3-256 digest and returns the value as a
byte[] . |
static byte[] |
sha3_256(String data)
Calculates the SHA3-256 digest and returns the value as a
byte[] . |
static String |
sha3_256Hex(byte[] data)
Calculates the SHA3-256 digest and returns the value as a hex string.
|
static String |
sha3_256Hex(InputStream data)
Calculates the SHA3-256 digest and returns the value as a hex string.
|
static String |
sha3_256Hex(String data)
Calculates the SHA3-256 digest and returns the value as a hex string.
|
static byte[] |
sha3_384(byte[] data)
Calculates the SHA3-384 digest and returns the value as a
byte[] . |
static byte[] |
sha3_384(InputStream data)
Calculates the SHA3-384 digest and returns the value as a
byte[] . |
static byte[] |
sha3_384(String data)
Calculates the SHA3-384 digest and returns the value as a
byte[] . |
static String |
sha3_384Hex(byte[] data)
Calculates the SHA3-384 digest and returns the value as a hex string.
|
static String |
sha3_384Hex(InputStream data)
Calculates the SHA3-384 digest and returns the value as a hex string.
|
static String |
sha3_384Hex(String data)
Calculates the SHA3-384 digest and returns the value as a hex string.
|
static byte[] |
sha3_512(byte[] data)
Calculates the SHA3-512 digest and returns the value as a
byte[] . |
static byte[] |
sha3_512(InputStream data)
Calculates the SHA3-512 digest and returns the value as a
byte[] . |
static byte[] |
sha3_512(String data)
Calculates the SHA3-512 digest and returns the value as a
byte[] . |
static String |
sha3_512Hex(byte[] data)
Calculates the SHA3-512 digest and returns the value as a hex string.
|
static String |
sha3_512Hex(InputStream data)
Calculates the SHA3-512 digest and returns the value as a hex string.
|
static String |
sha3_512Hex(String data)
Calculates the SHA3-512 digest and returns the value as a hex string.
|
static byte[] |
sha384(byte[] data)
Calculates the SHA-384 digest and returns the value as a
byte[] . |
static byte[] |
sha384(InputStream data)
Calculates the SHA-384 digest and returns the value as a
byte[] . |
static byte[] |
sha384(String data)
Calculates the SHA-384 digest and returns the value as a
byte[] . |
static String |
sha384Hex(byte[] data)
Calculates the SHA-384 digest and returns the value as a hex string.
|
static String |
sha384Hex(InputStream data)
Calculates the SHA-384 digest and returns the value as a hex string.
|
static String |
sha384Hex(String data)
Calculates the SHA-384 digest and returns the value as a hex string.
|
static byte[] |
sha512(byte[] data)
Calculates the SHA-512 digest and returns the value as a
byte[] . |
static byte[] |
sha512(InputStream data)
Calculates the SHA-512 digest and returns the value as a
byte[] . |
static byte[] |
sha512(String data)
Calculates the SHA-512 digest and returns the value as a
byte[] . |
static String |
sha512Hex(byte[] data)
Calculates the SHA-512 digest and returns the value as a hex string.
|
static String |
sha512Hex(InputStream data)
Calculates the SHA-512 digest and returns the value as a hex string.
|
static String |
sha512Hex(String data)
Calculates the SHA-512 digest and returns the value as a hex string.
|
static MessageDigest |
updateDigest(MessageDigest messageDigest,
byte[] valueToDigest)
Updates the given
MessageDigest . |
static MessageDigest |
updateDigest(MessageDigest messageDigest,
ByteBuffer valueToDigest)
Updates the given
MessageDigest . |
static MessageDigest |
updateDigest(MessageDigest digest,
File data)
Reads through a File and updates the digest for the data
|
static MessageDigest |
updateDigest(MessageDigest digest,
InputStream data)
Reads through an InputStream and updates the digest for the data
|
static MessageDigest |
updateDigest(MessageDigest messageDigest,
String valueToDigest)
Updates the given
MessageDigest from a String (converted to bytes using UTF-8). |
public DigestUtils(MessageDigest digest)
MessageDigest
parameter.
This can then be used to create digests using methods such as
digest(byte[])
and digestAsHex(File)
.digest
- the MessageDigest
to usepublic DigestUtils(String name)
MessageDigest
parameter.
This can then be used to create digests using methods such as
digest(byte[])
and digestAsHex(File)
.name
- the name of the MessageDigest
to useIllegalArgumentException
- when a NoSuchAlgorithmException
is caught.getDigest(String)
public byte[] digest(byte[] data)
data
- Data to digestpublic byte[] digest(ByteBuffer data)
data
- Data to digestpublic byte[] digest(File data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic byte[] digest(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static byte[] digest(MessageDigest messageDigest, byte[] data)
messageDigest
- The MessageDigest to use (e.g. MD5)data
- Data to digestpublic static byte[] digest(MessageDigest messageDigest, ByteBuffer data)
messageDigest
- The MessageDigest to use (e.g. MD5)data
- Data to digestpublic static byte[] digest(MessageDigest messageDigest, File data) throws IOException
messageDigest
- The MessageDigest to use (e.g. MD5)data
- Data to digestIOException
- On error reading from the streampublic static byte[] digest(MessageDigest messageDigest, InputStream data) throws IOException
messageDigest
- The MessageDigest to use (e.g. MD5)data
- Data to digestIOException
- On error reading from the streampublic byte[] digest(String data)
data
- Data to digest treated as UTF-8 stringpublic String digestAsHex(byte[] data)
data
- Data to digestpublic String digestAsHex(ByteBuffer data)
data
- Data to digestpublic String digestAsHex(File data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic String digestAsHex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic String digestAsHex(String data)
data
- Data to digest treated as UTF-8 stringpublic static MessageDigest getDigest(String algorithm)
MessageDigest
for the given algorithm
.algorithm
- the name of the algorithm requested. See Appendix A in the Java Cryptography Architecture Reference Guide for information about standard
algorithm names.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught.MessageDigest.getInstance(String)
public static MessageDigest getDigest(String algorithm, MessageDigest defaultMessageDigest)
MessageDigest
for the given algorithm
or a default if there is a problem
getting the algorithm.algorithm
- the name of the algorithm requested. See
Appendix A in the Java Cryptography Architecture Reference Guide for information about standard
algorithm names.defaultMessageDigest
- The default MessageDigest.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught.MessageDigest.getInstance(String)
public static MessageDigest getMd2Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should never happen because MD2 is a
built-in algorithmMessageDigestAlgorithms.MD2
public static MessageDigest getMd5Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should never happen because MD5 is a
built-in algorithmMessageDigestAlgorithms.MD5
public MessageDigest getMessageDigest()
public static MessageDigest getSha1Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should never happen because SHA-1 is a
built-in algorithmMessageDigestAlgorithms.SHA_1
public static MessageDigest getSha256Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should never happen because SHA-256 is a
built-in algorithmMessageDigestAlgorithms.SHA_256
public static MessageDigest getSha3_224Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should not happen on
Oracle Java 9 andgreater.MessageDigestAlgorithms.SHA3_224
public static MessageDigest getSha3_256Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should not happen on
Oracle Java 9 and greater.MessageDigestAlgorithms.SHA3_256
public static MessageDigest getSha3_384Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should not happen on
Oracle Java 9 and greater.MessageDigestAlgorithms.SHA3_384
public static MessageDigest getSha3_512Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should not happen
on Oracle Java 9 and greater.MessageDigestAlgorithms.SHA3_512
public static MessageDigest getSha384Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should never happen
because SHA-384 is a built-in algorithmMessageDigestAlgorithms.SHA_384
public static MessageDigest getSha512Digest()
IllegalArgumentException
- when a NoSuchAlgorithmException
is caught, which should never happen
because SHA-512 is a built-in algorithmMessageDigestAlgorithms.SHA_512
public static boolean isAvailable(String messageDigestAlgorithm)
messageDigestAlgorithm
- the algorithm nametrue
if the algorithm can be foundpublic static byte[] md2(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] md2(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] md2(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String md2Hex(byte[] data)
data
- Data to digestpublic static String md2Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String md2Hex(String data)
data
- Data to digestpublic static byte[] md5(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] md5(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] md5(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String md5Hex(byte[] data)
data
- Data to digestpublic static String md5Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String md5Hex(String data)
data
- Data to digestpublic static byte[] sha1(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] sha1(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] sha1(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String sha1Hex(byte[] data)
data
- Data to digestpublic static String sha1Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String sha1Hex(String data)
data
- Data to digestpublic static byte[] sha256(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] sha256(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] sha256(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String sha256Hex(byte[] data)
data
- Data to digestpublic static String sha256Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String sha256Hex(String data)
data
- Data to digestpublic static byte[] sha3_224(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] sha3_224(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] sha3_224(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String sha3_224Hex(byte[] data)
data
- Data to digestpublic static String sha3_224Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String sha3_224Hex(String data)
data
- Data to digestpublic static byte[] sha3_256(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] sha3_256(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] sha3_256(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String sha3_256Hex(byte[] data)
data
- Data to digestpublic static String sha3_256Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String sha3_256Hex(String data)
data
- Data to digestpublic static byte[] sha3_384(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] sha3_384(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] sha3_384(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String sha3_384Hex(byte[] data)
data
- Data to digestpublic static String sha3_384Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String sha3_384Hex(String data)
data
- Data to digestpublic static byte[] sha3_512(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] sha3_512(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] sha3_512(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String sha3_512Hex(byte[] data)
data
- Data to digestpublic static String sha3_512Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String sha3_512Hex(String data)
data
- Data to digestpublic static byte[] sha384(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] sha384(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] sha384(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String sha384Hex(byte[] data)
data
- Data to digestpublic static String sha384Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String sha384Hex(String data)
data
- Data to digestpublic static byte[] sha512(byte[] data)
byte[]
.data
- Data to digestpublic static byte[] sha512(InputStream data) throws IOException
byte[]
.data
- Data to digestIOException
- On error reading from the streampublic static byte[] sha512(String data)
byte[]
.data
- Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
public static String sha512Hex(byte[] data)
data
- Data to digestpublic static String sha512Hex(InputStream data) throws IOException
data
- Data to digestIOException
- On error reading from the streampublic static String sha512Hex(String data)
data
- Data to digestpublic static MessageDigest updateDigest(MessageDigest messageDigest, byte[] valueToDigest)
MessageDigest
.messageDigest
- the MessageDigest
to updatevalueToDigest
- the value to update the MessageDigest
withMessageDigest
public static MessageDigest updateDigest(MessageDigest messageDigest, ByteBuffer valueToDigest)
MessageDigest
.messageDigest
- the MessageDigest
to updatevalueToDigest
- the value to update the MessageDigest
withMessageDigest
public static MessageDigest updateDigest(MessageDigest digest, File data) throws IOException
digest
- The MessageDigest to use (e.g. MD5)data
- Data to digestIOException
- On error reading from the streampublic static MessageDigest updateDigest(MessageDigest digest, InputStream data) throws IOException
digest
- The MessageDigest to use (e.g. MD5)data
- Data to digestIOException
- On error reading from the streampublic static MessageDigest updateDigest(MessageDigest messageDigest, String valueToDigest)
MessageDigest
from a String (converted to bytes using UTF-8).
To update the digest using a different charset for the conversion,
convert the String to a byte array using
String#getBytes(java.nio.charset.Charset)
and pass that
to the updateDigest(MessageDigest, byte[])
method
messageDigest
- the MessageDigest
to updatevalueToDigest
- the value to update the MessageDigest
with;
converted to bytes using StringUtils.getBytesUtf8(String)
MessageDigest