修复SymmetricCrypto.setParams和setRandom没有加锁问题

This commit is contained in:
Looly 2024-08-13 23:08:08 +08:00
parent 67b8bc1332
commit 1cd3f633c9
4 changed files with 98 additions and 51 deletions

View File

@ -2,11 +2,12 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.32(2024-08-12) # 5.8.32(2024-08-13)
### 🐣新特性 ### 🐣新特性
### 🐞Bug修复 ### 🐞Bug修复
* 【crypto 】 修复SymmetricCrypto.setParams和setRandom没有加锁问题issue#IAJIY3@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.31(2024-08-12) # 5.8.31(2024-08-12)

View File

@ -168,7 +168,12 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @param encryptBlockSize 加密块大小 * @param encryptBlockSize 加密块大小
*/ */
public void setEncryptBlockSize(int encryptBlockSize) { public void setEncryptBlockSize(int encryptBlockSize) {
lock.lock();
try{
this.encryptBlockSize = encryptBlockSize; this.encryptBlockSize = encryptBlockSize;
}finally {
lock.unlock();
}
} }
/** /**
@ -186,7 +191,12 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @param decryptBlockSize 解密块大小 * @param decryptBlockSize 解密块大小
*/ */
public void setDecryptBlockSize(int decryptBlockSize) { public void setDecryptBlockSize(int decryptBlockSize) {
lock.lock();
try{
this.decryptBlockSize = decryptBlockSize; this.decryptBlockSize = decryptBlockSize;
}finally {
lock.unlock();
}
} }
/** /**
@ -208,7 +218,12 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @since 5.4.3 * @since 5.4.3
*/ */
public void setAlgorithmParameterSpec(AlgorithmParameterSpec algorithmParameterSpec) { public void setAlgorithmParameterSpec(AlgorithmParameterSpec algorithmParameterSpec) {
lock.lock();
try{
this.cipherWrapper.setParams(algorithmParameterSpec); this.cipherWrapper.setParams(algorithmParameterSpec);
}finally {
lock.unlock();
}
} }
/** /**
@ -219,14 +234,24 @@ public class AsymmetricCrypto extends AbstractAsymmetricCrypto<AsymmetricCrypto>
* @since 5.7.17 * @since 5.7.17
*/ */
public AsymmetricCrypto setRandom(SecureRandom random) { public AsymmetricCrypto setRandom(SecureRandom random) {
lock.lock();
try{
this.cipherWrapper.setRandom(random); this.cipherWrapper.setRandom(random);
}finally {
lock.unlock();
}
return this; return this;
} }
@Override @Override
public AsymmetricCrypto init(String algorithm, PrivateKey privateKey, PublicKey publicKey) { public AsymmetricCrypto init(String algorithm, PrivateKey privateKey, PublicKey publicKey) {
lock.lock();
try{
super.init(algorithm, privateKey, publicKey); super.init(algorithm, privateKey, publicKey);
initCipher(); initCipher();
}finally {
lock.unlock();
}
return this; return this;
} }

View File

@ -31,10 +31,13 @@ import java.util.Set;
public class Sign extends BaseAsymmetric<Sign> { public class Sign extends BaseAsymmetric<Sign> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** 签名,用于签名和验证 */ /**
* 签名用于签名和验证
*/
protected Signature signature; protected Signature signature;
// ------------------------------------------------------------------ Constructor start // ------------------------------------------------------------------ Constructor start
/** /**
* 构造创建新的私钥公钥对 * 构造创建新的私钥公钥对
* *
@ -113,8 +116,7 @@ public class Sign extends BaseAsymmetric<Sign> {
} }
/** /**
* 构造 * 构造<br>
*
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br> * 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
* 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做签名或验证 * 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做签名或验证
* *
@ -130,7 +132,8 @@ public class Sign extends BaseAsymmetric<Sign> {
} }
/** /**
* 构造 私钥和公钥同时为空时生成一对新的私钥和公钥<br> * 构造<br>
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
* 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做签名或验证 * 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做签名或验证
* *
* @param algorithm 算法{@link SignAlgorithm} * @param algorithm 算法{@link SignAlgorithm}
@ -141,8 +144,7 @@ public class Sign extends BaseAsymmetric<Sign> {
} }
/** /**
* 构造 * 构造<br>
*
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br> * 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
* 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做签名或验证 * 私钥和公钥可以单独传入一个如此则只能使用此钥匙来做签名或验证
* *
@ -165,8 +167,13 @@ public class Sign extends BaseAsymmetric<Sign> {
*/ */
@Override @Override
public Sign init(String algorithm, PrivateKey privateKey, PublicKey publicKey) { public Sign init(String algorithm, PrivateKey privateKey, PublicKey publicKey) {
lock.lock();
try {
signature = SecureUtil.createSignature(algorithm); signature = SecureUtil.createSignature(algorithm);
super.init(algorithm, privateKey, publicKey); super.init(algorithm, privateKey, publicKey);
} finally {
lock.unlock();
}
return this; return this;
} }
@ -178,15 +185,19 @@ public class Sign extends BaseAsymmetric<Sign> {
* @since 4.6.5 * @since 4.6.5
*/ */
public Sign setParameter(AlgorithmParameterSpec params) { public Sign setParameter(AlgorithmParameterSpec params) {
lock.lock();
try { try {
this.signature.setParameter(params); this.signature.setParameter(params);
} catch (InvalidAlgorithmParameterException e) { } catch (InvalidAlgorithmParameterException e) {
throw new CryptoException(e); throw new CryptoException(e);
} finally {
lock.unlock();
} }
return this; return this;
} }
// --------------------------------------------------------------------------------- Sign and Verify // --------------------------------------------------------------------------------- Sign and Verify
/** /**
* 生成文件签名 * 生成文件签名
* *

View File

@ -170,14 +170,13 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor,
} }
/** /**
* 设置 {@link AlgorithmParameterSpec}通常用于加盐或偏移向量 * 设置偏移向量
* *
* @param params {@link AlgorithmParameterSpec} * @param iv 偏移向量加盐
* @return 自身 * @return 自身
*/ */
public SymmetricCrypto setParams(AlgorithmParameterSpec params) { public SymmetricCrypto setIv(byte[] iv) {
this.cipherWrapper.setParams(params); return setIv(new IvParameterSpec(iv));
return this;
} }
/** /**
@ -191,13 +190,19 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor,
} }
/** /**
* 设置偏移向量 * 设置 {@link AlgorithmParameterSpec}通常用于加盐或偏移向量
* *
* @param iv 偏移向量加盐 * @param params {@link AlgorithmParameterSpec}
* @return 自身 * @return 自身
*/ */
public SymmetricCrypto setIv(byte[] iv) { public SymmetricCrypto setParams(AlgorithmParameterSpec params) {
return setIv(new IvParameterSpec(iv)); lock.lock();
try {
this.cipherWrapper.setParams(params);
} finally {
lock.unlock();
}
return this;
} }
/** /**
@ -208,7 +213,12 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor,
* @since 5.7.17 * @since 5.7.17
*/ */
public SymmetricCrypto setRandom(SecureRandom random) { public SymmetricCrypto setRandom(SecureRandom random) {
lock.lock();
try {
this.cipherWrapper.setRandom(random); this.cipherWrapper.setRandom(random);
} finally {
lock.unlock();
}
return this; return this;
} }