org.n52.security.common.crypto
Class DigestUtil

java.lang.Object
  extended by org.n52.security.common.crypto.DigestUtil

public final class DigestUtil
extends Object

Contains some convenient utility methods related to MessageDigest tasks.

Version:
: $
Author:
Marko Reiprecht Created on 19.09.2007

Method Summary
static boolean areEqualDigests(char[] a, char[] b)
          Method tests the both char[] on equality, ignoring the case.
static boolean calculateAndTestForEquality(char[] expected, char[] inputToCalculate, String algorithm)
          This is the prevered method to simple calculate a digest e.g.
static boolean calculateAndTestForEquality(String expected, String inputToCalculate, String algorithm)
          This is the prevered method to simple calculate a digest e.g.
static byte[] calculateDigestOf(byte[] value, String algorithm)
          calcualates a digest for the given value with the given algorithm.
static byte[] calculateDigestOf(char[] value, String algorithm)
          calcualates a digest for the given value with the given algorithm.
static byte[] calculateDigestOf(CharBuffer value, String algorithm)
          calcualates a digest for the given value with the given algorithm.
static byte[] calculateDigestOf(String value, String algorithm)
          calcualates a digest for the given value with the given algorithm.
static char[] digestToCharArray(byte[] digest)
          Method digestToCharArray converts a byte[] message digest to its two digit hexdecimal representation as chars.
static String digestToString(byte[] digest)
          Method digestToString converts a byte[] message digest to its two digit hexdecimal representation as string.
static MessageDigest getMessageDigest(String algorithm)
          convinient mehtod, wrapping the NoSuchAlgorithmException in an IllegalArgumentException if algorithm not exists.
static boolean isDigestAlgorithm(String algorithm)
          Responsible for .
static boolean isDigestOf(byte[] digest, int expectedSize)
          Method isDigestOf test if the given byte[] array is a digest.
static boolean isDigestOf(byte[] digest, MessageDigest algorithm)
          Method isDigestOf test if the given byte[] array is a digest.
static boolean isDigestOf(byte[] digest, String algorithm)
          Method isDigestOf test if the given byte[] array is a digest.
static boolean isDigestOf(char[] digest, int expectedSize)
          Method isDigestOf test if the given char[] array is a digest.
static boolean isDigestOf(char[] digest, MessageDigest algorithm)
          Method isDigestOf test if the given char[] array is a digest.
static boolean isDigestOf(char[] digest, String algorithm)
          Method isDigestOf test if the given char[] array is a digest.
static boolean isDigestOf(String digest, int expectedSize)
          Method isDigestOf test if the given String is a digest.
static boolean isDigestOf(String digest, MessageDigest algorithm)
          Method isDigestOf test if the given char[] array is a digest.
static boolean isDigestOf(String digest, String algorithm)
          Method isDigestOf test if the given char[] array is a digest.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getMessageDigest

public static MessageDigest getMessageDigest(String algorithm)
convinient mehtod, wrapping the NoSuchAlgorithmException in an IllegalArgumentException if algorithm not exists.


areEqualDigests

public static boolean areEqualDigests(char[] a,
                                      char[] b)
Method tests the both char[] on equality, ignoring the case.

Parameters:
a - the first to check.
b - the second to check.
Returns:
only true if both char[] equal(ignoring the case).

digestToString

public static String digestToString(byte[] digest)
Method digestToString converts a byte[] message digest to its two digit hexdecimal representation as string.

Parameters:
digest - the digest !=null.
Returns:
String.

digestToCharArray

public static char[] digestToCharArray(byte[] digest)
Method digestToCharArray converts a byte[] message digest to its two digit hexdecimal representation as chars.

Parameters:
digest - the digest !=null.
Returns:
char[].

isDigestOf

public static boolean isDigestOf(byte[] digest,
                                 int expectedSize)
Method isDigestOf test if the given byte[] array is a digest. This simple tests for the correct length and is only supplied for convenience.

Parameters:
digest - the digest !=null
expectedSize - the expectedSize.
Returns:
boolean.

isDigestOf

public static boolean isDigestOf(char[] digest,
                                 int expectedSize)
Method isDigestOf test if the given char[] array is a digest. This tests the correct length and if all chars are in a hexadecimal range.

The expectedSize must be the length of the digest in bytes, not in chars!

Parameters:
digest - the digest !=null.
expectedSize - the expectedSize of the digest in bytes as given by the algoritm MessageDigest.getDigestLength() Therewith this is the size in bytes, not the size of the char array, e.g. MD5 has a size of 16, and the char[] is tested for a length of 32.
Returns:
boolean.

isDigestOf

public static boolean isDigestOf(String digest,
                                 int expectedSize)
Method isDigestOf test if the given String is a digest. This tests the correct length and if all chars are in a hexadecimal range.

The expectedSize must be the length of the digest in bytes, not in chars!

Parameters:
digest - the digest !=null.
expectedSize - the expectedSize of the digest in bytes as given by the algoritm MessageDigest.getDigestLength() Therewith this is the size in bytes, not the size of the String, e.g. MD5 has a size of 16, and the String is tested for a length of 32.
Returns:
boolean.

isDigestOf

public static boolean isDigestOf(byte[] digest,
                                 MessageDigest algorithm)
Method isDigestOf test if the given byte[] array is a digest. This simple tests for the correct length and is only supplied for convenience.

Parameters:
digest - the digest !=null.
algorithm - the algorithm !=null.
Returns:
boolean.

isDigestOf

public static boolean isDigestOf(char[] digest,
                                 MessageDigest algorithm)
Method isDigestOf test if the given char[] array is a digest. This tests the correct length and if all chars are in a hexadecimal range.

Parameters:
digest - the digest !=null.
algorithm - the algorithm !=null.
Returns:
boolean.

isDigestOf

public static boolean isDigestOf(String digest,
                                 MessageDigest algorithm)
Method isDigestOf test if the given char[] array is a digest. This tests the correct length and if all chars are in a hexadecimal range.

Parameters:
digest - the digest !=null.
algorithm - the algorithm !=null.
Returns:
boolean.

isDigestOf

public static boolean isDigestOf(byte[] digest,
                                 String algorithm)
Method isDigestOf test if the given byte[] array is a digest. This simple tests for the correct length and is only supplied for convenience.

Parameters:
digest - the digest !=null.
algorithm - the algorithm !=null.
Returns:
boolean.
Throws:
IllegalArgumentException - if algorithm not supported.

isDigestOf

public static boolean isDigestOf(char[] digest,
                                 String algorithm)
Method isDigestOf test if the given char[] array is a digest. This tests the correct length and if all chars are in a hexadecimal range.

Parameters:
digest - the digest !=null.
algorithm - the method !=null.
Returns:
boolean.
Throws:
java.security.IllegalArgumentException - if algorithm not supported.

isDigestOf

public static boolean isDigestOf(String digest,
                                 String algorithm)
Method isDigestOf test if the given char[] array is a digest. This tests the correct length and if all chars are in a hexadecimal range.

Parameters:
digest - the digest !=null.
algorithm - the method !=null.
Returns:
boolean.
Throws:
IllegalArgumentException - if algorithm not supported.

calculateDigestOf

public static final byte[] calculateDigestOf(byte[] value,
                                             String algorithm)
calcualates a digest for the given value with the given algorithm.

Returns:
the byte digest.

calculateDigestOf

public static final byte[] calculateDigestOf(char[] value,
                                             String algorithm)
calcualates a digest for the given value with the given algorithm.

Returns:
the byte digest.

calculateDigestOf

public static final byte[] calculateDigestOf(String value,
                                             String algorithm)
calcualates a digest for the given value with the given algorithm.

Returns:
the byte digest.

calculateDigestOf

public static final byte[] calculateDigestOf(CharBuffer value,
                                             String algorithm)
calcualates a digest for the given value with the given algorithm.

Returns:
the byte digest.

isDigestAlgorithm

public static boolean isDigestAlgorithm(String algorithm)
Responsible for .

Parameters:
passwordEncryptionAlg -
Returns:

calculateAndTestForEquality

public static boolean calculateAndTestForEquality(char[] expected,
                                                  char[] inputToCalculate,
                                                  String algorithm)
This is the prevered method to simple calculate a digest e.g. of password and compare it with an already known password digest.

Parameters:
expected - the expected digest as char array.
inputToCalculate - the orginal password chars, from with first the digest is calculated.
algorithm - the algorithm used to calculate the digest from the input.
Returns:
true if equal.

calculateAndTestForEquality

public static boolean calculateAndTestForEquality(String expected,
                                                  String inputToCalculate,
                                                  String algorithm)
This is the prevered method to simple calculate a digest e.g. of password and compare it with an already known password digest.

Parameters:
expected - the expected digest as char array.
inputToCalculate - the orginal password chars, from with first the digest is calculated.
algorithm - the algorithm used to calculate the digest from the input.
Returns:
true if equal.


Copyright © 2004–2014 52north.org. All rights reserved.