Class DigestUtils
- java.lang.Object
-
- org.apache.commons.codec.digest.DigestUtils
-
public class DigestUtils extends java.lang.ObjectOperations to simplify commonMessageDigesttasks. This class is immutable and thread-safe.- Version:
- $Id: DigestUtils.java 1634433 2014-10-27 01:10:47Z ggregory $
-
-
Field Summary
Fields Modifier and Type Field Description private static intSTREAM_BUFFER_LENGTH
-
Constructor Summary
Constructors Constructor Description DigestUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static byte[]digest(java.security.MessageDigest digest, java.io.InputStream data)Read through an InputStream and returns the digest for the datastatic java.security.MessageDigestgetDigest(java.lang.String algorithm)Returns aMessageDigestfor the givenalgorithm.static java.security.MessageDigestgetMd2Digest()Returns an MD2 MessageDigest.static java.security.MessageDigestgetMd5Digest()Returns an MD5 MessageDigest.static java.security.MessageDigestgetSha1Digest()Returns an SHA-1 digest.static java.security.MessageDigestgetSha256Digest()Returns an SHA-256 digest.static java.security.MessageDigestgetSha384Digest()Returns an SHA-384 digest.static java.security.MessageDigestgetSha512Digest()Returns an SHA-512 digest.static java.security.MessageDigestgetShaDigest()Deprecated.UsegetSha1Digest()static byte[]md2(byte[] data)Calculates the MD2 digest and returns the value as a 16 elementbyte[].static byte[]md2(java.io.InputStream data)Calculates the MD2 digest and returns the value as a 16 elementbyte[].static byte[]md2(java.lang.String data)Calculates the MD2 digest and returns the value as a 16 elementbyte[].static java.lang.Stringmd2Hex(byte[] data)Calculates the MD2 digest and returns the value as a 32 character hex string.static java.lang.Stringmd2Hex(java.io.InputStream data)Calculates the MD2 digest and returns the value as a 32 character hex string.static java.lang.Stringmd2Hex(java.lang.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 elementbyte[].static byte[]md5(java.io.InputStream data)Calculates the MD5 digest and returns the value as a 16 elementbyte[].static byte[]md5(java.lang.String data)Calculates the MD5 digest and returns the value as a 16 elementbyte[].static java.lang.Stringmd5Hex(byte[] data)Calculates the MD5 digest and returns the value as a 32 character hex string.static java.lang.Stringmd5Hex(java.io.InputStream data)Calculates the MD5 digest and returns the value as a 32 character hex string.static java.lang.Stringmd5Hex(java.lang.String data)Calculates the MD5 digest and returns the value as a 32 character hex string.static byte[]sha(byte[] data)Deprecated.Usesha1(byte[])static byte[]sha(java.io.InputStream data)Deprecated.static byte[]sha(java.lang.String data)Deprecated.Usesha1(String)static byte[]sha1(byte[] data)Calculates the SHA-1 digest and returns the value as abyte[].static byte[]sha1(java.io.InputStream data)Calculates the SHA-1 digest and returns the value as abyte[].static byte[]sha1(java.lang.String data)Calculates the SHA-1 digest and returns the value as abyte[].static java.lang.Stringsha1Hex(byte[] data)Calculates the SHA-1 digest and returns the value as a hex string.static java.lang.Stringsha1Hex(java.io.InputStream data)Calculates the SHA-1 digest and returns the value as a hex string.static java.lang.Stringsha1Hex(java.lang.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 abyte[].static byte[]sha256(java.io.InputStream data)Calculates the SHA-256 digest and returns the value as abyte[].static byte[]sha256(java.lang.String data)Calculates the SHA-256 digest and returns the value as abyte[].static java.lang.Stringsha256Hex(byte[] data)Calculates the SHA-256 digest and returns the value as a hex string.static java.lang.Stringsha256Hex(java.io.InputStream data)Calculates the SHA-256 digest and returns the value as a hex string.static java.lang.Stringsha256Hex(java.lang.String data)Calculates the SHA-256 digest and returns the value as a hex string.static byte[]sha384(byte[] data)Calculates the SHA-384 digest and returns the value as abyte[].static byte[]sha384(java.io.InputStream data)Calculates the SHA-384 digest and returns the value as abyte[].static byte[]sha384(java.lang.String data)Calculates the SHA-384 digest and returns the value as abyte[].static java.lang.Stringsha384Hex(byte[] data)Calculates the SHA-384 digest and returns the value as a hex string.static java.lang.Stringsha384Hex(java.io.InputStream data)Calculates the SHA-384 digest and returns the value as a hex string.static java.lang.Stringsha384Hex(java.lang.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 abyte[].static byte[]sha512(java.io.InputStream data)Calculates the SHA-512 digest and returns the value as abyte[].static byte[]sha512(java.lang.String data)Calculates the SHA-512 digest and returns the value as abyte[].static java.lang.Stringsha512Hex(byte[] data)Calculates the SHA-512 digest and returns the value as a hex string.static java.lang.Stringsha512Hex(java.io.InputStream data)Calculates the SHA-512 digest and returns the value as a hex string.static java.lang.Stringsha512Hex(java.lang.String data)Calculates the SHA-512 digest and returns the value as a hex string.static java.lang.StringshaHex(byte[] data)Deprecated.Usesha1Hex(byte[])static java.lang.StringshaHex(java.io.InputStream data)Deprecated.static java.lang.StringshaHex(java.lang.String data)Deprecated.Usesha1Hex(String)static java.security.MessageDigestupdateDigest(java.security.MessageDigest messageDigest, byte[] valueToDigest)Updates the givenMessageDigest.static java.security.MessageDigestupdateDigest(java.security.MessageDigest digest, java.io.InputStream data)Reads through an InputStream and updates the digest for the datastatic java.security.MessageDigestupdateDigest(java.security.MessageDigest messageDigest, java.lang.String valueToDigest)Updates the givenMessageDigest.
-
-
-
Field Detail
-
STREAM_BUFFER_LENGTH
private static final int STREAM_BUFFER_LENGTH
- See Also:
- Constant Field Values
-
-
Method Detail
-
digest
private static byte[] digest(java.security.MessageDigest digest, java.io.InputStream data) throws java.io.IOExceptionRead through an InputStream and returns the digest for the data- Parameters:
digest- The MessageDigest to use (e.g. MD5)data- Data to digest- Returns:
- the digest
- Throws:
java.io.IOException- On error reading from the stream
-
getDigest
public static java.security.MessageDigest getDigest(java.lang.String algorithm)
Returns aMessageDigestfor the givenalgorithm.- Parameters:
algorithm- the name of the algorithm requested. See Appendix A in the Java Cryptography Architecture Reference Guide for information about standard algorithm names.- Returns:
- A digest instance.
- Throws:
java.lang.IllegalArgumentException- when aNoSuchAlgorithmExceptionis caught.- See Also:
MessageDigest.getInstance(String)
-
getMd2Digest
public static java.security.MessageDigest getMd2Digest()
Returns an MD2 MessageDigest.- Returns:
- An MD2 digest instance.
- Throws:
java.lang.IllegalArgumentException- when aNoSuchAlgorithmExceptionis caught, which should never happen because MD2 is a built-in algorithm- Since:
- 1.7
- See Also:
MessageDigestAlgorithms.MD2
-
getMd5Digest
public static java.security.MessageDigest getMd5Digest()
Returns an MD5 MessageDigest.- Returns:
- An MD5 digest instance.
- Throws:
java.lang.IllegalArgumentException- when aNoSuchAlgorithmExceptionis caught, which should never happen because MD5 is a built-in algorithm- See Also:
MessageDigestAlgorithms.MD5
-
getSha1Digest
public static java.security.MessageDigest getSha1Digest()
Returns an SHA-1 digest.- Returns:
- An SHA-1 digest instance.
- Throws:
java.lang.IllegalArgumentException- when aNoSuchAlgorithmExceptionis caught, which should never happen because SHA-1 is a built-in algorithm- Since:
- 1.7
- See Also:
MessageDigestAlgorithms.SHA_1
-
getSha256Digest
public static java.security.MessageDigest getSha256Digest()
Returns an SHA-256 digest.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Returns:
- An SHA-256 digest instance.
- Throws:
java.lang.IllegalArgumentException- when aNoSuchAlgorithmExceptionis caught, which should never happen because SHA-256 is a built-in algorithm- See Also:
MessageDigestAlgorithms.SHA_256
-
getSha384Digest
public static java.security.MessageDigest getSha384Digest()
Returns an SHA-384 digest.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Returns:
- An SHA-384 digest instance.
- Throws:
java.lang.IllegalArgumentException- when aNoSuchAlgorithmExceptionis caught, which should never happen because SHA-384 is a built-in algorithm- See Also:
MessageDigestAlgorithms.SHA_384
-
getSha512Digest
public static java.security.MessageDigest getSha512Digest()
Returns an SHA-512 digest.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Returns:
- An SHA-512 digest instance.
- Throws:
java.lang.IllegalArgumentException- when aNoSuchAlgorithmExceptionis caught, which should never happen because SHA-512 is a built-in algorithm- See Also:
MessageDigestAlgorithms.SHA_512
-
getShaDigest
@Deprecated public static java.security.MessageDigest getShaDigest()
Deprecated.UsegetSha1Digest()Returns an SHA-1 digest.- Returns:
- An SHA-1 digest instance.
- Throws:
java.lang.IllegalArgumentException- when aNoSuchAlgorithmExceptionis caught
-
md2
public static byte[] md2(byte[] data)
Calculates the MD2 digest and returns the value as a 16 elementbyte[].- Parameters:
data- Data to digest- Returns:
- MD2 digest
- Since:
- 1.7
-
md2
public static byte[] md2(java.io.InputStream data) throws java.io.IOExceptionCalculates the MD2 digest and returns the value as a 16 elementbyte[].- Parameters:
data- Data to digest- Returns:
- MD2 digest
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.7
-
md2
public static byte[] md2(java.lang.String data)
Calculates the MD2 digest and returns the value as a 16 elementbyte[].- Parameters:
data- Data to digest; converted to bytes usingStringUtils.getBytesUtf8(String)- Returns:
- MD2 digest
- Since:
- 1.7
-
md2Hex
public static java.lang.String md2Hex(byte[] data)
Calculates the MD2 digest and returns the value as a 32 character hex string.- Parameters:
data- Data to digest- Returns:
- MD2 digest as a hex string
- Since:
- 1.7
-
md2Hex
public static java.lang.String md2Hex(java.io.InputStream data) throws java.io.IOExceptionCalculates the MD2 digest and returns the value as a 32 character hex string.- Parameters:
data- Data to digest- Returns:
- MD2 digest as a hex string
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.7
-
md2Hex
public static java.lang.String md2Hex(java.lang.String data)
Calculates the MD2 digest and returns the value as a 32 character hex string.- Parameters:
data- Data to digest- Returns:
- MD2 digest as a hex string
- Since:
- 1.7
-
md5
public static byte[] md5(byte[] data)
Calculates the MD5 digest and returns the value as a 16 elementbyte[].- Parameters:
data- Data to digest- Returns:
- MD5 digest
-
md5
public static byte[] md5(java.io.InputStream data) throws java.io.IOExceptionCalculates the MD5 digest and returns the value as a 16 elementbyte[].- Parameters:
data- Data to digest- Returns:
- MD5 digest
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
md5
public static byte[] md5(java.lang.String data)
Calculates the MD5 digest and returns the value as a 16 elementbyte[].- Parameters:
data- Data to digest; converted to bytes usingStringUtils.getBytesUtf8(String)- Returns:
- MD5 digest
-
md5Hex
public static java.lang.String md5Hex(byte[] data)
Calculates the MD5 digest and returns the value as a 32 character hex string.- Parameters:
data- Data to digest- Returns:
- MD5 digest as a hex string
-
md5Hex
public static java.lang.String md5Hex(java.io.InputStream data) throws java.io.IOExceptionCalculates the MD5 digest and returns the value as a 32 character hex string.- Parameters:
data- Data to digest- Returns:
- MD5 digest as a hex string
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
md5Hex
public static java.lang.String md5Hex(java.lang.String data)
Calculates the MD5 digest and returns the value as a 32 character hex string.- Parameters:
data- Data to digest- Returns:
- MD5 digest as a hex string
-
sha
@Deprecated public static byte[] sha(byte[] data)
Deprecated.Usesha1(byte[])Calculates the SHA-1 digest and returns the value as abyte[].- Parameters:
data- Data to digest- Returns:
- SHA-1 digest
-
sha
@Deprecated public static byte[] sha(java.io.InputStream data) throws java.io.IOExceptionDeprecated.Calculates the SHA-1 digest and returns the value as abyte[].- Parameters:
data- Data to digest- Returns:
- SHA-1 digest
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
sha
@Deprecated public static byte[] sha(java.lang.String data)
Deprecated.Usesha1(String)Calculates the SHA-1 digest and returns the value as abyte[].- Parameters:
data- Data to digest- Returns:
- SHA-1 digest
-
sha1
public static byte[] sha1(byte[] data)
Calculates the SHA-1 digest and returns the value as abyte[].- Parameters:
data- Data to digest- Returns:
- SHA-1 digest
- Since:
- 1.7
-
sha1
public static byte[] sha1(java.io.InputStream data) throws java.io.IOExceptionCalculates the SHA-1 digest and returns the value as abyte[].- Parameters:
data- Data to digest- Returns:
- SHA-1 digest
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.7
-
sha1
public static byte[] sha1(java.lang.String data)
Calculates the SHA-1 digest and returns the value as abyte[].- Parameters:
data- Data to digest; converted to bytes usingStringUtils.getBytesUtf8(String)- Returns:
- SHA-1 digest
-
sha1Hex
public static java.lang.String sha1Hex(byte[] data)
Calculates the SHA-1 digest and returns the value as a hex string.- Parameters:
data- Data to digest- Returns:
- SHA-1 digest as a hex string
- Since:
- 1.7
-
sha1Hex
public static java.lang.String sha1Hex(java.io.InputStream data) throws java.io.IOExceptionCalculates the SHA-1 digest and returns the value as a hex string.- Parameters:
data- Data to digest- Returns:
- SHA-1 digest as a hex string
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.7
-
sha1Hex
public static java.lang.String sha1Hex(java.lang.String data)
Calculates the SHA-1 digest and returns the value as a hex string.- Parameters:
data- Data to digest- Returns:
- SHA-1 digest as a hex string
- Since:
- 1.7
-
sha256
public static byte[] sha256(byte[] data)
Calculates the SHA-256 digest and returns the value as abyte[].Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-256 digest
- Since:
- 1.4
-
sha256
public static byte[] sha256(java.io.InputStream data) throws java.io.IOExceptionCalculates the SHA-256 digest and returns the value as abyte[].Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-256 digest
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
sha256
public static byte[] sha256(java.lang.String data)
Calculates the SHA-256 digest and returns the value as abyte[].Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest; converted to bytes usingStringUtils.getBytesUtf8(String)- Returns:
- SHA-256 digest
- Since:
- 1.4
-
sha256Hex
public static java.lang.String sha256Hex(byte[] data)
Calculates the SHA-256 digest and returns the value as a hex string.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-256 digest as a hex string
- Since:
- 1.4
-
sha256Hex
public static java.lang.String sha256Hex(java.io.InputStream data) throws java.io.IOExceptionCalculates the SHA-256 digest and returns the value as a hex string.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-256 digest as a hex string
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
sha256Hex
public static java.lang.String sha256Hex(java.lang.String data)
Calculates the SHA-256 digest and returns the value as a hex string.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-256 digest as a hex string
- Since:
- 1.4
-
sha384
public static byte[] sha384(byte[] data)
Calculates the SHA-384 digest and returns the value as abyte[].Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-384 digest
- Since:
- 1.4
-
sha384
public static byte[] sha384(java.io.InputStream data) throws java.io.IOExceptionCalculates the SHA-384 digest and returns the value as abyte[].Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-384 digest
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
sha384
public static byte[] sha384(java.lang.String data)
Calculates the SHA-384 digest and returns the value as abyte[].Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest; converted to bytes usingStringUtils.getBytesUtf8(String)- Returns:
- SHA-384 digest
- Since:
- 1.4
-
sha384Hex
public static java.lang.String sha384Hex(byte[] data)
Calculates the SHA-384 digest and returns the value as a hex string.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-384 digest as a hex string
- Since:
- 1.4
-
sha384Hex
public static java.lang.String sha384Hex(java.io.InputStream data) throws java.io.IOExceptionCalculates the SHA-384 digest and returns the value as a hex string.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-384 digest as a hex string
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
sha384Hex
public static java.lang.String sha384Hex(java.lang.String data)
Calculates the SHA-384 digest and returns the value as a hex string.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-384 digest as a hex string
- Since:
- 1.4
-
sha512
public static byte[] sha512(byte[] data)
Calculates the SHA-512 digest and returns the value as abyte[].Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-512 digest
- Since:
- 1.4
-
sha512
public static byte[] sha512(java.io.InputStream data) throws java.io.IOExceptionCalculates the SHA-512 digest and returns the value as abyte[].Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-512 digest
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
sha512
public static byte[] sha512(java.lang.String data)
Calculates the SHA-512 digest and returns the value as abyte[].Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest; converted to bytes usingStringUtils.getBytesUtf8(String)- Returns:
- SHA-512 digest
- Since:
- 1.4
-
sha512Hex
public static java.lang.String sha512Hex(byte[] data)
Calculates the SHA-512 digest and returns the value as a hex string.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-512 digest as a hex string
- Since:
- 1.4
-
sha512Hex
public static java.lang.String sha512Hex(java.io.InputStream data) throws java.io.IOExceptionCalculates the SHA-512 digest and returns the value as a hex string.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-512 digest as a hex string
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
sha512Hex
public static java.lang.String sha512Hex(java.lang.String data)
Calculates the SHA-512 digest and returns the value as a hex string.Throws a
RuntimeExceptionon JRE versions prior to 1.4.0.- Parameters:
data- Data to digest- Returns:
- SHA-512 digest as a hex string
- Since:
- 1.4
-
shaHex
@Deprecated public static java.lang.String shaHex(byte[] data)
Deprecated.Usesha1Hex(byte[])Calculates the SHA-1 digest and returns the value as a hex string.- Parameters:
data- Data to digest- Returns:
- SHA-1 digest as a hex string
-
shaHex
@Deprecated public static java.lang.String shaHex(java.io.InputStream data) throws java.io.IOExceptionDeprecated.Calculates the SHA-1 digest and returns the value as a hex string.- Parameters:
data- Data to digest- Returns:
- SHA-1 digest as a hex string
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.4
-
shaHex
@Deprecated public static java.lang.String shaHex(java.lang.String data)
Deprecated.Usesha1Hex(String)Calculates the SHA-1 digest and returns the value as a hex string.- Parameters:
data- Data to digest- Returns:
- SHA-1 digest as a hex string
-
updateDigest
public static java.security.MessageDigest updateDigest(java.security.MessageDigest messageDigest, byte[] valueToDigest)Updates the givenMessageDigest.- Parameters:
messageDigest- theMessageDigestto updatevalueToDigest- the value to update theMessageDigestwith- Returns:
- the updated
MessageDigest - Since:
- 1.7
-
updateDigest
public static java.security.MessageDigest updateDigest(java.security.MessageDigest digest, java.io.InputStream data) throws java.io.IOExceptionReads through an InputStream and updates the digest for the data- Parameters:
digest- The MessageDigest to use (e.g. MD5)data- Data to digest- Returns:
- the digest
- Throws:
java.io.IOException- On error reading from the stream- Since:
- 1.8
-
updateDigest
public static java.security.MessageDigest updateDigest(java.security.MessageDigest messageDigest, java.lang.String valueToDigest)Updates the givenMessageDigest.- Parameters:
messageDigest- theMessageDigestto updatevalueToDigest- the value to update theMessageDigestwith; converted to bytes usingStringUtils.getBytesUtf8(String)- Returns:
- the updated
MessageDigest - Since:
- 1.7
-
-