|
22 | 22 | import org.springframework.security.crypto.codec.Hex;
|
23 | 23 | import org.springframework.security.crypto.keygen.BytesKeyGenerator;
|
24 | 24 |
|
| 25 | +import javax.crypto.SecretKey; |
| 26 | +import javax.crypto.spec.PBEKeySpec; |
| 27 | + |
25 | 28 | import static org.assertj.core.api.Assertions.assertThat;
|
26 | 29 | import static org.mockito.Mockito.mock;
|
27 | 30 | import static org.mockito.Mockito.when;
|
28 | 31 | import static org.springframework.security.crypto.encrypt.AesBytesEncryptor.CipherAlgorithm.GCM;
|
| 32 | +import static org.springframework.security.crypto.encrypt.CipherUtils.newSecretKey; |
| 33 | +import static org.springframework.security.crypto.password.Pbkdf2PasswordEncoder.SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA1; |
29 | 34 |
|
30 | 35 | /**
|
31 | 36 | * Tests for {@link AesBytesEncryptor}
|
@@ -69,6 +74,23 @@ public void roundtripWhenUsingDefaultCipherThenEncryptsAndDecrypts() {
|
69 | 74 | public void roundtripWhenUsingGcmThenEncryptsAndDecrypts() {
|
70 | 75 | CryptoAssumptions.assumeGCMJCE();
|
71 | 76 | AesBytesEncryptor encryptor = new AesBytesEncryptor(this.password, this.hexSalt, this.generator, GCM);
|
| 77 | + |
| 78 | + byte[] encryption = encryptor.encrypt(this.secret.getBytes()); |
| 79 | + assertThat(new String(Hex.encode(encryption))) |
| 80 | + .isEqualTo("4b0febebd439db7ca77153cb254520c3e4d61ae38207b4e42b820d311dc3d4e0e2f37ed5ee"); |
| 81 | + |
| 82 | + byte[] decryption = encryptor.decrypt(encryption); |
| 83 | + assertThat(new String(decryption)).isEqualTo(this.secret); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void roundtripWhenUsingSecretKeyThenEncryptsAndDecrypts() { |
| 88 | + CryptoAssumptions.assumeGCMJCE(); |
| 89 | + PBEKeySpec keySpec = new PBEKeySpec(this.password.toCharArray(), Hex.decode(this.hexSalt), |
| 90 | + 1024, 256); |
| 91 | + SecretKey secretKey = newSecretKey(PBKDF2WithHmacSHA1.name(), keySpec); |
| 92 | + AesBytesEncryptor encryptor = new AesBytesEncryptor(secretKey, this.generator, GCM); |
| 93 | + |
72 | 94 | byte[] encryption = encryptor.encrypt(this.secret.getBytes());
|
73 | 95 | assertThat(new String(Hex.encode(encryption)))
|
74 | 96 | .isEqualTo("4b0febebd439db7ca77153cb254520c3e4d61ae38207b4e42b820d311dc3d4e0e2f37ed5ee");
|
|
0 commit comments