|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgoldengate.common.digest.MD5
public class MD5
Fast implementation of RSA's MD5 hash generator in Java JDK Beta-2 or higher.
Originally written by Santeri Paavolainen, Helsinki Finland 1996.
(c) Santeri Paavolainen, Helsinki Finland 1996
Many changes Copyright (c) 2002 - 2005 Timothy W Macinta
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
See http://www.twmacinta.com/myjava/fast_md5.php for more information on this file and the related files.
This was originally a rather straight re-implementation of the reference implementation given in RFC1321 by RSA. It passes the MD5 test suite as defined in RFC1321.
Many optimizations made by Timothy W Macinta. Reduced time to checksum a test file in Java alone to roughly half the time taken compared with java.security.MessageDigest (within an intepretter). Also added an optional native method to reduce the time even further. See http://www.twmacinta.com/myjava/fast_md5.php for further information on the time improvements achieved.
Some bug fixes also made by Timothy W Macinta.
Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util package only because it came without a package. I was not the the original author of the code, although I did optimize it (substantially) and fix some bugs.
This Java class has been derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm and its reference implementation.
This class will attempt to use a native method to quickly compute checksums when the appropriate native library is available. On Linux, this library should be named "MD5.so" and on Windows it should be named "MD5.dll". The code will attempt to locate the library in the following locations in the order given:
If the library is not found, the code will fall back to the default (slower) Java code.
As a side effect of having the code search for the native library, SecurityExceptions might be thrown on JVMs that have a restrictive SecurityManager. The initialization code attempts to silently discard these exceptions and continue, but many SecurityManagers will attempt to notify the user directly of all SecurityExceptions thrown. Consequently, the code has provisions for skipping the search for the native library. Any of these provisions may be used to skip the search as long as they are performed before the first instance of a com.twmacinta.util.MD5 object is constructed (note that the convenience stream objects will implicitly create an MD5 object).
The first option is to set the system property "com.twmacinta.util.MD5.NO_NATIVE_LIB" to "true" or "1". Unfortunately, SecurityManagers may also choose to disallow system property setting, so this won't be of use in all cases.
The second option is to call com.twmacinta.util.MD5.initNativeLibrary(true) before any MD5 objects are constructed.
Constructor Summary | |
---|---|
MD5()
Class constructor |
|
MD5(Object ob)
Initialize class, and update hash with ob.toString() |
Method Summary | |
---|---|
static byte[] |
asByte(String buf)
Turns String into array of bytes representing each couple of unsigned hex number as one byte. |
String |
asHex()
Returns 32-character hex representation of this objects hash |
static String |
asHex(byte[] hash)
Turns array of bytes into string representing each byte as unsigned hex number. |
static boolean |
equalPasswd(byte[] pwd,
byte[] cryptPwd)
|
static boolean |
equalPasswd(String pwd,
String cryptPwd)
|
byte[] |
Final()
Returns array of bytes (16 bytes) representing hash as of the current state of this object. |
static byte[] |
getHash(File f)
Calculates and returns the hash of the contents of the given file. |
static byte[] |
getHash(InputStream stream)
Calculates and returns the hash of the contents of the given stream. |
static byte[] |
getHashNio(File f)
Calculates and returns the hash of the contents of the given file using Nio file access. |
static boolean |
hashesEqual(byte[] hash1,
byte[] hash2)
Test if both hashes are equal |
void |
Init()
Initialize MD5 internal state (object can be reused just by calling Init() after every Final() |
static boolean |
initNativeLibrary(boolean disallow_lib_loading)
Disable lib loading (True) or enable it (false) |
static boolean |
initNativeLibrary(String libpath)
Initialize the Native Library from path |
static void |
main(String[] argv)
Test function |
static byte[] |
passwdCrypt(byte[] bpwd)
Crypt a password |
static String |
passwdCrypt(String pwd)
Crypt a password |
void |
Update(byte b)
Updates hash with a single byte |
void |
Update(byte[] buffer)
Updates hash with given array of bytes |
void |
Update(byte[] buffer,
int length)
Plain update, updates this object |
void |
Update(byte[] buffer,
int offset,
int length)
Plain update, updates this object |
void |
Update(org.jboss.netty.buffer.ChannelBuffer buffer)
Updates hash with given ChannelBuffer (from Netty) |
void |
Update(int i)
Update buffer with a single integer (only & 0xff part is used, as a byte) |
void |
Update(goldengate.common.digest.MD5State stat,
byte[] buffer,
int offset,
int length)
Updates hash with the bytebuffer given (using at maximum length bytes from that buffer) |
void |
Update(String s)
Update buffer with given string. |
void |
Update(String s,
String charset_name)
Update buffer with given string using the given encoding. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public MD5()
public MD5(Object ob)
ob
- Object, ob.toString() is used to update hash after
initializationMethod Detail |
---|
public void Init()
public void Update(goldengate.common.digest.MD5State stat, byte[] buffer, int offset, int length)
stat
- Which state is updatedbuffer
- Array of bytes to be hashedoffset
- Offset to buffer arraylength
- Use at maximum `length' bytes (absolute maximum is
buffer.length)public void Update(byte[] buffer, int offset, int length)
buffer
- offset
- length
- public void Update(byte[] buffer, int length)
buffer
- length
- public void Update(byte[] buffer)
buffer
- Array of bytes to use for updating the hashpublic void Update(byte b)
b
- Single byte to update the hashpublic void Update(String s)
s
- String to be update to hash (is used as s.getBytes())public void Update(String s, String charset_name) throws UnsupportedEncodingException
s
- String to be update to hash (is used as
s.getBytes(charset_name))charset_name
- The character set to use to convert s to a byte array, or null
if the "ISO8859_1" character set is desired.
UnsupportedEncodingException
- If the named charset is not supported.public void Update(int i)
i
- Integer value, which is then converted to byte as i & 0xffpublic void Update(org.jboss.netty.buffer.ChannelBuffer buffer)
ChannelBuffer
(from Netty)
buffer
- ChannelBuffer to use for updating the hash
and this buffer will not be changedpublic byte[] Final()
public static final String asHex(byte[] hash)
hash
- Array of bytes to convert to hex-string
public static final byte[] asByte(String buf)
buf
- hex string
public String asHex()
public static final boolean initNativeLibrary(boolean disallow_lib_loading)
disallow_lib_loading
-
public static final boolean initNativeLibrary(String libpath)
libpath
-
public static byte[] getHash(File f) throws IOException
f
- FileInterface to hash
IOException
public static byte[] getHashNio(File f) throws IOException
f
- for the FileInterface
IOException
public static byte[] getHash(InputStream stream) throws IOException
stream
- Stream to hash
IOException
public static boolean hashesEqual(byte[] hash1, byte[] hash2)
hash1
- hash2
-
public static final String passwdCrypt(String pwd)
pwd
- to crypt
public static final byte[] passwdCrypt(byte[] bpwd)
bpwd
- to crypt
public static final boolean equalPasswd(String pwd, String cryptPwd)
pwd
- cryptPwd
-
public static final boolean equalPasswd(byte[] pwd, byte[] cryptPwd)
pwd
- cryptPwd
-
public static void main(String[] argv)
argv
- with 2 arguments as filename to hash and full path to the
Native Library
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |