BAEL-311 add exampe of one way encryption
This commit is contained in:
parent
64efdec3ef
commit
d99c87777f
|
@ -3,21 +3,24 @@ package org.baeldung.jasypt;
|
||||||
|
|
||||||
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
|
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
|
||||||
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
|
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
|
||||||
|
import org.jasypt.util.password.BasicPasswordEncryptor;
|
||||||
import org.jasypt.util.text.BasicTextEncryptor;
|
import org.jasypt.util.text.BasicTextEncryptor;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertFalse;
|
||||||
import static junit.framework.Assert.assertNotSame;
|
import static junit.framework.Assert.assertNotSame;
|
||||||
|
import static junit.framework.Assert.assertTrue;
|
||||||
import static junit.framework.TestCase.assertEquals;
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
|
||||||
public class JasyptTest {
|
public class JasyptTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTextPassword_whenDecrypt_shouldCompareToEncrypted() {
|
public void givenTextPassword_whenDecrypt_thenCompareToEncrypted() {
|
||||||
//given
|
//given
|
||||||
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
|
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
|
||||||
String password = "secret-pass";
|
String password = "secret-pass";
|
||||||
textEncryptor.setPasswordCharArray(password.toCharArray());
|
textEncryptor.setPasswordCharArray("some-random-password".toCharArray());
|
||||||
|
|
||||||
//when
|
//when
|
||||||
String myEncryptedText = textEncryptor.encrypt(password);
|
String myEncryptedText = textEncryptor.encrypt(password);
|
||||||
|
@ -28,10 +31,37 @@ public class JasyptTest {
|
||||||
assertEquals(plainText, password);
|
assertEquals(plainText, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldBeSame(){
|
||||||
|
String password = "secret-pass";
|
||||||
|
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
|
||||||
|
String encryptedPassword = passwordEncryptor.encryptPassword(password);
|
||||||
|
|
||||||
|
//when
|
||||||
|
boolean result = passwordEncryptor.checkPassword("secret-pass", encryptedPassword);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertTrue(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldNotBeSame(){
|
||||||
|
String password = "secret-pass";
|
||||||
|
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
|
||||||
|
String encryptedPassword = passwordEncryptor.encryptPassword(password);
|
||||||
|
|
||||||
|
//when
|
||||||
|
boolean result = passwordEncryptor.checkPassword("secret-pass-not-same", encryptedPassword);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertFalse(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore("should have installed local_policy.jar")
|
@Ignore("should have installed local_policy.jar")
|
||||||
public void givenTextPassword_whenDecrypt_shouldCompareToEncryptedWithCustomAlgorithm() {
|
public void givenTextPassword_whenDecrypt_thenCompareToEncryptedWithCustomAlgorithm() {
|
||||||
//given
|
//given
|
||||||
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
|
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
|
||||||
String password = "secret-pass";
|
String password = "secret-pass";
|
||||||
|
@ -49,7 +79,7 @@ public class JasyptTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore("should have installed local_policy.jar")
|
@Ignore("should have installed local_policy.jar")
|
||||||
public void givenTextPassword_whenDecryptOnHighPerformance_shouldDecrypt(){
|
public void givenTextPassword_whenDecryptOnHighPerformance_thenDecrypt(){
|
||||||
//given
|
//given
|
||||||
String password = "secret-pass";
|
String password = "secret-pass";
|
||||||
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
|
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
|
||||||
|
|
Loading…
Reference in New Issue