Updated the HashCalculator class to add some more functionality

This commit is contained in:
Joshua Kissoon 2014-04-05 22:45:01 +05:30
parent 33f9eea09f
commit 1a5991404d

View File

@ -33,6 +33,28 @@ public class HashCalculator
return md.digest();
}
/**
* Computes the SHA-1 Hash using a Salt.
*
* @param toHash The string to hash
* @param salt A salt used to blind the hash
*
* @return byte[20] The hashed string
*
* @throws java.security.NoSuchAlgorithmException
*/
public static byte[] sha1Hash(String toHash, String salt) throws NoSuchAlgorithmException
{
/* Create a MessageDigest */
MessageDigest md = MessageDigest.getInstance("SHA-1");
/* Add password bytes to digest */
md.update(toHash.getBytes());
/* Get the hashed bytes */
return md.digest(salt.getBytes());
}
/**
* Computes the MD5 Hash.
*
@ -42,7 +64,7 @@ public class HashCalculator
*
* @throws java.security.NoSuchAlgorithmException
*/
public byte[] md5Hash(String toHash) throws NoSuchAlgorithmException
public static byte[] md5Hash(String toHash) throws NoSuchAlgorithmException
{
/* Create a MessageDigest */
MessageDigest md = MessageDigest.getInstance("MD5");
@ -53,4 +75,26 @@ public class HashCalculator
/* Get the hashed bytes */
return md.digest();
}
/**
* Computes the MD5 Hash using a salt.
*
* @param toHash The string to hash
* @param salt A salt used to blind the hash
*
* @return byte[16] The hashed string
*
* @throws java.security.NoSuchAlgorithmException
*/
public static byte[] md5Hash(String toHash, String salt) throws NoSuchAlgorithmException
{
/* Create a MessageDigest */
MessageDigest md = MessageDigest.getInstance("MD5");
/* Add password bytes to digest */
md.update(toHash.getBytes());
/* Get the hashed bytes */
return md.digest(salt.getBytes());
}
}