use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project web3sdk by FISCO-BCOS.
the class SM2Sign method sign2.
/**
* The new sm2 signature algorithm with better performance
*
* @param message
* @param ecKeyPair
* @return
*/
public static Sign.SignatureData sign2(byte[] message, ECKeyPair ecKeyPair) {
SM2Signer sm2Signer = new SM2Signer();
ECPrivateKeyParameters eCPrivateKeyParameters = new ECPrivateKeyParameters(ecKeyPair.getPrivateKey(), eCDomainParameters);
sm2Signer.initWithCache(true, new ParametersWithID(new ParametersWithRandom(eCPrivateKeyParameters), identValue));
org.bouncycastle.crypto.digests.SM3Digest sm3Digest = new org.bouncycastle.crypto.digests.SM3Digest();
byte[] md = new byte[sm3Digest.getDigestSize()];
sm3Digest.update(message, 0, message.length);
sm3Digest.doFinal(md, 0);
sm2Signer.update(md, 0, md.length);
byte[] r = null;
byte[] s = null;
byte[] pub = null;
try {
BigInteger[] bigIntegers = sm2Signer.generateSignature2();
pub = Numeric.toBytesPadded(ecKeyPair.getPublicKey(), 64);
r = SM2Algorithm.getEncoded(bigIntegers[0]);
s = SM2Algorithm.getEncoded(bigIntegers[1]);
} catch (CryptoException e) {
throw new RuntimeException(e);
}
return new Sign.SignatureData((byte) 0, r, s, pub);
}
Aggregations