Reformat code using spring-javaformat

Run `./gradlew format` to reformat all java files.

Issue gh-8945
This commit is contained in:
Phillip Webb
2020-08-10 16:39:17 -05:00
committed by Rob Winch
parent 81d9c6cac5
commit b7fc18262d
2487 changed files with 41506 additions and 46548 deletions

View File

@@ -31,18 +31,16 @@ public class Argon2EncodingUtilsTests {
private TestDataEntry testDataEntry1 = new TestDataEntry(
"$argon2i$v=19$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs",
new Argon2EncodingUtils.Argon2Hash(decoder.decode("cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs"),
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_i)).
withVersion(19).withMemoryAsKB(1024).withIterations(3).withParallelism(2).
withSalt("cRdFbCw23gz2Mlxk".getBytes()).build()
));
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_i)).withVersion(19).withMemoryAsKB(1024)
.withIterations(3).withParallelism(2).withSalt("cRdFbCw23gz2Mlxk".getBytes()).build()));
private TestDataEntry testDataEntry2 = new TestDataEntry(
"$argon2id$v=19$m=333,t=5,p=2$JDR8N3k1QWx0$+PrEoHOHsWkU9lnsxqnOFrWTVEuOh7ZRIUIbe2yUG8FgTYNCWJfHQI09JAAFKzr2JAvoejEpTMghUt0WsntQYA",
new Argon2EncodingUtils.Argon2Hash(decoder.decode("+PrEoHOHsWkU9lnsxqnOFrWTVEuOh7ZRIUIbe2yUG8FgTYNCWJfHQI09JAAFKzr2JAvoejEpTMghUt0WsntQYA"),
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)).
withVersion(19).withMemoryAsKB(333).withIterations(5).withParallelism(2).
withSalt("$4|7y5Alt".getBytes()).build()
));
new Argon2EncodingUtils.Argon2Hash(
decoder.decode(
"+PrEoHOHsWkU9lnsxqnOFrWTVEuOh7ZRIUIbe2yUG8FgTYNCWJfHQI09JAAFKzr2JAvoejEpTMghUt0WsntQYA"),
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)).withVersion(19).withMemoryAsKB(333)
.withIterations(5).withParallelism(2).withSalt("$4|7y5Alt".getBytes()).build()));
@Test
public void decodeWhenValidEncodedHashWithIThenDecodeCorrectly() {
@@ -56,22 +54,20 @@ public class Argon2EncodingUtilsTests {
@Test
public void encodeWhenValidArgumentsWithIThenEncodeToCorrectHash() {
assertThat(Argon2EncodingUtils
.encode(testDataEntry1.decoded.getHash(), testDataEntry1.decoded.getParameters()))
assertThat(Argon2EncodingUtils.encode(testDataEntry1.decoded.getHash(), testDataEntry1.decoded.getParameters()))
.isEqualTo(testDataEntry1.encoded);
}
@Test
public void encodeWhenValidArgumentsWithID2ThenEncodeToCorrectHash() {
assertThat(Argon2EncodingUtils
.encode(testDataEntry2.decoded.getHash(), testDataEntry2.decoded.getParameters()))
assertThat(Argon2EncodingUtils.encode(testDataEntry2.decoded.getHash(), testDataEntry2.decoded.getParameters()))
.isEqualTo(testDataEntry2.encoded);
}
@Test(expected = IllegalArgumentException.class)
public void encodeWhenNonexistingAlgorithmThenThrowException() {
Argon2EncodingUtils.encode(new byte[]{0, 1, 2, 3}, (new Argon2Parameters.Builder(3)).
withVersion(19).withMemoryAsKB(333).withIterations(5).withParallelism(2).build());
Argon2EncodingUtils.encode(new byte[] { 0, 1, 2, 3 }, (new Argon2Parameters.Builder(3)).withVersion(19)
.withMemoryAsKB(333).withIterations(5).withParallelism(2).build());
}
@Test(expected = IllegalArgumentException.class)
@@ -81,70 +77,80 @@ public class Argon2EncodingUtilsTests {
@Test(expected = IllegalArgumentException.class)
public void decodeWhenNonexistingAlgorithmThenThrowException() {
Argon2EncodingUtils.decode("$argon2x$v=19$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
Argon2EncodingUtils.decode(
"$argon2x$v=19$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
}
@Test(expected = IllegalArgumentException.class)
public void decodeWhenIllegalVersionParameterThenThrowException() {
Argon2EncodingUtils.decode("$argon2i$v=x$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
Argon2EncodingUtils.decode(
"$argon2i$v=x$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
}
@Test(expected = IllegalArgumentException.class)
public void decodeWhenIllegalMemoryParameterThenThrowException() {
Argon2EncodingUtils.decode("$argon2i$v=19$m=x,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
Argon2EncodingUtils
.decode("$argon2i$v=19$m=x,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
}
@Test(expected = IllegalArgumentException.class)
public void decodeWhenIllegalIterationsParameterThenThrowException() {
Argon2EncodingUtils.decode("$argon2i$v=19$m=1024,t=x,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
Argon2EncodingUtils.decode(
"$argon2i$v=19$m=1024,t=x,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
}
@Test(expected = IllegalArgumentException.class)
public void decodeWhenIllegalParallelityParameterThenThrowException() {
Argon2EncodingUtils.decode("$argon2i$v=19$m=1024,t=3,p=x$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
Argon2EncodingUtils.decode(
"$argon2i$v=19$m=1024,t=3,p=x$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
}
@Test(expected = IllegalArgumentException.class)
public void decodeWhenMissingVersionParameterThenThrowException() {
Argon2EncodingUtils.decode("$argon2i$m=1024,t=3,p=x$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
Argon2EncodingUtils
.decode("$argon2i$m=1024,t=3,p=x$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
}
@Test(expected = IllegalArgumentException.class)
public void decodeWhenMissingMemoryParameterThenThrowException() {
Argon2EncodingUtils.decode("$argon2i$v=19$t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
Argon2EncodingUtils
.decode("$argon2i$v=19$t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
}
@Test(expected = IllegalArgumentException.class)
public void decodeWhenMissingIterationsParameterThenThrowException() {
Argon2EncodingUtils.decode("$argon2i$v=19$m=1024,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
Argon2EncodingUtils
.decode("$argon2i$v=19$m=1024,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
}
@Test(expected = IllegalArgumentException.class)
public void decodeWhenMissingParallelityParameterThenThrowException() {
Argon2EncodingUtils.decode("$argon2i$v=19$m=1024,t=3$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
Argon2EncodingUtils
.decode("$argon2i$v=19$m=1024,t=3$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs");
}
private void assertArgon2HashEquals(Argon2EncodingUtils.Argon2Hash expected, Argon2EncodingUtils.Argon2Hash actual) {
private void assertArgon2HashEquals(Argon2EncodingUtils.Argon2Hash expected,
Argon2EncodingUtils.Argon2Hash actual) {
assertThat(actual.getHash()).isEqualTo(expected.getHash());
assertThat(actual.getParameters().getSalt()).isEqualTo(expected.getParameters().getSalt());
assertThat(actual.getParameters().getType()).isEqualTo(expected.getParameters().getType());
assertThat(actual.getParameters().getVersion())
.isEqualTo(expected.getParameters().getVersion());
assertThat(actual.getParameters().getMemory())
.isEqualTo(expected.getParameters().getMemory());
assertThat(actual.getParameters().getIterations())
.isEqualTo(expected.getParameters().getIterations());
assertThat(actual.getParameters().getLanes())
.isEqualTo(expected.getParameters().getLanes());
assertThat(actual.getParameters().getVersion()).isEqualTo(expected.getParameters().getVersion());
assertThat(actual.getParameters().getMemory()).isEqualTo(expected.getParameters().getMemory());
assertThat(actual.getParameters().getIterations()).isEqualTo(expected.getParameters().getIterations());
assertThat(actual.getParameters().getLanes()).isEqualTo(expected.getParameters().getLanes());
}
private static class TestDataEntry {
String encoded;
Argon2EncodingUtils.Argon2Hash decoded;
TestDataEntry(String encoded, Argon2EncodingUtils.Argon2Hash decoded) {
this.encoded = encoded;
this.decoded = decoded;
}
}
}

View File

@@ -198,17 +198,18 @@ public class Argon2PasswordEncoderTests {
encoder.upgradeEncoding("thisIsNoValidHash");
}
private void injectPredictableSaltGen() throws Exception {
byte[] bytes = new byte[16];
Arrays.fill(bytes, (byte) 0x41);
Mockito.when(keyGeneratorMock.generateKey()).thenReturn(bytes);
//we can't use the @InjectMock-annotation because the salt-generator is set in the constructor
//and Mockito will only inject mocks if they are null
// we can't use the @InjectMock-annotation because the salt-generator is set in
// the constructor
// and Mockito will only inject mocks if they are null
Field saltGen = encoder.getClass().getDeclaredField("saltGenerator");
saltGen.setAccessible(true);
saltGen.set(encoder, keyGeneratorMock);
saltGen.setAccessible(false);
}
}

View File

@@ -79,8 +79,7 @@ public class BCryptPasswordEncoderTests {
@Test
public void $2bUnicode() {
BCryptPasswordEncoder encoder =
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B);
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B);
String result = encoder.encode("passw\u9292rd");
assertThat(encoder.matches("pass\u9292\u9292rd", result)).isFalse();
assertThat(encoder.matches("passw\u9292rd", result)).isTrue();
@@ -96,16 +95,14 @@ public class BCryptPasswordEncoderTests {
@Test
public void $2aNotMatches() {
BCryptPasswordEncoder encoder =
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2A);
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2A);
String result = encoder.encode("password");
assertThat(encoder.matches("bogus", result)).isFalse();
}
@Test
public void $2bNotMatches() {
BCryptPasswordEncoder encoder =
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B);
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B);
String result = encoder.encode("password");
assertThat(encoder.matches("bogus", result)).isFalse();
}
@@ -120,16 +117,14 @@ public class BCryptPasswordEncoderTests {
@Test
public void $2aCustomStrength() {
BCryptPasswordEncoder encoder =
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2A, 8);
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2A, 8);
String result = encoder.encode("password");
assertThat(encoder.matches("password", result)).isTrue();
}
@Test
public void $2bCustomStrength() {
BCryptPasswordEncoder encoder =
new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B, 8);
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B, 8);
String result = encoder.encode("password");
assertThat(encoder.matches("password", result)).isTrue();
}
@@ -182,7 +177,8 @@ public class BCryptPasswordEncoderTests {
}
/**
* @see <a href="https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
* @see <a href=
* "https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
*/
@Test
public void upgradeFromNullOrEmpty() {
@@ -192,7 +188,8 @@ public class BCryptPasswordEncoderTests {
}
/**
* @see <a href="https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
* @see <a href=
* "https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
*/
@Test(expected = IllegalArgumentException.class)
public void upgradeFromNonBCrypt() {

View File

@@ -25,13 +25,17 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* JUnit unit tests for BCrypt routines
*
* @author Damien Miller
*/
public class BCryptTests {
private static class TestObject<T> {
private final T password;
private final String salt;
private final String expected;
private TestObject(T password, String salt, String expected) {
@@ -39,6 +43,7 @@ public class BCryptTests {
this.salt = salt;
this.expected = expected;
}
}
private static void print(String s) {
@@ -138,21 +143,21 @@ public class BCryptTests {
"$2y$06$6Xm0gCw4g7ZNDCEp4yTisez0kSdpXEl66MvdxGidnmChIe8dFmMnq"));
testObjectsByteArray = new ArrayList<>();
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2a$06$fPIsBO8qRqkjj273rfaOI.",
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2a$06$fPIsBO8qRqkjj273rfaOI.",
"$2a$06$fPIsBO8qRqkjj273rfaOI.uiVGfgi6Z1Iz.vZr11mi/38o09TUVCy"));
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2a$08$Eq2r4G/76Wv39MzSX262hu",
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2a$08$Eq2r4G/76Wv39MzSX262hu",
"$2a$08$Eq2r4G/76Wv39MzSX262hu2lrqIItOWKIkPsMMvm5LAFD.iVB7Nmm"));
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2a$10$LgfYWkbzEvQ4JakH7rOvHe",
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2a$10$LgfYWkbzEvQ4JakH7rOvHe",
"$2a$10$LgfYWkbzEvQ4JakH7rOvHeU6pINYiHnazYxe4GikGWx9MaUr27Vpa"));
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2a$12$WApznUOJfkEGSmYRfnkrPO",
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2a$12$WApznUOJfkEGSmYRfnkrPO",
"$2a$12$WApznUOJfkEGSmYRfnkrPONS3wcUvmKuh3LpjxSs6g78T77gZta3W"));
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2b$06$FGWA8OlY6RtQhXBXuCJ8Wu",
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2b$06$FGWA8OlY6RtQhXBXuCJ8Wu",
"$2b$06$FGWA8OlY6RtQhXBXuCJ8Wu5oPJaT8BeCRmS273I6cpp5RwwjAWn7S"));
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2b$06$G6aYU7UhUEUDJBdTgq3CRe",
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2b$06$G6aYU7UhUEUDJBdTgq3CRe",
"$2b$06$G6aYU7UhUEUDJBdTgq3CRebzUYAyG8MCS3WdBk0CcPb9bfj1.3cSG"));
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2y$06$sYDFHqOcXTjBgOsqC0WCKe",
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2y$06$sYDFHqOcXTjBgOsqC0WCKe",
"$2y$06$sYDFHqOcXTjBgOsqC0WCKeOv88fqPKkuV1yGVh./TROmn1mL8gYh2"));
testObjectsByteArray.add(new TestObject<>(new byte[] { }, "$2y$06$6Xm0gCw4g7ZNDCEp4yTise",
testObjectsByteArray.add(new TestObject<>(new byte[] {}, "$2y$06$6Xm0gCw4g7ZNDCEp4yTise",
"$2y$06$6Xm0gCw4g7ZNDCEp4yTisecBqTHmLJBHxTNZa8w2hupJKsIhPWOgG"));
testObjectsByteArray.add(new TestObject<>(new byte[] { -11 }, "$2a$06$fPIsBO8qRqkjj273rfaOI.",
"$2a$06$fPIsBO8qRqkjj273rfaOI.AyMTPwvUEmZ2EdJM/p0S0eP3UQpBas."));
@@ -342,8 +347,7 @@ public class BCryptTests {
BCrypt.decode_base64("", 0);
}
private static String encode_base64(byte d[], int len)
throws IllegalArgumentException {
private static String encode_base64(byte d[], int len) throws IllegalArgumentException {
StringBuilder rs = new StringBuilder();
BCrypt.encode_base64(d, len, rs);
return rs.toString();
@@ -353,7 +357,7 @@ public class BCryptTests {
public void testBase64EncodeSimpleByteArrays() {
assertThat(encode_base64(new byte[] { 0 }, 1)).isEqualTo("..");
assertThat(encode_base64(new byte[] { 0, 0 }, 2)).isEqualTo("...");
assertThat(encode_base64(new byte[] { 0, 0 , 0 }, 3)).isEqualTo("....");
assertThat(encode_base64(new byte[] { 0, 0, 0 }, 3)).isEqualTo("....");
}
@Test
@@ -435,8 +439,8 @@ public class BCryptTests {
@Test
public void hashpwWorksWithOldRevision() {
assertThat(BCrypt.hashpw("password", "$2$05$......................")).isEqualTo(
"$2$05$......................bvpG2UfzdyW/S0ny/4YyEZrmczoJfVm");
assertThat(BCrypt.hashpw("password", "$2$05$......................"))
.isEqualTo("$2$05$......................bvpG2UfzdyW/S0ny/4YyEZrmczoJfVm");
}
@Test(expected = IllegalArgumentException.class)
@@ -454,4 +458,5 @@ public class BCryptTests {
assertThat(BCrypt.equalsNoEarlyReturn("test", "pass")).isFalse();
}
}

View File

@@ -29,15 +29,13 @@ public class Base64Tests {
public void isBase64ReturnsTrueForValidBase64() {
new Base64(); // unused
assertThat(Base64.isBase64(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C',
(byte) 'D' })).isTrue();
assertThat(Base64.isBase64(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D' })).isTrue();
}
@Test
public void isBase64ReturnsFalseForInvalidBase64() {
// Include invalid '`' character
assertThat(Base64.isBase64(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C',
(byte) '`' })).isFalse();
assertThat(Base64.isBase64(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C', (byte) '`' })).isFalse();
}
@Test(expected = NullPointerException.class)
@@ -49,4 +47,5 @@ public class Base64Tests {
public void isBase64RejectsInvalidLength() {
Base64.isBase64(new byte[] { (byte) 'A' });
}
}

View File

@@ -33,8 +33,8 @@ public class HexTests {
@Test
public void encode() {
assertThat(Hex.encode(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C',
(byte) 'D' })).isEqualTo(new char[] {'4', '1', '4', '2', '4', '3', '4', '4'});
assertThat(Hex.encode(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D' }))
.isEqualTo(new char[] { '4', '1', '4', '2', '4', '3', '4', '4' });
}
@Test
@@ -44,8 +44,7 @@ public class HexTests {
@Test
public void decode() {
assertThat(Hex.decode("41424344")).isEqualTo(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C',
(byte) 'D' });
assertThat(Hex.decode("41424344")).isEqualTo(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D' });
}
@Test

View File

@@ -37,4 +37,5 @@ public class Utf8Tests {
assertThat(decoded).isEqualTo("6048b75ed560785c");
}
}

View File

@@ -36,8 +36,11 @@ import static org.springframework.security.crypto.password.Pbkdf2PasswordEncoder
* Tests for {@link AesBytesEncryptor}
*/
public class AesBytesEncryptorTests {
private String secret = "value";
private String password = "password";
private String hexSalt = "deadbeef";
private BytesKeyGenerator generator;
@@ -86,8 +89,7 @@ public class AesBytesEncryptorTests {
@Test
public void roundtripWhenUsingSecretKeyThenEncryptsAndDecrypts() {
CryptoAssumptions.assumeGCMJCE();
PBEKeySpec keySpec = new PBEKeySpec(this.password.toCharArray(), Hex.decode(this.hexSalt),
1024, 256);
PBEKeySpec keySpec = new PBEKeySpec(this.password.toCharArray(), Hex.decode(this.hexSalt), 1024, 256);
SecretKey secretKey = newSecretKey(PBKDF2WithHmacSHA1.name(), keySpec);
AesBytesEncryptor encryptor = new AesBytesEncryptor(secretKey, this.generator, GCM);

View File

@@ -30,15 +30,18 @@ import org.springframework.security.crypto.keygen.KeyGenerators;
public class BouncyCastleAesBytesEncryptorEquivalencyTests {
private byte[] testData;
private String password;
private String salt;
private SecureRandom secureRandom = new SecureRandom();
@Before
public void setup() {
// generate random password, salt, and test data
password = UUID.randomUUID().toString();
/** insecure salt byte, recommend 64 or larger than 64*/
/** insecure salt byte, recommend 64 or larger than 64 */
byte[] saltBytes = new byte[16];
secureRandom.nextBytes(saltBytes);
salt = new String(Hex.encode(saltBytes));
@@ -49,8 +52,7 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
CryptoAssumptions.assumeCBCJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, new PredictableRandomBytesKeyGenerator(16));
testEquivalence(bcEncryptor, jceEncryptor);
}
@@ -59,8 +61,7 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
CryptoAssumptions.assumeCBCJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, KeyGenerators.secureRandom(16));
testCompatibility(bcEncryptor, jceEncryptor);
}
@@ -69,8 +70,8 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
CryptoAssumptions.assumeGCMJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
new PredictableRandomBytesKeyGenerator(16), CipherAlgorithm.GCM);
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, new PredictableRandomBytesKeyGenerator(16),
CipherAlgorithm.GCM);
testEquivalence(bcEncryptor, jceEncryptor);
}
@@ -79,8 +80,8 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
CryptoAssumptions.assumeGCMJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
KeyGenerators.secureRandom(16), CipherAlgorithm.GCM);
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, KeyGenerators.secureRandom(16),
CipherAlgorithm.GCM);
testCompatibility(bcEncryptor, jceEncryptor);
}

View File

@@ -28,7 +28,9 @@ import org.springframework.security.crypto.keygen.KeyGenerators;
public class BouncyCastleAesBytesEncryptorTests {
private byte[] testData;
private String password;
private String salt;
@Before
@@ -67,13 +69,12 @@ public class BouncyCastleAesBytesEncryptorTests {
@Test(expected = IllegalArgumentException.class)
public void bcCbcWithWrongLengthIv() {
new BouncyCastleAesCbcBytesEncryptor(password, salt,
KeyGenerators.secureRandom(8));
new BouncyCastleAesCbcBytesEncryptor(password, salt, KeyGenerators.secureRandom(8));
}
@Test(expected = IllegalArgumentException.class)
public void bcGcmWithWrongLengthIv() {
new BouncyCastleAesGcmBytesEncryptor(password, salt,
KeyGenerators.secureRandom(8));
new BouncyCastleAesGcmBytesEncryptor(password, salt, KeyGenerators.secureRandom(8));
}
}

View File

@@ -41,16 +41,12 @@ public class CryptoAssumptions {
aes256Available = Cipher.getMaxAllowedKeyLength("AES") >= 256;
}
catch (NoSuchAlgorithmException e) {
throw new AssumptionViolatedException(
cipherAlgorithm + " not available, skipping test", e);
throw new AssumptionViolatedException(cipherAlgorithm + " not available, skipping test", e);
}
catch (NoSuchPaddingException e) {
throw new AssumptionViolatedException(
cipherAlgorithm + " padding not available, skipping test", e);
throw new AssumptionViolatedException(cipherAlgorithm + " padding not available, skipping test", e);
}
Assume.assumeTrue(
"AES key length of 256 not allowed, skipping test",
aes256Available);
Assume.assumeTrue("AES key length of 256 not allowed, skipping test", aes256Available);
}

View File

@@ -29,8 +29,7 @@ public class EncryptorsTests {
assertThat(result).isNotNull();
assertThat(new String(result).equals("text")).isFalse();
assertThat(new String(encryptor.decrypt(result))).isEqualTo("text");
assertThat(new String(result)).isNotEqualTo(
new String(encryptor.encrypt("text".getBytes())));
assertThat(new String(result)).isNotEqualTo(new String(encryptor.encrypt("text".getBytes())));
}
@Test
@@ -41,8 +40,7 @@ public class EncryptorsTests {
assertThat(result).isNotNull();
assertThat(new String(result).equals("text")).isFalse();
assertThat(new String(encryptor.decrypt(result))).isEqualTo("text");
assertThat(new String(result)).isNotEqualTo(
new String(encryptor.encrypt("text".getBytes())));
assertThat(new String(result)).isNotEqualTo(new String(encryptor.encrypt("text".getBytes())));
}
@Test
@@ -70,8 +68,7 @@ public class EncryptorsTests {
@Test
public void queryableText() {
CryptoAssumptions.assumeCBCJCE();
TextEncryptor encryptor = Encryptors.queryableText("password",
"5c0744940b5c369b");
TextEncryptor encryptor = Encryptors.queryableText("password", "5c0744940b5c369b");
String result = encryptor.encrypt("text");
assertThat(result).isNotNull();
assertThat(result.equals("text")).isFalse();

View File

@@ -26,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 5.0
*/
public class PasswordEncoderFactoriesTests {
private PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
private String rawPassword = "password";

View File

@@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.*;
* @since 5.0
*/
public class Base64StringKeyGeneratorTests {
@Test(expected = IllegalArgumentException.class)
public void constructorIntWhenLessThan32ThenIllegalArgumentException() {
new Base64StringKeyGenerator(31);
@@ -63,4 +64,5 @@ public class Base64StringKeyGeneratorTests {
String result = new Base64StringKeyGenerator(Base64.getUrlEncoder(), size).generateKey();
assertThat(Base64.getUrlDecoder().decode(result.getBytes())).hasSize(size);
}
}

View File

@@ -40,6 +40,7 @@ import static org.mockito.Mockito.when;
*/
@RunWith(MockitoJUnitRunner.class)
public class DelegatingPasswordEncoderTests {
@Mock
private PasswordEncoder bcrypt;
@@ -128,8 +129,8 @@ public class DelegatingPasswordEncoderTests {
@Test
public void matchesWhenUnMappedThenIllegalArgumentException() {
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "{unmapped}" + this.rawPassword))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"unmapped\"");
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"unmapped\"");
verifyZeroInteractions(this.bcrypt, this.noop);
}
@@ -137,8 +138,8 @@ public class DelegatingPasswordEncoderTests {
@Test
public void matchesWhenNoClosingPrefixStringThenIllegalArgumentExcetion() {
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "{bcrypt" + this.rawPassword))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
verifyZeroInteractions(this.bcrypt, this.noop);
}
@@ -146,8 +147,8 @@ public class DelegatingPasswordEncoderTests {
@Test
public void matchesWhenNoStartingPrefixStringThenFalse() {
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "bcrypt}" + this.rawPassword))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
verifyZeroInteractions(this.bcrypt, this.noop);
}
@@ -155,8 +156,8 @@ public class DelegatingPasswordEncoderTests {
@Test
public void matchesWhenNoIdStringThenFalse() {
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "{}" + this.rawPassword))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"\"");
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"\"");
verifyZeroInteractions(this.bcrypt, this.noop);
}
@@ -164,8 +165,8 @@ public class DelegatingPasswordEncoderTests {
@Test
public void matchesWhenPrefixInMiddleThenFalse() {
assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "invalid" + this.bcryptEncodedPassword))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
verifyZeroInteractions(this.bcrypt, this.noop);
}
@@ -177,8 +178,8 @@ public class DelegatingPasswordEncoderTests {
DelegatingPasswordEncoder passwordEncoder = new DelegatingPasswordEncoder(this.bcryptId, this.delegates);
assertThatThrownBy(() -> passwordEncoder.matches(this.rawPassword, this.rawPassword))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
verifyZeroInteractions(this.bcrypt, this.noop);
}
@@ -212,7 +213,7 @@ public class DelegatingPasswordEncoderTests {
@Test
public void upgradeEncodingWhenIdInvalidFormatThenTrue() {
assertThat(this.passwordEncoder.upgradeEncoding("{bcrypt"+ this.encodedPassword)).isTrue();
assertThat(this.passwordEncoder.upgradeEncoding("{bcrypt" + this.encodedPassword)).isTrue();
}
@Test
@@ -237,4 +238,5 @@ public class DelegatingPasswordEncoderTests {
verifyZeroInteractions(bcrypt);
}
}

View File

@@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@SuppressWarnings("deprecation")
public class LdapShaPasswordEncoderTests {
// ~ Instance fields
// ================================================================================================
@@ -109,4 +110,5 @@ public class LdapShaPasswordEncoderTests {
// No right brace
this.sha.matches("somepassword", "{SSHA25ro4PKC8jhQZ26jVsozhX/xaP0suHgX");
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.security.crypto.password;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
@@ -72,5 +71,5 @@ public class Md4PasswordEncoderTests {
Md4PasswordEncoder encoder = new Md4PasswordEncoder();
assertThat(encoder.matches("password", "{thisissalt}6cc7924dad12ade79dfb99e424f25260"));
}
}
}

View File

@@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@SuppressWarnings("deprecation")
public class MessageDigestPasswordEncoderTests {
// ~ Methods
// ========================================================================================================
@@ -39,7 +40,7 @@ public class MessageDigestPasswordEncoderTests {
public void md5BasicFunctionality() {
MessageDigestPasswordEncoder pe = new MessageDigestPasswordEncoder("MD5");
String raw = "abc123";
assertThat(pe.matches( raw, "{THIS_IS_A_SALT}a68aafd90299d0b137de28fb4bb68573")).isTrue();
assertThat(pe.matches(raw, "{THIS_IS_A_SALT}a68aafd90299d0b137de28fb4bb68573")).isTrue();
}
@Test
@@ -119,4 +120,5 @@ public class MessageDigestPasswordEncoderTests {
public void testInvalidStrength() {
new MessageDigestPasswordEncoder("SHA-666");
}
}

View File

@@ -67,4 +67,5 @@ public class PasswordEncoderUtilsTests {
public void equalsWhenSameThenTrue() {
assertThat(PasswordEncoderUtils.equals("abcdef", "abcdef")).isTrue();
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.security.crypto.keygen.KeyGenerators;
import static org.assertj.core.api.Assertions.assertThat;
public class Pbkdf2PasswordEncoderTests {
private Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder("secret");
@Test
@@ -37,8 +38,7 @@ public class Pbkdf2PasswordEncoderTests {
@Test
public void matchesLengthChecked() {
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("password",
result.substring(0, result.length() - 2))).isFalse();
assertThat(this.encoder.matches("password", result.substring(0, result.length() - 2))).isFalse();
}
@Test
@@ -68,8 +68,7 @@ public class Pbkdf2PasswordEncoderTests {
String encodedPassword = "ab1146a8458d4ce4e65789e5a3f60e423373cfa10b01abd23739e5ae2fdc37f8e9ede4ae6da65264";
String originalEncodedPassword = "ab1146a8458d4ce4ab1146a8458d4ce4e65789e5a3f60e423373cfa10b01abd23739e5ae2fdc37f8e9ede4ae6da65264";
byte[] originalBytes = Hex.decode(originalEncodedPassword);
byte[] fixedBytes = Arrays.copyOfRange(originalBytes, saltLength,
originalBytes.length);
byte[] fixedBytes = Arrays.copyOfRange(originalBytes, saltLength, originalBytes.length);
String fixedHex = String.valueOf(Hex.encode(fixedBytes));
assertThat(fixedHex).isEqualTo(encodedPassword);
@@ -91,7 +90,8 @@ public class Pbkdf2PasswordEncoderTests {
String encodedPassword = "3FOwOMcDgxP+z1x/sv184LFY2WVD+ZGMgYP3LPOSmCcDmk1XPYvcCQ==";
assertThat(this.encoder.matches(rawPassword, encodedPassword)).isTrue();
java.util.Base64.getDecoder().decode(encodedPassword); // validate can decode as Base64
java.util.Base64.getDecoder().decode(encodedPassword); // validate can decode as
// Base64
}
@Test
@@ -111,6 +111,7 @@ public class Pbkdf2PasswordEncoderTests {
String encodedPassword = "821447f994e2b04c5014e31fa9fca4ae1cc9f2188c4ed53d3ddb5ba7980982b51a0ecebfc0b81a79";
assertThat(this.encoder.matches(rawPassword, encodedPassword)).isTrue();
}
/**
* Used to find the iteration count that takes .5 seconds.
*/
@@ -126,8 +127,7 @@ public class Pbkdf2PasswordEncoderTests {
long avg = 0;
while (avg < HALF_SECOND) {
iterations += 10000;
Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder("", iterations,
256);
Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder("", iterations, 256);
String encoded = encoder.encode("password");
System.out.println("Trying " + iterations);
long start = System.currentTimeMillis();
@@ -141,4 +141,5 @@ public class Pbkdf2PasswordEncoderTests {
}
System.out.println("Iterations " + iterations);
}
}

View File

@@ -161,5 +161,5 @@ public class SCryptPasswordEncoderTests {
public void upgradeEncodingWhenInvalidInputThenException() {
new SCryptPasswordEncoder().upgradeEncoding("not-a-scrypt-password");
}
}
}

View File

@@ -26,35 +26,34 @@ public class EncodingUtilsTests {
@Test
public void hexEncode() {
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66,
(byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1,
(byte) 0xC2 };
String result = new String(Hex.encode(bytes));
assertThat(result).isEqualTo("01ff414243c0c1c2");
}
@Test
public void hexDecode() {
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66,
(byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1,
(byte) 0xC2 };
byte[] result = Hex.decode("01ff414243c0c1c2");
assertThat(Arrays.equals(bytes, result)).isTrue();
}
@Test
public void concatenate() {
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66,
(byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1,
(byte) 0xC2 };
byte[] one = new byte[] { (byte) 0x01 };
byte[] two = new byte[] { (byte) 0xFF, (byte) 65, (byte) 66 };
byte[] three = new byte[] { (byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
assertThat(Arrays.equals(bytes,
EncodingUtils.concatenate(one, two, three))).isTrue();
assertThat(Arrays.equals(bytes, EncodingUtils.concatenate(one, two, three))).isTrue();
}
@Test
public void subArray() {
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66,
(byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 };
byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1,
(byte) 0xC2 };
byte[] two = new byte[] { (byte) 0xFF, (byte) 65, (byte) 66 };
byte[] subArray = EncodingUtils.subArray(bytes, 1, 4);
assertThat(subArray).hasSize(3);