Skip to content

Commit d99c877

Browse files
author
Tomasz Lelek
committed
BAEL-311 add exampe of one way encryption
1 parent 64efdec commit d99c877

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

‎jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33

44
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
55
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
6+
import org.jasypt.util.password.BasicPasswordEncryptor;
67
import org.jasypt.util.text.BasicTextEncryptor;
78
import org.junit.Ignore;
89
import org.junit.Test;
910

11+
import static junit.framework.Assert.assertFalse;
1012
import static junit.framework.Assert.assertNotSame;
13+
import static junit.framework.Assert.assertTrue;
1114
import static junit.framework.TestCase.assertEquals;
1215

1316
public class JasyptTest {
1417

1518
@Test
16-
public void givenTextPassword_whenDecrypt_shouldCompareToEncrypted() {
19+
public void givenTextPassword_whenDecrypt_thenCompareToEncrypted() {
1720
//given
1821
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
1922
String password = "secret-pass";
20-
textEncryptor.setPasswordCharArray(password.toCharArray());
23+
textEncryptor.setPasswordCharArray("some-random-password".toCharArray());
2124

2225
//when
2326
String myEncryptedText = textEncryptor.encrypt(password);
@@ -28,10 +31,37 @@ public void givenTextPassword_whenDecrypt_shouldCompareToEncrypted() {
2831
assertEquals(plainText, password);
2932
}
3033

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+
3161

3262
@Test
3363
@Ignore("should have installed local_policy.jar")
34-
public void givenTextPassword_whenDecrypt_shouldCompareToEncryptedWithCustomAlgorithm() {
64+
public void givenTextPassword_whenDecrypt_thenCompareToEncryptedWithCustomAlgorithm() {
3565
//given
3666
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
3767
String password = "secret-pass";
@@ -49,7 +79,7 @@ public void givenTextPassword_whenDecrypt_shouldCompareToEncryptedWithCustomAlgo
4979

5080
@Test
5181
@Ignore("should have installed local_policy.jar")
52-
public void givenTextPassword_whenDecryptOnHighPerformance_shouldDecrypt(){
82+
public void givenTextPassword_whenDecryptOnHighPerformance_thenDecrypt(){
5383
//given
5484
String password = "secret-pass";
5585
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();

0 commit comments

Comments
 (0)