SEC-1659: Ensure that Digester is returning digest(digest(value)...) instead of digesting the same value multiple times.

Make it so that the Digester returns digest(digest(value)...) instead of digesting the same value multiple times. This
alligns with the OWASP recommendations at http://www.owasp.org/index.php/Hashing_Java#Hardening_against_the_attacker.27s_attack
This commit is contained in:
Rob Winch 2011-01-27 22:17:16 -06:00
parent 347a2a91a9
commit 2e822e9abe
2 changed files with 15 additions and 1 deletions

View File

@ -48,7 +48,7 @@ public class Digester {
public byte[] digest(byte[] value) {
synchronized (messageDigest) {
for (int i = 0; i < (iterations - 1); i++) {
invokeDigest(value);
value = invokeDigest(value);
}
return messageDigest.digest(value);
}

View File

@ -3,6 +3,9 @@ package org.springframework.security.crypto.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.security.MessageDigest;
import java.util.Arrays;
import org.junit.Test;
public class DigesterTests {
@ -16,4 +19,15 @@ public class DigesterTests {
assertFalse(new String(result).equals("text"));
}
@Test
public void multiPassDigest() throws Exception {
MessageDigest d = MessageDigest.getInstance("SHA-1","SUN");
d.reset();
byte[] value = "text".getBytes("UTF-8");
byte[] singlePass = d.digest(value);
byte[] multiPass = digester.digest(value);
assertFalse(Arrays.toString(singlePass) + " should not be equal to "
+ Arrays.toString(multiPass),
Arrays.equals(singlePass, multiPass));
}
}