Browse Source

remove 32-byte minimum keyLength restriction in `Base64StringKeyGenerator` (#17012)

Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
pull/17113/head
Andrey Litvitski 7 months ago committed by Rob Winch
parent
commit
3b492a9628
  1. 7
      crypto/src/main/java/org/springframework/security/crypto/keygen/Base64StringKeyGenerator.java
  2. 7
      crypto/src/test/java/org/springframework/security/crypto/keygen/Base64StringKeyGeneratorTests.java

7
crypto/src/main/java/org/springframework/security/crypto/keygen/Base64StringKeyGenerator.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,6 +24,7 @@ import java.util.Base64;
* *
* @author Joe Grandja * @author Joe Grandja
* @author Rob Winch * @author Rob Winch
* @author Andrey Litvitski
* @since 5.0 * @since 5.0
*/ */
public class Base64StringKeyGenerator implements StringKeyGenerator { public class Base64StringKeyGenerator implements StringKeyGenerator {
@ -67,8 +68,8 @@ public class Base64StringKeyGenerator implements StringKeyGenerator {
if (encoder == null) { if (encoder == null) {
throw new IllegalArgumentException("encode cannot be null"); throw new IllegalArgumentException("encode cannot be null");
} }
if (keyLength < DEFAULT_KEY_LENGTH) { if (keyLength <= 0) {
throw new IllegalArgumentException("keyLength must be greater than or equal to " + DEFAULT_KEY_LENGTH); throw new IllegalArgumentException("keyLength must be greater than 0");
} }
this.encoder = encoder; this.encoder = encoder;
this.keyGenerator = KeyGenerators.secureRandom(keyLength); this.keyGenerator = KeyGenerators.secureRandom(keyLength);

7
crypto/src/test/java/org/springframework/security/crypto/keygen/Base64StringKeyGeneratorTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,13 +25,14 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
/** /**
* @author Rob Winch * @author Rob Winch
* @author Andrey Litvitski
* @since 5.0 * @since 5.0
*/ */
public class Base64StringKeyGeneratorTests { public class Base64StringKeyGeneratorTests {
@Test @Test
public void constructorIntWhenLessThan32ThenIllegalArgumentException() { public void constructorIntWhenEqual0ThenIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> new Base64StringKeyGenerator(31)); assertThatIllegalArgumentException().isThrownBy(() -> new Base64StringKeyGenerator(0));
} }
@Test @Test

Loading…
Cancel
Save