org.apache.commons.codec.digest
Class DigestUtils

java.lang.Object
  extended by org.apache.commons.codec.digest.DigestUtils

public class DigestUtils
extends java.lang.Object

Operations to simplify common MessageDigest tasks. This class is thread safe.

Version:
$Id: DigestUtils.java 1157192 2011-08-12 17:27:38Z ggregory $
Author:
Apache Software Foundation

Field Summary
private static int STREAM_BUFFER_LENGTH
           
 
Constructor Summary
DigestUtils()
           
 
Method Summary
private static byte[] digest(java.security.MessageDigest digest, java.io.InputStream data)
          Read through an InputStream and returns the digest for the data
private static byte[] getBytesUtf8(java.lang.String data)
          Calls StringUtils.getBytesUtf8(String)
(package private) static java.security.MessageDigest getDigest(java.lang.String algorithm)
          Returns a MessageDigest for the given algorithm.
private static java.security.MessageDigest getMd5Digest()
          Returns an MD5 MessageDigest.
private static java.security.MessageDigest getSha256Digest()
          Returns an SHA-256 digest.
private static java.security.MessageDigest getSha384Digest()
          Returns an SHA-384 digest.
private static java.security.MessageDigest getSha512Digest()
          Returns an SHA-512 digest.
private static java.security.MessageDigest getShaDigest()
          Returns an SHA-1 digest.
static byte[] md5(byte[] data)
          Calculates the MD5 digest and returns the value as a 16 element byte[].
static byte[] md5(java.io.InputStream data)
          Calculates the MD5 digest and returns the value as a 16 element byte[].
static byte[] md5(java.lang.String data)
          Calculates the MD5 digest and returns the value as a 16 element byte[].
static java.lang.String md5Hex(byte[] data)
          Calculates the MD5 digest and returns the value as a 32 character hex string.
static java.lang.String md5Hex(java.io.InputStream data)
          Calculates the MD5 digest and returns the value as a 32 character hex string.
static java.lang.String md5Hex(java.lang.String data)
          Calculates the MD5 digest and returns the value as a 32 character hex string.
static byte[] sha(byte[] data)
          Calculates the SHA-1 digest and returns the value as a byte[].
static byte[] sha(java.io.InputStream data)
          Calculates the SHA-1 digest and returns the value as a byte[].
static byte[] sha(java.lang.String data)
          Calculates the SHA-1 digest and returns the value as a byte[].
static byte[] sha256(byte[] data)
          Calculates the SHA-256 digest and returns the value as a byte[].
static byte[] sha256(java.io.InputStream data)
          Calculates the SHA-256 digest and returns the value as a byte[].
static byte[] sha256(java.lang.String data)
          Calculates the SHA-256 digest and returns the value as a byte[].
static java.lang.String sha256Hex(byte[] data)
          Calculates the SHA-256 digest and returns the value as a hex string.
static java.lang.String sha256Hex(java.io.InputStream data)
          Calculates the SHA-256 digest and returns the value as a hex string.
static java.lang.String sha256Hex(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 a byte[].
static byte[] sha384(java.io.InputStream data)
          Calculates the SHA-384 digest and returns the value as a byte[].
static byte[] sha384(java.lang.String data)
          Calculates the SHA-384 digest and returns the value as a byte[].
static java.lang.String sha384Hex(byte[] data)
          Calculates the SHA-384 digest and returns the value as a hex string.
static java.lang.String sha384Hex(java.io.InputStream data)
          Calculates the SHA-384 digest and returns the value as a hex string.
static java.lang.String sha384Hex(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 a byte[].
static byte[] sha512(java.io.InputStream data)
          Calculates the SHA-512 digest and returns the value as a byte[].
static byte[] sha512(java.lang.String data)
          Calculates the SHA-512 digest and returns the value as a byte[].
static java.lang.String sha512Hex(byte[] data)
          Calculates the SHA-512 digest and returns the value as a hex string.
static java.lang.String sha512Hex(java.io.InputStream data)
          Calculates the SHA-512 digest and returns the value as a hex string.
static java.lang.String sha512Hex(java.lang.String data)
          Calculates the SHA-512 digest and returns the value as a hex string.
static java.lang.String shaHex(byte[] data)
          Calculates the SHA-1 digest and returns the value as a hex string.
static java.lang.String shaHex(java.io.InputStream data)
          Calculates the SHA-1 digest and returns the value as a hex string.
static java.lang.String shaHex(java.lang.String data)
          Calculates the SHA-1 digest and returns the value as a hex string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STREAM_BUFFER_LENGTH

private static final int STREAM_BUFFER_LENGTH
See Also:
Constant Field Values
Constructor Detail

DigestUtils

public DigestUtils()
Method Detail

digest

private static byte[] digest(java.security.MessageDigest digest,
                             java.io.InputStream data)
                      throws java.io.IOException
Read through an InputStream and returns the digest for the data

Parameters:
digest - The MessageDigest to use (e.g. MD5)
data - Data to digest
Returns:
MD5 digest
Throws:
java.io.IOException - On error reading from the stream

getBytesUtf8

private static byte[] getBytesUtf8(java.lang.String data)
Calls StringUtils.getBytesUtf8(String)

Parameters:
data - the String to encode
Returns:
encoded bytes

getDigest

static java.security.MessageDigest getDigest(java.lang.String algorithm)
Returns a MessageDigest for the given algorithm.

Parameters:
algorithm - the name of the algorithm requested. See Appendix A in the Java Cryptography Architecture API Specification & Reference for information about standard algorithm names.
Returns:
An MD5 digest instance.
Throws:
java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.
See Also:
MessageDigest.getInstance(String)

getMd5Digest

private static java.security.MessageDigest getMd5Digest()
Returns an MD5 MessageDigest.

Returns:
An MD5 digest instance.
Throws:
java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.

getSha256Digest

private static java.security.MessageDigest getSha256Digest()
Returns an SHA-256 digest.

Throws a RuntimeException on JRE versions prior to 1.4.0.

Returns:
An SHA-256 digest instance.
Throws:
java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.

getSha384Digest

private static java.security.MessageDigest getSha384Digest()
Returns an SHA-384 digest.

Throws a RuntimeException on JRE versions prior to 1.4.0.

Returns:
An SHA-384 digest instance.
Throws:
java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.

getSha512Digest

private static java.security.MessageDigest getSha512Digest()
Returns an SHA-512 digest.

Throws a RuntimeException on JRE versions prior to 1.4.0.

Returns:
An SHA-512 digest instance.
Throws:
java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.

getShaDigest

private static java.security.MessageDigest getShaDigest()
Returns an SHA-1 digest.

Returns:
An SHA-1 digest instance.
Throws:
java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.

md5

public static byte[] md5(byte[] data)
Calculates the MD5 digest and returns the value as a 16 element byte[].

Parameters:
data - Data to digest
Returns:
MD5 digest

md5

public static byte[] md5(java.io.InputStream data)
                  throws java.io.IOException
Calculates the MD5 digest and returns the value as a 16 element byte[].

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 element byte[].

Parameters:
data - Data to digest
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.IOException
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
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

public static byte[] sha(byte[] data)
Calculates the SHA-1 digest and returns the value as a byte[].

Parameters:
data - Data to digest
Returns:
SHA-1 digest

sha

public static byte[] sha(java.io.InputStream data)
                  throws java.io.IOException
Calculates the SHA-1 digest and returns the value as a byte[].

Parameters:
data - Data to digest
Returns:
SHA-1 digest
Throws:
java.io.IOException - On error reading from the stream
Since:
1.4

sha

public static byte[] sha(java.lang.String data)
Calculates the SHA-1 digest and returns the value as a byte[].

Parameters:
data - Data to digest
Returns:
SHA-1 digest

sha256

public static byte[] sha256(byte[] data)
Calculates the SHA-256 digest and returns the value as a byte[].

Throws a RuntimeException on 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.IOException
Calculates the SHA-256 digest and returns the value as a byte[].

Throws a RuntimeException on 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 a byte[].

Throws a RuntimeException on JRE versions prior to 1.4.0.

Parameters:
data - Data to digest
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 RuntimeException on 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.IOException
Calculates the SHA-256 digest and returns the value as a hex string.

Throws a RuntimeException on 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 RuntimeException on 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 a byte[].

Throws a RuntimeException on 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.IOException
Calculates the SHA-384 digest and returns the value as a byte[].

Throws a RuntimeException on 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 a byte[].

Throws a RuntimeException on JRE versions prior to 1.4.0.

Parameters:
data - Data to digest
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 RuntimeException on 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.IOException
Calculates the SHA-384 digest and returns the value as a hex string.

Throws a RuntimeException on 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 RuntimeException on 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 a byte[].

Throws a RuntimeException on 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.IOException
Calculates the SHA-512 digest and returns the value as a byte[].

Throws a RuntimeException on 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 a byte[].

Throws a RuntimeException on JRE versions prior to 1.4.0.

Parameters:
data - Data to digest
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 RuntimeException on 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.IOException
Calculates the SHA-512 digest and returns the value as a hex string.

Throws a RuntimeException on 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 RuntimeException on JRE versions prior to 1.4.0.

Parameters:
data - Data to digest
Returns:
SHA-512 digest as a hex string
Since:
1.4

shaHex

public static java.lang.String shaHex(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

shaHex

public static java.lang.String shaHex(java.io.InputStream data)
                               throws java.io.IOException
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

public static java.lang.String shaHex(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


commons-codec version 1.6-SNAPSHOT - Copyright © 2002-2012 - Apache Software Foundation