BAEL-1489: Introducing Red13PasswordEncoder

Something to nether use, but explain the idea of delegation and prefixing.
This commit is contained in:
Holger Steinhauer
2018-02-07 12:45:24 +00:00
parent 372cba10bb
commit 62d9eed9fd
2 changed files with 66 additions and 0 deletions
@@ -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);
}
}