From 73fb1764b8b5b8b76aa02e6be22829aff727089e Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Tue, 26 Apr 2011 19:06:45 +0100 Subject: [PATCH] SEC-1730: Fix broken KeyGenerators method. --- .../security/crypto/keygen/KeyGenerators.java | 2 +- .../crypto/keygen/KeyGeneratorsTests.java | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/springframework/security/crypto/keygen/KeyGenerators.java b/core/src/main/java/org/springframework/security/crypto/keygen/KeyGenerators.java index 5aaa49167a..6df6ae1247 100644 --- a/core/src/main/java/org/springframework/security/crypto/keygen/KeyGenerators.java +++ b/core/src/main/java/org/springframework/security/crypto/keygen/KeyGenerators.java @@ -44,7 +44,7 @@ public class KeyGenerators { * @param keyLength the key length in bytes, e.g. 16, for a 16 byte key. */ public static BytesKeyGenerator shared(int keyLength) { - return new SharedKeyGenerator(secureRandom(16).generateKey()); + return new SharedKeyGenerator(secureRandom(keyLength).generateKey()); } /** diff --git a/core/src/test/java/org/springframework/security/crypto/keygen/KeyGeneratorsTests.java b/core/src/test/java/org/springframework/security/crypto/keygen/KeyGeneratorsTests.java index ae01e03628..3d56f4c31f 100644 --- a/core/src/test/java/org/springframework/security/crypto/keygen/KeyGeneratorsTests.java +++ b/core/src/test/java/org/springframework/security/crypto/keygen/KeyGeneratorsTests.java @@ -1,7 +1,6 @@ package org.springframework.security.crypto.keygen; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.*; import java.util.Arrays; @@ -22,14 +21,24 @@ public class KeyGeneratorsTests { @Test public void secureRandomCustomLength() { - BytesKeyGenerator keyGenerator = KeyGenerators.secureRandom(16); - assertEquals(16, keyGenerator.getKeyLength()); + BytesKeyGenerator keyGenerator = KeyGenerators.secureRandom(21); + assertEquals(21, keyGenerator.getKeyLength()); byte[] key = keyGenerator.generateKey(); - assertEquals(16, key.length); + assertEquals(21, key.length); byte[] key2 = keyGenerator.generateKey(); 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 public void string() { StringKeyGenerator keyGenerator = KeyGenerators.string();