Added hasher test class and changed converter hex function to lower case

This commit is contained in:
Ziver Koc 2010-11-04 19:04:38 +00:00
parent e508fbe783
commit b9a11aff97
4 changed files with 84 additions and 25 deletions

View file

@ -42,6 +42,21 @@ public class Hasher {
return output;
}
/**
* Returns the MD5 hash of the given object
*
* @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
*
@ -57,6 +72,21 @@ public class Hasher {
return null;
}
/**
* Returns the SHA-1 hash of the given object
*
* @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
*
@ -80,7 +110,7 @@ public class Hasher {
* @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);
}
/**

View file

@ -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
*

View file

@ -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 );
}
@ -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) );

View file

@ -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");
}
}