FOTA v.0.9.2.7版本的 MD5 值为:2305fdf4520259063967289da5df0dc6
以下内容为反编译后的 Sign.java 源代码,内容仅作参考
package cn.hutool.crypto.asymmetric;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.crypto.CryptoException;
import cn.hutool.crypto.SecureUtil;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Collection;
import java.util.Set;
public class Sign extends BaseAsymmetric<Sign> {
protected Signature signature;
public Sign(SignAlgorithm algorithm) {
this(algorithm, (byte[]) null, (byte[]) null);
}
public Sign(String algorithm) {
this(algorithm, (byte[]) null, (byte[]) null);
}
public Sign(SignAlgorithm algorithm, String privateKeyStr, String publicKeyStr) {
this(algorithm.getValue(), SecureUtil.decode(privateKeyStr), SecureUtil.decode(publicKeyStr));
}
public Sign(SignAlgorithm algorithm, byte[] privateKey, byte[] publicKey) {
this(algorithm.getValue(), privateKey, publicKey);
}
public Sign(SignAlgorithm algorithm, KeyPair keyPair) {
this(algorithm.getValue(), keyPair);
}
public Sign(SignAlgorithm algorithm, PrivateKey privateKey, PublicKey publicKey) {
this(algorithm.getValue(), privateKey, publicKey);
}
public Sign(String algorithm, String privateKeyBase64, String publicKeyBase64) {
this(algorithm, Base64.decode(privateKeyBase64), Base64.decode(publicKeyBase64));
}
public Sign(String algorithm, byte[] privateKey, byte[] publicKey) {
this(algorithm, SecureUtil.generatePrivateKey(algorithm, privateKey), SecureUtil.generatePublicKey(algorithm, publicKey));
}
public Sign(String algorithm, KeyPair keyPair) {
this(algorithm, keyPair.getPrivate(), keyPair.getPublic());
}
public Sign(String algorithm, PrivateKey privateKey, PublicKey publicKey) {
super(algorithm, privateKey, publicKey);
}
@Override
public Sign init(String algorithm, PrivateKey privateKey, PublicKey publicKey) {
try {
this.signature = Signature.getInstance(algorithm);
super.init(algorithm, privateKey, publicKey);
return this;
} catch (NoSuchAlgorithmException e) {
throw new CryptoException(e);
}
}
public Sign setParameter(AlgorithmParameterSpec params) {
try {
this.signature.setParameter(params);
return this;
} catch (InvalidAlgorithmParameterException e) {
throw new CryptoException(e);
}
}
public byte[] sign(byte[] data) {
this.lock.lock();
try {
try {
this.signature.initSign(this.privateKey);
this.signature.update(data);
return this.signature.sign();
} catch (Exception e) {
throw new CryptoException(e);
}
} finally {
this.lock.unlock();
}
}
public boolean verify(byte[] data, byte[] sign) {
this.lock.lock();
try {
try {
this.signature.initVerify(this.publicKey);
this.signature.update(data);
return this.signature.verify(sign);
} catch (Exception e) {
throw new CryptoException(e);
}
} finally {
this.lock.unlock();
}
}
public Signature getSignature() {
return this.signature;
}
public Sign setSignature(Signature signature) {
this.signature = signature;
return this;
}
public Sign setCertificate(Certificate certificate) {
boolean[] keyUsageInfo;
if (certificate instanceof X509Certificate) {
X509Certificate cert = (X509Certificate) certificate;
Set<String> critSet = cert.getCriticalExtensionOIDs();
if (CollUtil.isNotEmpty((Collection<?>) critSet) && critSet.contains("2.5.29.15") && (keyUsageInfo = cert.getKeyUsage()) != null && !keyUsageInfo[0]) {
throw new CryptoException("Wrong key usage");
}
}
this.publicKey = certificate.getPublicKey();
return this;
}
}