SEC-1730: Fix broken KeyGenerators method.

This commit is contained in:
Luke Taylor 2011-04-26 19:06:45 +01:00
parent e473897fd9
commit 73fb1764b8
2 changed files with 15 additions and 6 deletions

View File

@ -44,7 +44,7 @@ public class KeyGenerators {
* @param keyLength the key length in bytes, e.g. 16, for a 16 byte key. * @param keyLength the key length in bytes, e.g. 16, for a 16 byte key.
*/ */
public static BytesKeyGenerator shared(int keyLength) { public static BytesKeyGenerator shared(int keyLength) {
return new SharedKeyGenerator(secureRandom(16).generateKey()); return new SharedKeyGenerator(secureRandom(keyLength).generateKey());
} }
/** /**

View File

@ -1,7 +1,6 @@
package org.springframework.security.crypto.keygen; package org.springframework.security.crypto.keygen;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import java.util.Arrays; import java.util.Arrays;
@ -22,14 +21,24 @@ public class KeyGeneratorsTests {
@Test @Test
public void secureRandomCustomLength() { public void secureRandomCustomLength() {
BytesKeyGenerator keyGenerator = KeyGenerators.secureRandom(16); BytesKeyGenerator keyGenerator = KeyGenerators.secureRandom(21);
assertEquals(16, keyGenerator.getKeyLength()); assertEquals(21, keyGenerator.getKeyLength());
byte[] key = keyGenerator.generateKey(); byte[] key = keyGenerator.generateKey();
assertEquals(16, key.length); assertEquals(21, key.length);
byte[] key2 = keyGenerator.generateKey(); byte[] key2 = keyGenerator.generateKey();
assertFalse(Arrays.equals(key, key2)); assertFalse(Arrays.equals(key, key2));
} }
@Test
public void shared() throws Exception {
BytesKeyGenerator keyGenerator = KeyGenerators.shared(21);
assertEquals(21, keyGenerator.getKeyLength());
byte[] key = keyGenerator.generateKey();
assertEquals(21, key.length);
byte[] key2 = keyGenerator.generateKey();
assertTrue(Arrays.equals(key, key2));
}
@Test @Test
public void string() { public void string() {
StringKeyGenerator keyGenerator = KeyGenerators.string(); StringKeyGenerator keyGenerator = KeyGenerators.string();