Tomasz Lelek e1d136368b BAEL-311 move jasypt to libraries module (#1309)
* BEEL-311 move jasypt to libraries module

* BAEL-9: README.md file updated (#1310)

* BAEL-278: Updated README.md

* BAEL-554: Add and update README.md files

* BAEL-345: fixed assertion

* BAEL-109: Updated README.md

* BAEL-345: Added README.md

* Reinstating reactor-core module in root-level pom

* BAEL-393: Adding guide-intro module to root pom

* BAEL-9: Updated README.md

* Guide to "when" block in Kotlin pull request (#1296)

* Char array to string and string to char array test cases added

* Minor code renames

* Added groupingBy collector unit tests

* Added test case for int summary calculation on grouped results

* Added the grouping by classes to the main source path

* Reverting char array to string test class

* Reverting char array to string test class

* Reverting char array to string test class

* Reverting char array to string test class

* Unit test class for Kotlin when block + required types

* Minor changes to kotlin when block tests

* Minor change

* Minor change

* Bael 655 (#1256)

BAEL-655 hbase

* Remove unnecessary files and update .gitignore (#1313)

* BAEL-311 Removed jasypt module from parent pom (moved into libraries module)
2017-03-06 18:04:28 +01:00

97 lines
3.3 KiB
Java

package com.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.*;
import static junit.framework.TestCase.assertEquals;
public class JasyptTest {
@Test
public void givenTextPrivateData_whenDecrypt_thenCompareToEncrypted() {
//given
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
String privateData = "secret-data";
textEncryptor.setPasswordCharArray("some-random-data".toCharArray());
//when
String myEncryptedText = textEncryptor.encrypt(privateData);
assertNotSame(privateData, myEncryptedText); //myEncryptedText can be save in db
//then
String plainText = textEncryptor.decrypt(myEncryptedText);
assertEquals(plainText, privateData);
}
@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 givenTextPrivateData_whenDecrypt_thenCompareToEncryptedWithCustomAlgorithm() {
//given
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
String privateData = "secret-data";
encryptor.setPassword("some-random-data");
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
//when
String encryptedText = encryptor.encrypt("secret-pass");
assertNotSame(privateData, encryptedText);
//then
String plainText = encryptor.decrypt(encryptedText);
assertEquals(plainText, privateData);
}
@Test
@Ignore("should have installed local_policy.jar")
public void givenTextPrivateData_whenDecryptOnHighPerformance_thenDecrypt(){
//given
String privateData = "secret-data";
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setPoolSize(4);
encryptor.setPassword("some-random-data");
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
//when
String encryptedText = encryptor.encrypt(privateData);
assertNotSame(privateData, encryptedText);
//then
String plainText = encryptor.decrypt(encryptedText);
assertEquals(plainText, privateData);
}
}