From c53449208dd38e664c1c3e1ac48b8903452441b8 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Thu, 7 May 2009 21:40:13 +0000 Subject: [PATCH] added an binary function --- src/zutil/Hasher.java | 46 ++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/zutil/Hasher.java b/src/zutil/Hasher.java index 7e806b1..99b08b4 100644 --- a/src/zutil/Hasher.java +++ b/src/zutil/Hasher.java @@ -14,11 +14,9 @@ public class Hasher { /** * Returns a hash of a file * - * @param file The path to the file - * @param hashType The hash type - * @return A String with the hash - * @throws NoSuchAlgorithmException - * @throws IOException + * @param file is the path to the file + * @param hashType is the hash type + * @return a String with the hash */ public static String hash(File file, String hashType) throws NoSuchAlgorithmException, IOException { MessageDigest digest = MessageDigest.getInstance(hashType);//"MD5" @@ -46,8 +44,8 @@ public class Hasher { /** * Returns the MD5 hash of the given object * - * @param object The object to hash - * @return String containing the hash + * @param object is the object to hash + * @return an String containing the hash */ public static String MD5(Serializable object){ try { @@ -61,8 +59,8 @@ public class Hasher { /** * Returns the SHA-1 hash of the given object * - * @param object The object to hash - * @return String containing the hash + * @param object is the object to hash + * @return an String containing the hash */ public static String SHA1(Serializable object){ try { @@ -76,29 +74,37 @@ public class Hasher { /** * Returns the hash of the given object * - * @param object The object to hash - * @param hashType The hash method (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 ) - * @return String containing the hash - * @throws NoSuchAlgorithmException - * @throws IOException + * @param object is the object to hash + * @param hashType is the hash method (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 ) + * @return an String containing the hash */ 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; md = MessageDigest.getInstance(hashType); //MD5 || SHA - md.update(Converter.toBytes(object)); + md.update(data); byte raw[] = md.digest(); return Converter.toHexString(raw);//(new BASE64Encoder()).encode(raw); } - /** - * MurmurHash2 ported from cpp source + * MurmurHash2 ported from c++ source * - * @param object The Key - * @param seed Seed + * @param object is the Key + * @param seed is the seed * @return A MurmurHash of the key - * @throws Exception */ public static int MurmurHash(Serializable object, int seed) throws Exception{ byte[] data = Converter.toBytes(object);