|
@@ -3,6 +3,8 @@ package com.chelvc.framework.common.crypto;
|
|
|
import javax.crypto.Cipher;
|
|
|
|
|
|
import com.chelvc.framework.common.util.AESUtils;
|
|
|
+import com.chelvc.framework.common.util.AssertUtils;
|
|
|
+import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import lombok.NonNull;
|
|
|
|
|
|
/**
|
|
@@ -17,15 +19,15 @@ public class AESCipherFactory implements CipherFactory {
|
|
|
private final String secret;
|
|
|
private final String iv;
|
|
|
|
|
|
- public AESCipherFactory(@NonNull String secret) {
|
|
|
- this(secret, AESUtils.secret2iv(secret));
|
|
|
+ public AESCipherFactory(String secret) {
|
|
|
+ this(secret, ObjectUtils.ifNull(secret, AESUtils::secret2iv));
|
|
|
}
|
|
|
|
|
|
- public AESCipherFactory(@NonNull String secret, @NonNull String iv) {
|
|
|
+ public AESCipherFactory(String secret, String iv) {
|
|
|
this(AESUtils.CBC_PKCS5PADDING, secret, iv);
|
|
|
}
|
|
|
|
|
|
- public AESCipherFactory(@NonNull String name, @NonNull String secret, @NonNull String iv) {
|
|
|
+ public AESCipherFactory(@NonNull String name, String secret, String iv) {
|
|
|
this.key = name + secret;
|
|
|
this.name = name;
|
|
|
this.secret = secret;
|
|
@@ -39,6 +41,10 @@ public class AESCipherFactory implements CipherFactory {
|
|
|
|
|
|
@Override
|
|
|
public Cipher getCipher(int mode) {
|
|
|
- return AESUtils.lookupCipher(this.key, mode, () -> AESUtils.getCipher(this.name, mode, this.secret, this.iv));
|
|
|
+ return AESUtils.lookupCipher(this.key, mode, () -> {
|
|
|
+ String secret = AssertUtils.nonempty(this.secret, () -> "Cipher secret is missing");
|
|
|
+ String iv = AssertUtils.nonempty(this.iv, () -> "Cipher iv is missing");
|
|
|
+ return AESUtils.getCipher(this.name, mode, secret, iv);
|
|
|
+ });
|
|
|
}
|
|
|
}
|