From 42e0e158b479b48dc1500e689481eb4a01efbd7c Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Wed, 25 May 2011 19:09:08 +0100 Subject: [PATCH] Simplify Digester utility class. --- .../security/crypto/password/Digester.java | 17 ++++++----------- .../security/crypto/password/DigesterTests.java | 8 ++++---- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/springframework/security/crypto/password/Digester.java b/core/src/main/java/org/springframework/security/crypto/password/Digester.java index a74d58cd66..f5a060ab77 100644 --- a/core/src/main/java/org/springframework/security/crypto/password/Digester.java +++ b/core/src/main/java/org/springframework/security/crypto/password/Digester.java @@ -22,12 +22,12 @@ import java.security.NoSuchProviderException; /** * Helper for working with the MessageDigest API. * - * Performs 1024 iterations of the hashing algorithm per digest to aid in protecting against brute force attacks. + * Performs the configured number of iterations of the hashing algorithm per digest to aid in protecting against brute force attacks. * * @author Keith Donald * @author Luke Taylor */ -class Digester { +final class Digester { private final MessageDigest messageDigest; @@ -36,6 +36,7 @@ class Digester { /** * Create a new Digester. * @param algorithm the digest algorithm; for example, "SHA-1" or "SHA-256". + * @param iterations the number of times to apply the digest algorithm to the input */ public Digester(String algorithm, int iterations) { try { @@ -49,16 +50,10 @@ class Digester { public byte[] digest(byte[] value) { synchronized (messageDigest) { - for (int i = 0; i < (iterations - 1); i++) { - value = invokeDigest(value); + for (int i = 0; i < iterations; i++) { + value = messageDigest.digest(value); } - return messageDigest.digest(value); + return value; } } - - private byte[] invokeDigest(byte[] value) { - messageDigest.reset(); - return messageDigest.digest(value); - } - } diff --git a/core/src/test/java/org/springframework/security/crypto/password/DigesterTests.java b/core/src/test/java/org/springframework/security/crypto/password/DigesterTests.java index 56c7e4baa7..bfb40584a9 100644 --- a/core/src/test/java/org/springframework/security/crypto/password/DigesterTests.java +++ b/core/src/test/java/org/springframework/security/crypto/password/DigesterTests.java @@ -14,11 +14,11 @@ import org.springframework.security.crypto.password.Digester; public class DigesterTests { @Test - public void digestIsCorrectFor2Iterations() { - Digester digester = new Digester("SHA-1", 2); + public void digestIsCorrectFor3Iterations() { + Digester digester = new Digester("SHA-1", 3); byte[] result = digester.digest(Utf8.encode("text")); - // echo -n text | openssl sha1 -binary | openssl sha1 - assertEquals("cdcefc6a573f294e60e1d633bca3aeba450954a3", new String(Hex.encode(result))); + // echo -n text | openssl sha1 -binary | openssl sha1 -binary | openssl sha1 + assertEquals("3cfa28da425eca5b894f0af2b158adf7001e000f", new String(Hex.encode(result))); } }