com.evanmclean.evlib.security
Class Digests

java.lang.Object
  extended by com.evanmclean.evlib.security.Digests

public final class Digests
extends Object

Various utilities to make it simpler to get common message digests.

Author:
Evan McLean McLean Computer Services (see the overview for copyright and licensing.)

Method Summary
static String digest(File file, MessageDigest digest)
          Run a digest against the contents of a file.
static String digest(InputStream in, MessageDigest digest)
          Run a digest against the input stream.
static MessageDigest getDigest(String algorithm)
          Returns a MessageDigest object that implements the specified digest algorithm.
static MessageDigest getMd5Digest()
          Returns a MessageDigest object that implements the MD5 algorithm.
static MessageDigest getShaDigest()
          Returns a MessageDigest object that implements the SHA algorithm.
static String hex(byte[] buffer)
          Converts an array of bytes into a String representing the hexidecimal values of each byte in order.
static String hex(MessageDigest digest)
          Converts the results of MessageDigest.digest() into a String representing the hexidecimal values of each byte in order.
static String md5(File file)
          Product an MD5 checksum against contents of a file.
static String md5(FileInputStream in)
          Product an MD5 checksum against a file input stream.
static String md5(InputStream in)
          Product an MD5 checksum against an input stream.
static String sha(File file)
          Product an SHA checksum against contents of a file.
static String sha(FileInputStream in)
          Product an SHA checksum against a file input stream.
static String sha(InputStream in)
          Product an SHA checksum against an input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

digest

public static String digest(File file,
                            MessageDigest digest)
                     throws IOException
Run a digest against the contents of a file.

Parameters:
file - The file to read.
digest - The digest object to use.
Returns:
The return of digest.digest() converted to a hexidecimal string.
Throws:
IOException

digest

public static String digest(InputStream in,
                            MessageDigest digest)
                     throws IOException
Run a digest against the input stream.

Parameters:
in - The input stream to read.
digest - The digest object to use.
Returns:
The return of digest.digest() converted to a hexidecimal string.
Throws:
IOException

getDigest

public static MessageDigest getDigest(String algorithm)
Returns a MessageDigest object that implements the specified digest algorithm. Basically a wrapper around MessageDigest.getInstance(String) that throws a RuntimeException instead of a NoSuchAlgorithmException.

Parameters:
algorithm - The name of the algorithm requested.
Returns:
A Message Digest object that implements the specified algorithm.
Throws:
RuntimeException - If no Provider supports a MessageDigestSpi implementation for the specified algorithm.

getMd5Digest

public static MessageDigest getMd5Digest()
Returns a MessageDigest object that implements the MD5 algorithm.

Returns:
A Message Digest object that implements the MD5 algorithm.
Throws:
RuntimeException - If no Provider supports a MessageDigestSpi implementation for the MD5 algorithm.

getShaDigest

public static MessageDigest getShaDigest()
Returns a MessageDigest object that implements the SHA algorithm.

Returns:
A Message Digest object that implements the SHA algorithm.
Throws:
RuntimeException - If no Provider supports a MessageDigestSpi implementation for the SHA algorithm.

hex

public static String hex(byte[] buffer)
Converts an array of bytes into a String representing the hexidecimal values of each byte in order.

Parameters:
buffer - A byte[] to convert to Hex characters.
Returns:
A String representing the hexidecimal values of each byte in order.

hex

public static String hex(MessageDigest digest)
Converts the results of MessageDigest.digest() into a String representing the hexidecimal values of each byte in order.

Parameters:
digest - A digest to convert to Hex characters.
Returns:
A String representing the hexidecimal values of each byte in order.

md5

public static String md5(File file)
                  throws IOException
Product an MD5 checksum against contents of a file. This will use nio ByteBuffer logic on files larger than 100K for efficiency.

Parameters:
file - The file to read.
Returns:
The MD5 checksum.
Throws:
IOException

md5

public static String md5(FileInputStream in)
                  throws IOException
Product an MD5 checksum against a file input stream. This will use nio ByteBuffer logic on files larger than 100K for efficiency.

Parameters:
in - The file input stream to read.
Returns:
The MD5 checksum.
Throws:
IOException

md5

public static String md5(InputStream in)
                  throws IOException
Product an MD5 checksum against an input stream.

Parameters:
in - The input stream to read.
Returns:
The MD5 checksum.
Throws:
IOException

sha

public static String sha(File file)
                  throws IOException
Product an SHA checksum against contents of a file. This will use nio ByteBuffer logic on files larger than 100K for efficiency.

Parameters:
file - The file to read.
Returns:
The SHA checksum.
Throws:
IOException

sha

public static String sha(FileInputStream in)
                  throws IOException
Product an SHA checksum against a file input stream. This will use nio ByteBuffer logic on files larger than 100K for efficiency.

Parameters:
in - The file input stream to read.
Returns:
The SHA checksum.
Throws:
IOException

sha

public static String sha(InputStream in)
                  throws IOException
Product an SHA checksum against an input stream.

Parameters:
in - The input stream to read.
Returns:
The SHA checksum.
Throws:
IOException