add simple examples for password encoders
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package io.reflectoring.passwordencoding.encoder;
|
||||
|
||||
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
|
||||
|
||||
public class Argon2Example {
|
||||
|
||||
|
||||
public String encode(String plainPassword) {
|
||||
int saltLength = 16; // salt length in bytes
|
||||
int hashLength = 32; // hash length in bytes
|
||||
int parallelism = 1; // currently is not supported
|
||||
int memory = 4096; // memory costs
|
||||
int iterations = 3;
|
||||
|
||||
Argon2PasswordEncoder argon2PasswordEncoder = new Argon2PasswordEncoder(saltLength, hashLength, parallelism, memory, iterations);
|
||||
return argon2PasswordEncoder.encode(plainPassword);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.reflectoring.passwordencoding.encoder;
|
||||
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class BCryptExample {
|
||||
|
||||
public String encode(String plainPassword) {
|
||||
int strength = 10;
|
||||
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(strength, new SecureRandom());
|
||||
return bCryptPasswordEncoder.encode(plainPassword);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.reflectoring.passwordencoding.encoder;
|
||||
|
||||
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
|
||||
|
||||
public class Pbkdf2Example {
|
||||
|
||||
public String encode(String plainPassword) {
|
||||
|
||||
String pepper = "pepper"; // secret key used by password encoding
|
||||
int iterations = 200000; // number of hash iteration
|
||||
int hashWidth = 256; // hash with in bits
|
||||
|
||||
Pbkdf2PasswordEncoder pbkdf2PasswordEncoder = new Pbkdf2PasswordEncoder(pepper, iterations, hashWidth);
|
||||
return pbkdf2PasswordEncoder.encode(plainPassword);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.reflectoring.passwordencoding.encoder;
|
||||
|
||||
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
|
||||
|
||||
public class SCryptExample {
|
||||
|
||||
public String encode(String plainPassword) {
|
||||
int cpuCost = (int) Math.pow(2, 14); // factor to increase CPU costs
|
||||
int memoryCost = 8; // factor to increases memory usage
|
||||
int parallelization = 1; // currently nor supported by Spring Security
|
||||
int keyLength = 32; // key length in bytes
|
||||
int saltLength = 64; // salt length in bytes
|
||||
|
||||
SCryptPasswordEncoder sCryptPasswordEncoder = new SCryptPasswordEncoder(cpuCost, memoryCost, parallelization, keyLength, saltLength);
|
||||
return sCryptPasswordEncoder.encode(plainPassword);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.reflectoring.passwordencoding.encoder;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
class Argon2ExampleTest {
|
||||
|
||||
private Argon2Example argon2Example = new Argon2Example();
|
||||
|
||||
@Test
|
||||
void encode() {
|
||||
// given
|
||||
String plainPassword = "password";
|
||||
|
||||
// when
|
||||
String actual = argon2Example.encode(plainPassword);
|
||||
|
||||
// then
|
||||
assertThat(actual).startsWith("$argon2id$v=19$m=4096,t=3,p=1");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.reflectoring.passwordencoding.encoder;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
class BCryptExampleTest {
|
||||
|
||||
private BCryptExample bcryptExample = new BCryptExample();
|
||||
|
||||
@Test
|
||||
void encode() {
|
||||
// given
|
||||
String plainPassword = "password";
|
||||
|
||||
// when
|
||||
String encoded = bcryptExample.encode(plainPassword);
|
||||
|
||||
// then
|
||||
assertThat(encoded).startsWith("$2a$10");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.reflectoring.passwordencoding.encoder;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
class Pbkdf2ExampleTest {
|
||||
|
||||
private Pbkdf2Example pbkdf2Example = new Pbkdf2Example();
|
||||
|
||||
@Test
|
||||
void encode() {
|
||||
// given
|
||||
String plainPassword = "plainPassword";
|
||||
|
||||
// when
|
||||
String actual = pbkdf2Example.encode(plainPassword);
|
||||
|
||||
// then
|
||||
assertThat(actual).hasSize(80);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.reflectoring.passwordencoding.encoder;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
class SCryptExampleTest {
|
||||
|
||||
private SCryptExample sCryptExample = new SCryptExample();
|
||||
|
||||
@Test
|
||||
void encode() {
|
||||
// given
|
||||
String plainPassword = "password";
|
||||
|
||||
// when
|
||||
String actual = sCryptExample.encode(plainPassword);
|
||||
|
||||
// then
|
||||
assertThat(actual).hasSize(140);
|
||||
assertThat(actual).startsWith("$e0801");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user