goldengate.common.digest
Class FilesystemBasedDigest

java.lang.Object
  extended by goldengate.common.digest.FilesystemBasedDigest

public class FilesystemBasedDigest
extends Object

Class implementing digest like MD5, SHA1. MD5 is based on the Fast MD5 implementation, without C library support, but can be revert to JVM native digest.

Some performance reports: (done using java -server option)

Author:
Frederic Bregier

Nested Class Summary
static class FilesystemBasedDigest.DigestAlgo
          All Algo that Digest Class could handle
 
Field Summary
protected  FilesystemBasedDigest.DigestAlgo algo
           
protected  Checksum checksum
           
protected  MessageDigest digest
           
static String fastMd5Path
          If using Fast MD5, should we used the binary JNI library, empty meaning no.
protected  MD5 md5
           
static boolean useFastMd5
          Should a file MD5 be computed using FastMD5
 
Constructor Summary
FilesystemBasedDigest(FilesystemBasedDigest.DigestAlgo algo)
          Constructor of an independent Digest
 
Method Summary
static boolean digestEquals(byte[] dig1, byte[] dig2)
           
static boolean digestEquals(String dig1, byte[] dig2)
           
static boolean equalPasswd(byte[] pwd, byte[] cryptPwd)
           
static boolean equalPasswd(String pwd, String cryptPwd)
           
 byte[] Final()
           
static byte[] getFromHex(String hex)
          Get the array of bytes representation of the hexadecimal String
static byte[] getHash(org.jboss.netty.buffer.ChannelBuffer buffer, FilesystemBasedDigest.DigestAlgo algo)
          Get hash with given ChannelBuffer (from Netty)
static byte[] getHash(File f, boolean nio, FilesystemBasedDigest.DigestAlgo algo)
          Get the Digest for the file using the specified algorithm using access through NIO or not
static byte[] getHash(InputStream stream, FilesystemBasedDigest.DigestAlgo algo)
          Get the Digest for the file using the specified algorithm using access through NIO or not
static byte[] getHashMd5(org.jboss.netty.buffer.ChannelBuffer buffer)
          Get hash with given ChannelBuffer (from Netty)
static byte[] getHashMd5(File f)
          get the byte array of the MD5 for the given FileInterface using standard access
static byte[] getHashMd5Nio(File f)
          get the byte array of the MD5 for the given FileInterface using Nio access
static byte[] getHashSha1(File f)
          get the byte array of the SHA-1 for the given FileInterface using standard access
static byte[] getHashSha1Nio(File f)
          get the byte array of the SHA-1 for the given FileInterface using Nio access
static String getHex(byte[] hash)
          Get the hexadecimal representation as a String of the array of bytes
 void initialize()
          (Re)Initialize the digest
 void initialize(FilesystemBasedDigest.DigestAlgo algo)
          (Re)Initialize the digest
static boolean initializeMd5(boolean mustUseFastMd5, String path)
          Initialize the MD5 support
static void main(String[] argv)
          Test function
static byte[] passwdCrypt(byte[] pwd)
          Crypt a password
static String passwdCrypt(String pwd)
          Crypt a password
 void Update(byte[] bytes, int offset, int length)
          Update the digest with new bytes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

md5

protected MD5 md5

checksum

protected Checksum checksum

digest

protected MessageDigest digest

algo

protected FilesystemBasedDigest.DigestAlgo algo

useFastMd5

public static boolean useFastMd5
Should a file MD5 be computed using FastMD5


fastMd5Path

public static String fastMd5Path
If using Fast MD5, should we used the binary JNI library, empty meaning no. FastMD5 is up to 50% fastest than JVM, but JNI library might be not better than java FastMD5.

Constructor Detail

FilesystemBasedDigest

public FilesystemBasedDigest(FilesystemBasedDigest.DigestAlgo algo)
                      throws NoSuchAlgorithmException
Constructor of an independent Digest

Parameters:
algo -
Throws:
NoSuchAlgorithmException
Method Detail

initialize

public void initialize()
                throws NoSuchAlgorithmException
(Re)Initialize the digest

Throws:
NoSuchAlgorithmException

initialize

public void initialize(FilesystemBasedDigest.DigestAlgo algo)
                throws NoSuchAlgorithmException
(Re)Initialize the digest

Parameters:
algo -
Throws:
NoSuchAlgorithmException

Update

public void Update(byte[] bytes,
                   int offset,
                   int length)
Update the digest with new bytes

Parameters:
bytes -
offset -
length -

Final

public byte[] Final()
Returns:
the digest in array of bytes

initializeMd5

public static boolean initializeMd5(boolean mustUseFastMd5,
                                    String path)
Initialize the MD5 support

Parameters:
mustUseFastMd5 - True will use FastMD5 support, False will use JVM native MD5
path - If useFastMD5 is True, if path is not null, specify the C Library (optional), else if null will use the Java implementation
Returns:
True if the native library is loaded

digestEquals

public static final boolean digestEquals(byte[] dig1,
                                         byte[] dig2)
Parameters:
dig1 -
dig2 -
Returns:
True if the two digest are equals

digestEquals

public static final boolean digestEquals(String dig1,
                                         byte[] dig2)
Parameters:
dig1 -
dig2 -
Returns:
True if the two digest are equals

getHashMd5Nio

public static byte[] getHashMd5Nio(File f)
                            throws IOException
get the byte array of the MD5 for the given FileInterface using Nio access

Parameters:
f -
Returns:
the byte array representing the MD5
Throws:
IOException

getHashMd5

public static byte[] getHashMd5(File f)
                         throws IOException
get the byte array of the MD5 for the given FileInterface using standard access

Parameters:
f -
Returns:
the byte array representing the MD5
Throws:
IOException

getHashSha1Nio

public static byte[] getHashSha1Nio(File f)
                             throws IOException
get the byte array of the SHA-1 for the given FileInterface using Nio access

Parameters:
f -
Returns:
the byte array representing the SHA-1
Throws:
IOException

getHashSha1

public static byte[] getHashSha1(File f)
                          throws IOException
get the byte array of the SHA-1 for the given FileInterface using standard access

Parameters:
f -
Returns:
the byte array representing the SHA-1
Throws:
IOException

getHash

public static byte[] getHash(File f,
                             boolean nio,
                             FilesystemBasedDigest.DigestAlgo algo)
                      throws IOException
Get the Digest for the file using the specified algorithm using access through NIO or not

Parameters:
f -
nio -
algo -
Returns:
the digest
Throws:
IOException

getHash

public static byte[] getHash(InputStream stream,
                             FilesystemBasedDigest.DigestAlgo algo)
                      throws IOException
Get the Digest for the file using the specified algorithm using access through NIO or not

Parameters:
stream -
algo -
Returns:
the digest
Throws:
IOException

getHash

public static byte[] getHash(org.jboss.netty.buffer.ChannelBuffer buffer,
                             FilesystemBasedDigest.DigestAlgo algo)
                      throws IOException
Get hash with given ChannelBuffer (from Netty)

Parameters:
buffer - this buffer will not be changed
algo -
Returns:
the hash
Throws:
IOException

getHashMd5

public static byte[] getHashMd5(org.jboss.netty.buffer.ChannelBuffer buffer)
Get hash with given ChannelBuffer (from Netty)

Parameters:
buffer - ChannelBuffer to use to get the hash and this buffer will not be changed
Returns:
the hash

getHex

public static final String getHex(byte[] hash)
Get the hexadecimal representation as a String of the array of bytes

Parameters:
hash -
Returns:
the hexadecimal representation as a String of the array of bytes

getFromHex

public static final byte[] getFromHex(String hex)
Get the array of bytes representation of the hexadecimal String

Parameters:
hex -
Returns:
the array of bytes representation of the hexadecimal String

passwdCrypt

public static final String passwdCrypt(String pwd)
Crypt a password

Parameters:
pwd - to crypt
Returns:
the crypted password
Throws:
IOException

passwdCrypt

public static final byte[] passwdCrypt(byte[] pwd)
Crypt a password

Parameters:
pwd - to crypt
Returns:
the crypted password
Throws:
IOException

equalPasswd

public static final boolean equalPasswd(String pwd,
                                        String cryptPwd)
Parameters:
pwd -
cryptPwd -
Returns:
True if the pwd is comparable with the cryptPwd
Throws:
IOException

equalPasswd

public static final boolean equalPasswd(byte[] pwd,
                                        byte[] cryptPwd)
Parameters:
pwd -
cryptPwd -
Returns:
True if the pwd is comparable with the cryptPwd

main

public static void main(String[] argv)
                 throws IOException
Test function

Parameters:
argv - with 2 arguments as filename to hash and full path to the Native Library
Throws:
IOException


Copyright © 2009-2012 Frederic Bregier. All Rights Reserved.