Merge pull request #1266 from tomekl007/BAEL-311_jasypt

BAEL-311 added more tests
This commit is contained in:
pedja4 2017-03-03 07:42:55 +01:00 committed by GitHub
commit 9004d90bc7
3 changed files with 133 additions and 0 deletions

34
jasypt/pom.xml Normal file
View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jasypt</artifactId>
<dependencies>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>${jasypt.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<jasypt.version>1.9.2</jasypt.version>
<junit.version>4.12</junit.version>
</properties>
</project>

View File

@ -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);
}
}

View File

@ -62,6 +62,7 @@
<module>javaslang</module> <module>javaslang</module>
<module>javax-servlets</module> <module>javax-servlets</module>
<module>javaxval</module> <module>javaxval</module>
<module>jasypt</module>
<module>jaxb</module> <module>jaxb</module>
<module>jee7</module> <module>jee7</module>
<module>jjwt</module> <module>jjwt</module>