diff --git a/README.md b/README.md index 4f43428..a5461ce 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # Java Password storage solutions 1. [Hashing](hash.java) +2. [Salted Hashing](saltedHash.java) + + +https://crackstation.net/ - Decrypt Hashes + diff --git a/hash.java b/hash.java index b759ca0..fc29999 100644 --- a/hash.java +++ b/hash.java @@ -1,7 +1,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; diff --git a/saltedHash.java b/saltedHash.java new file mode 100644 index 0000000..6734b2b --- /dev/null +++ b/saltedHash.java @@ -0,0 +1,55 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +public class saltedHash { + public static void main(String[] args) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + System.out.println("Enter a string to hash and salt: "); + String input = reader.readLine(); + + try { + String salt = bytetohex.toHexString(getSalt()); + // Remove starting 0s + while (salt.charAt(0) == '0') { + salt = salt.substring(1); + } + + String salted = input + salt; + System.out.println("Hashed as:"); + String hashed = hash.hashstring(salted); + String stored = hashed + ":"+salt; + System.out.println(stored); + + System.out.println("Enter a string to check: "); + String check = reader.readLine(); + + // Split stored by the colon + String[] parts = stored.split(":"); + // Salt the checked + String saltedCheck = check + parts[1]; + // Hash the salted checked + String hashedCheck = hash.hashstring(saltedCheck); + // Compare the hashed salted checked to the stored hash + if (hashedCheck.equals(parts[0])) { + System.out.println("Match!"); + } else { + System.out.println("No match!"); + } + + + } catch (NoSuchAlgorithmException e) { + System.out.println(e.toString()); + } + + } + + private static byte[] getSalt() throws NoSuchAlgorithmException { + SecureRandom random = new SecureRandom(); + byte[] salt = new byte[16]; + random.nextBytes(salt); + return salt; + } +}