Polish SCryptPasswordEncoder

* JKD8 Base64 -> Spring Security's Base64 to continue to support older JDKs
* Spaces to tabs
* Javadoc cleanup
* Remove of @Override to compile in Eclipse

Issue gh-3702
This commit is contained in:
Rob Winch
2016-03-03 14:05:26 -06:00
parent 7d02e259df
commit fc75a679d9
4 changed files with 214 additions and 191 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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,96 +25,96 @@ import org.junit.Test;
*/
public class SCryptPasswordEncoderTests {
@Test
public void matches() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
String result = encoder.encode("password");
assertThat(result).isNotEqualTo("password");
assertThat(encoder.matches("password", result)).isTrue();
}
@Test
public void matches() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
String result = encoder.encode("password");
assertThat(result).isNotEqualTo("password");
assertThat(encoder.matches("password", result)).isTrue();
}
@Test
public void unicode() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
String result = encoder.encode("passw\u9292rd");
assertThat(encoder.matches("pass\u9292\u9292rd", result)).isFalse();
assertThat(encoder.matches("passw\u9292rd", result)).isTrue();
}
@Test
public void unicode() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
String result = encoder.encode("passw\u9292rd");
assertThat(encoder.matches("pass\u9292\u9292rd", result)).isFalse();
assertThat(encoder.matches("passw\u9292rd", result)).isTrue();
}
@Test
public void notMatches() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
String result = encoder.encode("password");
assertThat(encoder.matches("bogus", result)).isFalse();
}
@Test
public void customParameters() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder(512, 8, 4, 32, 16);
String result = encoder.encode("password");
assertThat(result).isNotEqualTo("password");
assertThat(encoder.matches("password", result)).isTrue();
}
@Test
public void differentPasswordHashes() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
String password = "secret";
assertThat(encoder.encode(password)).isNotEqualTo(encoder.encode(password));
}
@Test
public void samePasswordWithDifferentParams() {
SCryptPasswordEncoder oldEncoder = new SCryptPasswordEncoder(512, 8, 4, 64, 16);
SCryptPasswordEncoder newEncoder = new SCryptPasswordEncoder();
@Test
public void notMatches() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
String result = encoder.encode("password");
assertThat(encoder.matches("bogus", result)).isFalse();
}
String password = "secret";
String oldEncodedPassword = oldEncoder.encode(password);
assertThat(newEncoder.matches(password, oldEncodedPassword)).isTrue();
}
@Test
public void customParameters() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder(512, 8, 4, 32, 16);
String result = encoder.encode("password");
assertThat(result).isNotEqualTo("password");
assertThat(encoder.matches("password", result)).isTrue();
}
@Test
public void doesntMatchNullEncodedValue() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
assertThat(encoder.matches("password", null)).isFalse();
}
@Test
public void differentPasswordHashes() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
String password = "secret";
assertThat(encoder.encode(password)).isNotEqualTo(encoder.encode(password));
}
@Test
public void doesntMatchEmptyEncodedValue() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
assertThat(encoder.matches("password", "")).isFalse();
}
@Test
public void samePasswordWithDifferentParams() {
SCryptPasswordEncoder oldEncoder = new SCryptPasswordEncoder(512, 8, 4, 64, 16);
SCryptPasswordEncoder newEncoder = new SCryptPasswordEncoder();
@Test
public void doesntMatchBogusEncodedValue() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
assertThat(encoder.matches("password", "012345678901234567890123456789")).isFalse();
}
@Test(expected = IllegalArgumentException.class)
public void invalidCpuCostParameter() {
new SCryptPasswordEncoder(Integer.MIN_VALUE, 16, 2, 32, 16);
}
@Test(expected = IllegalArgumentException.class)
public void invalidMemoryCostParameter() {
new SCryptPasswordEncoder(2, Integer.MAX_VALUE, 2, 32, 16);
}
@Test(expected = IllegalArgumentException.class)
public void invalidParallelizationParameter() {
new SCryptPasswordEncoder(2, 8, Integer.MAX_VALUE, 32, 16);
}
@Test(expected = IllegalArgumentException.class)
public void invalidSaltLengthParameter() {
new SCryptPasswordEncoder(2, 8, 1, 16, -1);
}
@Test(expected = IllegalArgumentException.class)
public void invalidKeyLengthParameter() {
new SCryptPasswordEncoder(2, 8, 1, -1, 16);
}
String password = "secret";
String oldEncodedPassword = oldEncoder.encode(password);
assertThat(newEncoder.matches(password, oldEncodedPassword)).isTrue();
}
@Test
public void doesntMatchNullEncodedValue() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
assertThat(encoder.matches("password", null)).isFalse();
}
@Test
public void doesntMatchEmptyEncodedValue() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
assertThat(encoder.matches("password", "")).isFalse();
}
@Test
public void doesntMatchBogusEncodedValue() {
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
assertThat(encoder.matches("password", "012345678901234567890123456789")).isFalse();
}
@Test(expected = IllegalArgumentException.class)
public void invalidCpuCostParameter() {
new SCryptPasswordEncoder(Integer.MIN_VALUE, 16, 2, 32, 16);
}
@Test(expected = IllegalArgumentException.class)
public void invalidMemoryCostParameter() {
new SCryptPasswordEncoder(2, Integer.MAX_VALUE, 2, 32, 16);
}
@Test(expected = IllegalArgumentException.class)
public void invalidParallelizationParameter() {
new SCryptPasswordEncoder(2, 8, Integer.MAX_VALUE, 32, 16);
}
@Test(expected = IllegalArgumentException.class)
public void invalidSaltLengthParameter() {
new SCryptPasswordEncoder(2, 8, 1, 16, -1);
}
@Test(expected = IllegalArgumentException.class)
public void invalidKeyLengthParameter() {
new SCryptPasswordEncoder(2, 8, 1, -1, 16);
}
}