From 1a5991404dba7c7c6483b84eab9bb1d4f9066bf2 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Sat, 5 Apr 2014 22:45:01 +0530 Subject: [PATCH] Updated the HashCalculator class to add some more functionality --- src/kademlia/util/hashing/HashCalculator.java | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/kademlia/util/hashing/HashCalculator.java b/src/kademlia/util/hashing/HashCalculator.java index e94b196..0076cd0 100644 --- a/src/kademlia/util/hashing/HashCalculator.java +++ b/src/kademlia/util/hashing/HashCalculator.java @@ -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()); + } }