BAEL-1489: Introducing Red13PasswordEncoder
Something to nether use, but explain the idea of delegation and prefixing.
This commit is contained in:
parent
372cba10bb
commit
62d9eed9fd
@ -0,0 +1,30 @@
|
||||
package com.baeldung.passwordstorage;
|
||||
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
/**
|
||||
* DISCLAIMER: Never ever use this in any production environment!
|
||||
* <p>
|
||||
* Does only work for characters.
|
||||
*/
|
||||
public class Rot13PasswordEncoder implements PasswordEncoder {
|
||||
|
||||
@Override
|
||||
public String encode(CharSequence rawPassword) {
|
||||
StringBuffer result = new StringBuffer(rawPassword.length());
|
||||
rawPassword.chars().forEach(charCode -> {
|
||||
if (charCode >= 65 && charCode <= 77 || charCode >= 97 && charCode <= 109) {
|
||||
result.append(Character.toChars(charCode + 13));
|
||||
} else if (charCode >= 78 && charCode <= 90 || charCode >= 110 && charCode <= 133) {
|
||||
result.append(Character.toChars(charCode - 13));
|
||||
}
|
||||
});
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(CharSequence rawPassword, String encodedPassword) {
|
||||
return encode(rawPassword).equals(encodedPassword);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.passwordstorage;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class Rot13PasswordEncoderTest {
|
||||
|
||||
private final Rot13PasswordEncoder encoder = new Rot13PasswordEncoder();
|
||||
|
||||
@Test
|
||||
public void given_theEncodedPassword_should_returnTheClearTextPassword() {
|
||||
String password = "baeldung";
|
||||
String encoded = encoder.encode(password);
|
||||
String actualResult = encoder.encode(encoded);
|
||||
|
||||
assertThat(actualResult, is(password));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given_correctPassword_should_returnTrue() {
|
||||
String password = "baeldung";
|
||||
String encoded = encoder.encode(password);
|
||||
boolean actualResult = encoder.matches(password, encoded);
|
||||
|
||||
assertThat(actualResult, is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given_incorrectPassword_should_returnFalse() {
|
||||
boolean actualResult = encoder.matches("baeldung", "spring");
|
||||
|
||||
assertThat(actualResult, is(false));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user