diff --git a/crypto/src/main/java/org/springframework/security/crypto/keygen/Base64StringKeyGenerator.java b/crypto/src/main/java/org/springframework/security/crypto/keygen/Base64StringKeyGenerator.java index 9b28d68a723..40e89d10fb7 100644 --- a/crypto/src/main/java/org/springframework/security/crypto/keygen/Base64StringKeyGenerator.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ * * @author Joe Grandja * @author Rob Winch + * @author Andrey Litvitski * @since 5.0 */ public class Base64StringKeyGenerator implements StringKeyGenerator { @@ -67,8 +68,8 @@ public Base64StringKeyGenerator(Base64.Encoder encoder, int keyLength) { if (encoder == null) { throw new IllegalArgumentException("encode cannot be null"); } - if (keyLength < DEFAULT_KEY_LENGTH) { - throw new IllegalArgumentException("keyLength must be greater than or equal to " + DEFAULT_KEY_LENGTH); + if (keyLength <= 0) { + throw new IllegalArgumentException("keyLength must be greater than 0"); } this.encoder = encoder; this.keyGenerator = KeyGenerators.secureRandom(keyLength); diff --git a/crypto/src/test/java/org/springframework/security/crypto/keygen/Base64StringKeyGeneratorTests.java b/crypto/src/test/java/org/springframework/security/crypto/keygen/Base64StringKeyGeneratorTests.java index 4590b676835..a781513f680 100644 --- a/crypto/src/test/java/org/springframework/security/crypto/keygen/Base64StringKeyGeneratorTests.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -25,13 +25,14 @@ /** * @author Rob Winch + * @author Andrey Litvitski * @since 5.0 */ public class Base64StringKeyGeneratorTests { @Test - public void constructorIntWhenLessThan32ThenIllegalArgumentException() { - assertThatIllegalArgumentException().isThrownBy(() -> new Base64StringKeyGenerator(31)); + public void constructorIntWhenEqual0ThenIllegalArgumentException() { + assertThatIllegalArgumentException().isThrownBy(() -> new Base64StringKeyGenerator(0)); } @Test