|
| 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 | +} |
0 commit comments