This commit is contained in:
Looly
2022-09-30 23:00:15 +08:00
parent 00fad42926
commit d6be80f8ea
22 changed files with 173 additions and 130 deletions

View File

@@ -1,5 +1,7 @@
package cn.hutool.crypto;
import cn.hutool.core.lang.func.Wrapper;
import javax.crypto.Cipher;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
@@ -18,7 +20,7 @@ import java.security.spec.AlgorithmParameterSpec;
* @author looly
* @since 5.7.17
*/
public class CipherWrapper {
public class CipherWrapper implements Wrapper<Cipher> {
private final Cipher cipher;
/**
@@ -85,7 +87,8 @@ public class CipherWrapper {
*
* @return {@link Cipher}
*/
public Cipher getCipher() {
@Override
public Cipher getRaw() {
return this.cipher;
}

View File

@@ -289,7 +289,7 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @since 5.4.3
*/
public Cipher getCipher() {
return this.cipherWrapper.getCipher();
return this.cipherWrapper.getRaw();
}
/**
@@ -364,6 +364,6 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @throws InvalidKeyException 异常KEY错误
*/
private Cipher initMode(final int mode, final Key key) throws InvalidAlgorithmParameterException, InvalidKeyException {
return this.cipherWrapper.initMode(mode, key).getCipher();
return this.cipherWrapper.initMode(mode, key).getRaw();
}
}

View File

@@ -166,7 +166,7 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor,
* @return 加密或解密
*/
public Cipher getCipher() {
return cipherWrapper.getCipher();
return cipherWrapper.getRaw();
}
/**
@@ -242,7 +242,7 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor,
* @since 5.6.8
*/
public byte[] update(final byte[] data) {
final Cipher cipher = cipherWrapper.getCipher();
final Cipher cipher = cipherWrapper.getRaw();
lock.lock();
try {
return cipher.update(paddingDataWithZero(data, cipher.getBlockSize()));
@@ -383,7 +383,7 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor,
private SymmetricCrypto initParams(final String algorithm, AlgorithmParameterSpec paramsSpec) {
if (null == paramsSpec) {
byte[] iv = Opt.ofNullable(cipherWrapper)
.map(CipherWrapper::getCipher).map(Cipher::getIV).get();
.map(CipherWrapper::getRaw).map(Cipher::getIV).get();
// 随机IV
if (StrUtil.startWithIgnoreCase(algorithm, "PBE")) {
@@ -412,7 +412,7 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor,
* @throws InvalidAlgorithmParameterException 无效算法
*/
private Cipher initMode(final int mode) throws InvalidKeyException, InvalidAlgorithmParameterException {
return this.cipherWrapper.initMode(mode, this.secretKey).getCipher();
return this.cipherWrapper.initMode(mode, this.secretKey).getRaw();
}
/**