Class MD5GET
- java.lang.Object
-
- com.twmacinta.util.MD5GET
-
public class MD5GET extends java.lang.ObjectFast implementation of RSA's MD5GET 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 - 2008 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 MD5GET 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. MD5GET 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 "MD5GET.so" and on Windows it should be named "MD5GET.dll". The code will attempt to locate the library in the following locations in the order given:
- The path specified by the system property "com.twmacinta.util.MD5GET.NATIVE_LIB_FILE" (be sure to include "MD5GET.so" or "MD5GET.dll" as appropriate at the end of the path).
- A platform specific directory beneath the "lib/arch/" directory. On Linux for x86, this is "lib/arch/linux_x86/". On Windows for x86, this is "lib/arch/win32_x86/".
- Within the "lib/" directory.
- Within the current directory.
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.MD5GET object is constructed (note that the convenience stream objects will implicitly create an MD5GET object).
The first option is to set the system property "com.twmacinta.util.MD5GET.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.MD5GET.initNativeLibrary(true) before any MD5GET objects are constructed.
-
-
Constructor Summary
Constructors Constructor and Description MD5GET()Class constructorMD5GET(java.lang.Object ob)Initialize class, and update hash with ob.toString()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description java.lang.StringasHex()Returns 32-character hex representation of this objects hashstatic java.lang.StringasHex(byte[] hash)Turns array of bytes into string representing each byte as unsigned hex number.byte[]Final()Returns array of bytes (16 bytes) representing hash as of the current state of this object.static byte[]getHash(java.io.File f)Calculates and returns the hash of the contents of the given file.static booleanhashesEqual(byte[] hash1, byte[] hash2)voidInit()Initialize MD5GET internal state (object can be reused just by calling Init() after every Final()voidUpdate(byte b)Updates hash with a single bytevoidUpdate(byte[] buffer)Updates hash with given array of bytesvoidUpdate(byte[] buffer, int length)voidUpdate(byte[] buffer, int offset, int length)Plain update, updates this objectvoidUpdate(int i)Update buffer with a single integer (only & 0xff part is used, as a byte)voidUpdate(com.twmacinta.util.MD5State stat, byte[] buffer, int offset, int length)Updates hash with the bytebuffer given (using at maximum length bytes from that buffer)voidUpdate(java.lang.String s)Update buffer with given string.voidUpdate(java.lang.String s, java.lang.String charset_name)Update buffer with given string using the given encoding.
-
-
-
Constructor Detail
-
MD5GET
public MD5GET()
Class constructor
-
MD5GET
public MD5GET(java.lang.Object ob)
Initialize class, and update hash with ob.toString()- Parameters:
ob- Object, ob.toString() is used to update hash after initialization
-
-
Method Detail
-
Init
public void Init()
Initialize MD5GET internal state (object can be reused just by calling Init() after every Final()
-
Update
public void Update(com.twmacinta.util.MD5State stat, byte[] buffer, int offset, int length)Updates hash with the bytebuffer given (using at maximum length bytes from that buffer)- Parameters:
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)
-
Update
public void Update(byte[] buffer, int offset, int length)Plain update, updates this object
-
Update
public void Update(byte[] buffer, int length)
-
Update
public void Update(byte[] buffer)
Updates hash with given array of bytes- Parameters:
buffer- Array of bytes to use for updating the hash
-
Update
public void Update(byte b)
Updates hash with a single byte- Parameters:
b- Single byte to update the hash
-
Update
public void Update(java.lang.String s)
Update buffer with given string. Note that because the version of the s.getBytes() method without parameters is used to convert the string to a byte array, the results of this method may be different on different platforms. The s.getBytes() method converts the string into a byte array using the current platform's default character set and may therefore have different results on platforms with different default character sets. If a version that works consistently across platforms with different default character sets is desired, use the overloaded version of the Update() method which takes a string and a character encoding.- Parameters:
s- String to be update to hash (is used as s.getBytes())
-
Update
public void Update(java.lang.String s, java.lang.String charset_name) throws java.io.UnsupportedEncodingExceptionUpdate buffer with given string using the given encoding. If the given encoding is null, the encoding "ISO8859_1" is used.- Parameters:
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.- Throws:
java.io.UnsupportedEncodingException- If the named charset is not supported.
-
Update
public void Update(int i)
Update buffer with a single integer (only & 0xff part is used, as a byte)- Parameters:
i- Integer value, which is then converted to byte as i & 0xff
-
Final
public byte[] Final()
Returns array of bytes (16 bytes) representing hash as of the current state of this object. Note: getting a hash does not invalidate the hash object, it only creates a copy of the real state which is finalized.- Returns:
- Array of 16 bytes, the hash of all updated bytes
-
asHex
public static java.lang.String asHex(byte[] hash)
Turns array of bytes into string representing each byte as unsigned hex number.- Parameters:
hash- Array of bytes to convert to hex-string- Returns:
- Generated hex string
-
asHex
public java.lang.String asHex()
Returns 32-character hex representation of this objects hash- Returns:
- String of this object's hash
-
getHash
public static byte[] getHash(java.io.File f) throws java.io.IOExceptionCalculates and returns the hash of the contents of the given file.- Throws:
java.io.IOException
-
hashesEqual
public static boolean hashesEqual(byte[] hash1, byte[] hash2)- Returns:
- true if the first 16 bytes of both hash1 and hash2 are equal; both hash1 and hash2 are null; or either hash array is less than 16 bytes in length and their lengths and all of their bytes are equal.
-
-
DMelt 3.0 © DataMelt by jWork.ORG