added an binary function

This commit is contained in:
Ziver Koc 2009-05-07 21:40:13 +00:00
parent 2627894e19
commit c53449208d

View file

@ -14,11 +14,9 @@ public class Hasher {
/** /**
* Returns a hash of a file * Returns a hash of a file
* *
* @param file The path to the file * @param file is the path to the file
* @param hashType The hash type * @param hashType is the hash type
* @return A String with the hash * @return a String with the hash
* @throws NoSuchAlgorithmException
* @throws IOException
*/ */
public static String hash(File file, String hashType) throws NoSuchAlgorithmException, IOException { public static String hash(File file, String hashType) throws NoSuchAlgorithmException, IOException {
MessageDigest digest = MessageDigest.getInstance(hashType);//"MD5" MessageDigest digest = MessageDigest.getInstance(hashType);//"MD5"
@ -46,8 +44,8 @@ public class Hasher {
/** /**
* Returns the MD5 hash of the given object * Returns the MD5 hash of the given object
* *
* @param object The object to hash * @param object is the object to hash
* @return String containing the hash * @return an String containing the hash
*/ */
public static String MD5(Serializable object){ public static String MD5(Serializable object){
try { try {
@ -61,8 +59,8 @@ public class Hasher {
/** /**
* Returns the SHA-1 hash of the given object * Returns the SHA-1 hash of the given object
* *
* @param object The object to hash * @param object is the object to hash
* @return String containing the hash * @return an String containing the hash
*/ */
public static String SHA1(Serializable object){ public static String SHA1(Serializable object){
try { try {
@ -76,29 +74,37 @@ public class Hasher {
/** /**
* Returns the hash of the given object * Returns the hash of the given object
* *
* @param object The object to hash * @param object is the object to hash
* @param hashType The hash method (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 ) * @param hashType is the hash method (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 )
* @return String containing the hash * @return an String containing the hash
* @throws NoSuchAlgorithmException
* @throws IOException
*/ */
public static String hash(Serializable object, String hashType) throws Exception { public static String hash(Serializable object, String hashType) throws Exception {
return hash(Converter.toBytes(object), hashType);//(new BASE64Encoder()).encode(raw);
}
/**
* Hashes a given byte array
*
* @param data is the byte array to hash
* @param hashType is the hash method (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 )
* @return an String containing the hash
* @throws Exception
*/
public static String hash(byte[] data, String hashType) throws Exception {
MessageDigest md = null; MessageDigest md = null;
md = MessageDigest.getInstance(hashType); //MD5 || SHA md = MessageDigest.getInstance(hashType); //MD5 || SHA
md.update(Converter.toBytes(object)); md.update(data);
byte raw[] = md.digest(); byte raw[] = md.digest();
return Converter.toHexString(raw);//(new BASE64Encoder()).encode(raw); return Converter.toHexString(raw);//(new BASE64Encoder()).encode(raw);
} }
/** /**
* MurmurHash2 ported from cpp source * MurmurHash2 ported from c++ source
* *
* @param object The Key * @param object is the Key
* @param seed Seed * @param seed is the seed
* @return A MurmurHash of the key * @return A MurmurHash of the key
* @throws Exception
*/ */
public static int MurmurHash(Serializable object, int seed) throws Exception{ public static int MurmurHash(Serializable object, int seed) throws Exception{
byte[] data = Converter.toBytes(object); byte[] data = Converter.toBytes(object);