diff --git a/jasypt/pom.xml b/jasypt/pom.xml new file mode 100644 index 0000000000..7e0c51f0b9 --- /dev/null +++ b/jasypt/pom.xml @@ -0,0 +1,34 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + jasypt + + + + org.jasypt + jasypt + ${jasypt.version} + + + junit + junit + ${junit.version} + test + + + + + 1.9.2 + 4.12 + + + + \ No newline at end of file diff --git a/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java new file mode 100644 index 0000000000..c4bed5de83 --- /dev/null +++ b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java @@ -0,0 +1,98 @@ +package org.baeldung.jasypt; + + +import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; +import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; +import org.jasypt.util.password.BasicPasswordEncryptor; +import org.jasypt.util.text.BasicTextEncryptor; +import org.junit.Ignore; +import org.junit.Test; + +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNotSame; +import static junit.framework.Assert.assertTrue; +import static junit.framework.TestCase.assertEquals; + +public class JasyptTest { + + @Test + public void givenTextPassword_whenDecrypt_thenCompareToEncrypted() { + //given + BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); + String password = "secret-pass"; + textEncryptor.setPasswordCharArray("some-random-password".toCharArray()); + + //when + String myEncryptedText = textEncryptor.encrypt(password); + assertNotSame(password, myEncryptedText); //myEncryptedText can be save in db + + //then + String plainText = textEncryptor.decrypt(myEncryptedText); + 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 + @Ignore("should have installed local_policy.jar") + public void givenTextPassword_whenDecrypt_thenCompareToEncryptedWithCustomAlgorithm() { + //given + StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); + String password = "secret-pass"; + encryptor.setPassword("secret-pass"); + encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); + + //when + String encryptedText = encryptor.encrypt("secret-pass"); + assertNotSame(password, encryptedText); + + //then + String plainText = encryptor.decrypt(encryptedText); + assertEquals(plainText, password); + } + + @Test + @Ignore("should have installed local_policy.jar") + public void givenTextPassword_whenDecryptOnHighPerformance_thenDecrypt(){ + //given + String password = "secret-pass"; + PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); + encryptor.setPoolSize(4); + encryptor.setPassword(password); + encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); + + //when + String encryptedText = encryptor.encrypt(password); + assertNotSame(password, encryptedText); + + //then + String plainText = encryptor.decrypt(encryptedText); + assertEquals(plainText, password); + } +} diff --git a/pom.xml b/pom.xml index d859f529da..72099fc584 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,7 @@ javaslang javax-servlets javaxval + jasypt jaxb jee7 jjwt