Skip to content

Commit 9004d90

Browse files
authored
Merge pull request eugenp#1266 from tomekl007/BAEL-311_jasypt
BAEL-311 added more tests
2 parents d62253f + d99c877 commit 9004d90

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

‎jasypt/pom.xml‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>parent-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>jasypt</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.jasypt</groupId>
17+
<artifactId>jasypt</artifactId>
18+
<version>${jasypt.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>${junit.version}</version>
24+
<scope>test</scope>
25+
</dependency>
26+
</dependencies>
27+
28+
<properties>
29+
<jasypt.version>1.9.2</jasypt.version>
30+
<junit.version>4.12</junit.version>
31+
</properties>
32+
33+
34+
</project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.baeldung.jasypt;
2+
3+
4+
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
5+
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
6+
import org.jasypt.util.password.BasicPasswordEncryptor;
7+
import org.jasypt.util.text.BasicTextEncryptor;
8+
import org.junit.Ignore;
9+
import org.junit.Test;
10+
11+
import static junit.framework.Assert.assertFalse;
12+
import static junit.framework.Assert.assertNotSame;
13+
import static junit.framework.Assert.assertTrue;
14+
import static junit.framework.TestCase.assertEquals;
15+
16+
public class JasyptTest {
17+
18+
@Test
19+
public void givenTextPassword_whenDecrypt_thenCompareToEncrypted() {
20+
//given
21+
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
22+
String password = "secret-pass";
23+
textEncryptor.setPasswordCharArray("some-random-password".toCharArray());
24+
25+
//when
26+
String myEncryptedText = textEncryptor.encrypt(password);
27+
assertNotSame(password, myEncryptedText); //myEncryptedText can be save in db
28+
29+
//then
30+
String plainText = textEncryptor.decrypt(myEncryptedText);
31+
assertEquals(plainText, password);
32+
}
33+
34+
@Test
35+
public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldBeSame(){
36+
String password = "secret-pass";
37+
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
38+
String encryptedPassword = passwordEncryptor.encryptPassword(password);
39+
40+
//when
41+
boolean result = passwordEncryptor.checkPassword("secret-pass", encryptedPassword);
42+
43+
//then
44+
assertTrue(result);
45+
}
46+
47+
@Test
48+
public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldNotBeSame(){
49+
String password = "secret-pass";
50+
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
51+
String encryptedPassword = passwordEncryptor.encryptPassword(password);
52+
53+
//when
54+
boolean result = passwordEncryptor.checkPassword("secret-pass-not-same", encryptedPassword);
55+
56+
//then
57+
assertFalse(result);
58+
}
59+
60+
61+
62+
@Test
63+
@Ignore("should have installed local_policy.jar")
64+
public void givenTextPassword_whenDecrypt_thenCompareToEncryptedWithCustomAlgorithm() {
65+
//given
66+
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
67+
String password = "secret-pass";
68+
encryptor.setPassword("secret-pass");
69+
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
70+
71+
//when
72+
String encryptedText = encryptor.encrypt("secret-pass");
73+
assertNotSame(password, encryptedText);
74+
75+
//then
76+
String plainText = encryptor.decrypt(encryptedText);
77+
assertEquals(plainText, password);
78+
}
79+
80+
@Test
81+
@Ignore("should have installed local_policy.jar")
82+
public void givenTextPassword_whenDecryptOnHighPerformance_thenDecrypt(){
83+
//given
84+
String password = "secret-pass";
85+
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
86+
encryptor.setPoolSize(4);
87+
encryptor.setPassword(password);
88+
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
89+
90+
//when
91+
String encryptedText = encryptor.encrypt(password);
92+
assertNotSame(password, encryptedText);
93+
94+
//then
95+
String plainText = encryptor.decrypt(encryptedText);
96+
assertEquals(plainText, password);
97+
}
98+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<module>javaslang</module>
6363
<module>javax-servlets</module>
6464
<module>javaxval</module>
65+
<module>jasypt</module>
6566
<module>jaxb</module>
6667
<module>jee7</module>
6768
<module>jjwt</module>

0 commit comments

Comments
 (0)