diff --git a/src/zutil/Hasher.java b/src/zutil/Hasher.java index 7e7a067..7db683f 100644 --- a/src/zutil/Hasher.java +++ b/src/zutil/Hasher.java @@ -16,9 +16,9 @@ public class Hasher { /** * Returns a hash of a file * - * @param file is the path to the file - * @param hashType is the hash type - * @return a String with the hash + * @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" @@ -45,8 +45,23 @@ public class Hasher { /** * Returns the MD5 hash of the given object * - * @param object is the object to hash - * @return an String containing the hash + * @param object is the String to hash + * @return an String containing the hash + */ + public static String MD5(String str){ + try { + return hash(str, "MD5"); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * Returns the MD5 hash of the given object + * + * @param object is the object to hash + * @return an String containing the hash */ public static String MD5(Serializable object){ try { @@ -60,8 +75,23 @@ public class Hasher { /** * Returns the SHA-1 hash of the given object * - * @param object is the object to hash - * @return an String containing the hash + * @param str is the String to hash + * @return an String containing the hash + */ + public static String SHA1(String str){ + try { + return hash(str, "SHA-1"); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * Returns the SHA-1 hash of the given object + * + * @param object is the object to hash + * @return an String containing the hash */ public static String SHA1(Serializable object){ try { @@ -75,20 +105,20 @@ public class Hasher { /** * Hashes the given String as UTF8 * - * @param object is the String - * @param hashType is the hash algorithm (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 ) - * @return a hex String of the hash + * @param object is the String + * @param hashType is the hash algorithm (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 ) + * @return a hex String of the hash */ public static String hash(String object, String hashType) throws Exception { - return hash(object.getBytes("UTF8"), hashType);//(new BASE64Encoder()).encode(raw); + return hash(object.getBytes(), hashType);//(new BASE64Encoder()).encode(raw); } /** * Returns the hash of the given object * - * @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 + * @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); @@ -97,9 +127,9 @@ public class Hasher { /** * 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 + * @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 { @@ -114,9 +144,9 @@ public class Hasher { /** * MurmurHash2 ported from c++ source * - * @param object is the Key - * @param seed is the seed - * @return A MurmurHash of the key + * @param object is the Key + * @param seed is the seed + * @return A MurmurHash of the key */ public static int MurmurHash(Serializable object, int seed) throws Exception{ byte[] data = Converter.toBytes(object); diff --git a/src/zutil/converters/Converter.java b/src/zutil/converters/Converter.java index 1b6ba93..4bb6554 100644 --- a/src/zutil/converters/Converter.java +++ b/src/zutil/converters/Converter.java @@ -108,7 +108,7 @@ public class Converter { } /** array needed for byteToHex */ - private static char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; + private static char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; /** * Converts a byte Array to a Hex String * diff --git a/src/zutil/network/http/HTTPHeaderParser.java b/src/zutil/network/http/HTTPHeaderParser.java index 2aaa805..5fd7ebf 100644 --- a/src/zutil/network/http/HTTPHeaderParser.java +++ b/src/zutil/network/http/HTTPHeaderParser.java @@ -37,7 +37,7 @@ public class HTTPHeaderParser { String tmp = null; if( (tmp=in.readLine()) != null && !tmp.isEmpty() ){ - parseFirstLine( tmp ); + parseStatusLine( tmp ); while( (tmp=in.readLine()) != null && !tmp.isEmpty() ){ parseLine( tmp ); } @@ -48,7 +48,7 @@ public class HTTPHeaderParser { /** * Parses the HTTP header information from an String * - * @param in is the string + * @param in is the string */ public HTTPHeaderParser(String in){ url_attr = new HashMap(); @@ -59,7 +59,7 @@ public class HTTPHeaderParser { sc.useDelimiter("\n"); String tmp = null; if( sc.hasNext() && !(tmp=sc.next()).isEmpty() ){ - parseFirstLine( tmp ); + parseStatusLine( tmp ); while( sc.hasNext() && !(tmp=sc.next()).isEmpty() ){ parseLine( tmp ); } @@ -75,7 +75,7 @@ public class HTTPHeaderParser { * @param map The HashMap to put the variables to * @return The path and file name as a String */ - protected void parseFirstLine(String line){ + protected void parseStatusLine(String line){ // Server Response if( line.startsWith("HTTP/") ){ version = Float.parseFloat( line.substring( 5 , 8) ); diff --git a/src/zutil/test/HasherTest.java b/src/zutil/test/HasherTest.java new file mode 100644 index 0000000..a911313 --- /dev/null +++ b/src/zutil/test/HasherTest.java @@ -0,0 +1,29 @@ +package zutil.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import zutil.Hasher; + + +public class HasherTest { + + @Test + public void MD5Test(){ + assertEquals(Hasher.MD5("AAAABBBB"), "9da4fc50e09e5eeb8ae8149ef4f23792"); + assertEquals(Hasher.MD5("qwerty12345"), "85064efb60a9601805dcea56ec5402f7"); + assertEquals(Hasher.MD5("123456789"), "25f9e794323b453885f5181f1b624d0b"); + //assertEquals(Hasher.MD5(".,<>|!#¤%&/()=?"), "20d5cda029514fa49a8bbe854a539847"); + assertEquals(Hasher.MD5("Test45"), "fee43a4c9d88769e14ec6a1d8b80f2e7"); + } + + @Test + public void SHA1Test(){ + assertEquals(Hasher.SHA1("AAAABBBB"), "7cd188ef3a9ea7fa0ee9c62c168709695460f5c0"); + assertEquals(Hasher.SHA1("qwerty12345"), "4e17a448e043206801b95de317e07c839770c8b8"); + assertEquals(Hasher.SHA1("123456789"), "f7c3bc1d808e04732adf679965ccc34ca7ae3441"); + //assertEquals(Hasher.SHA1(".,<>|!#¤%&/()=?"), "6b3de029cdb367bb365d5154a197294ee590a77a"); + assertEquals(Hasher.SHA1("Test45"), "9194c6e64a6801e24e63a924d5843a46428d2b3a"); + } +}