|
|
@@ -6,13 +6,18 @@ import cn.hutool.crypto.BCUtil;
|
|
|
import cn.hutool.crypto.CryptoException;
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
import org.bouncycastle.crypto.CipherParameters;
|
|
|
+import org.bouncycastle.crypto.Digest;
|
|
|
import org.bouncycastle.crypto.InvalidCipherTextException;
|
|
|
+import org.bouncycastle.crypto.digests.SM3Digest;
|
|
|
import org.bouncycastle.crypto.engines.SM2Engine;
|
|
|
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
|
|
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
|
|
import org.bouncycastle.crypto.params.ParametersWithID;
|
|
|
import org.bouncycastle.crypto.params.ParametersWithRandom;
|
|
|
+import org.bouncycastle.crypto.signers.DSAEncoding;
|
|
|
+import org.bouncycastle.crypto.signers.PlainDSAEncoding;
|
|
|
import org.bouncycastle.crypto.signers.SM2Signer;
|
|
|
+import org.bouncycastle.crypto.signers.StandardDSAEncoding;
|
|
|
|
|
|
import java.security.PrivateKey;
|
|
|
import java.security.PublicKey;
|
|
|
@@ -35,10 +40,13 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
|
|
protected SM2Engine engine;
|
|
|
protected SM2Signer signer;
|
|
|
|
|
|
- private SM2Engine.Mode mode = SM2Engine.Mode.C1C3C2;
|
|
|
private ECPrivateKeyParameters privateKeyParams;
|
|
|
private ECPublicKeyParameters publicKeyParams;
|
|
|
|
|
|
+ private DSAEncoding encoding = StandardDSAEncoding.INSTANCE;
|
|
|
+ private Digest digest = new SM3Digest();
|
|
|
+ private SM2Engine.Mode mode = SM2Engine.Mode.C1C3C2;
|
|
|
+
|
|
|
// ------------------------------------------------------------------ Constructor start
|
|
|
|
|
|
/**
|
|
|
@@ -414,16 +422,51 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置加密类型
|
|
|
+ * 设置DSA signatures的编码为PlainDSAEncoding
|
|
|
+ *
|
|
|
+ * @return this
|
|
|
+ * @since 5.3.1
|
|
|
+ */
|
|
|
+ public SM2 usePlainEncoding() {
|
|
|
+ return setEncoding(PlainDSAEncoding.INSTANCE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置DSA signatures的编码
|
|
|
+ *
|
|
|
+ * @param encoding {@link DSAEncoding}实现
|
|
|
+ * @return this
|
|
|
+ * @since 5.3.1
|
|
|
+ */
|
|
|
+ public SM2 setEncoding(DSAEncoding encoding) {
|
|
|
+ this.encoding = encoding;
|
|
|
+ this.signer = null;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置Hash算法
|
|
|
+ *
|
|
|
+ * @param digest {@link Digest}实现
|
|
|
+ * @return this
|
|
|
+ * @since 5.3.1
|
|
|
+ */
|
|
|
+ public SM2 setDigest(Digest digest) {
|
|
|
+ this.digest = digest;
|
|
|
+ this.engine = null;
|
|
|
+ this.signer = null;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置SM2模式,旧版是C1C2C3,新版本是C1C3C2
|
|
|
*
|
|
|
* @param mode {@link SM2Engine.Mode}
|
|
|
* @return this
|
|
|
*/
|
|
|
public SM2 setMode(SM2Engine.Mode mode) {
|
|
|
this.mode = mode;
|
|
|
- if (null != this.engine) {
|
|
|
- this.engine = null;
|
|
|
- }
|
|
|
+ this.engine = null;
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
@@ -455,7 +498,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
|
|
*/
|
|
|
private SM2Engine getEngine() {
|
|
|
if (null == this.engine) {
|
|
|
- this.engine = new SM2Engine(this.mode);
|
|
|
+ this.engine = new SM2Engine(this.digest, this.mode);
|
|
|
}
|
|
|
return this.engine;
|
|
|
}
|
|
|
@@ -467,10 +510,9 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
|
|
*/
|
|
|
private SM2Signer getSigner() {
|
|
|
if (null == this.signer) {
|
|
|
- this.signer = new SM2Signer();
|
|
|
+ this.signer = new SM2Signer(this.encoding, this.digest);
|
|
|
}
|
|
|
return this.signer;
|
|
|
}
|
|
|
-
|
|
|
// ------------------------------------------------------------------------------------------------------------------------- Private method end
|
|
|
}
|