org.bouncycastle.crypto.signers.SM2Signer Java Examples

The following examples show how to use org.bouncycastle.crypto.signers.SM2Signer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SM2.java    From julongchain with Apache License 2.0 6 votes vote down vote up
/**
 * 验证签名值
 *
 * @param publicKey
 * @param signValue
 * @param msg
 * @return
 */
public boolean verify(byte[] publicKey, byte[] signValue, byte[] msg) throws CspException {
    if (null == publicKey) {
        throw new CspException("publicKey is null");
    }
    if (publicKey.length == 0) {
        throw new CspException("publicKey's length is 0");
    }
    if (null == signValue) {
        throw new CspException("signValue is null");
    }
    if (signValue.length == 0) {
        throw new CspException("signValue's length is 0");
    }
    if (null==msg) {
        throw new CspException("plainText is null");
    }
    if (msg.length == 0) {
        throw new CspException("plainText's length is 0");
    }
    SM2Signer signer = new SM2Signer();
    ECPublicKeyParameters ecPub = new ECPublicKeyParameters(byte2ECpoint(publicKey), ecc_bc_spec);
    signer.init(false, ecPub);
    signer.update(msg, 0, msg.length);
    return signer.verifySignature(signValue);
}
 
Example #2
Source File: SM2.java    From julongchain with Apache License 2.0 5 votes vote down vote up
/**
 * 对数据进行签名
 *
 * @param privateKey
 * @param msg
 * @return
 * @throws CryptoException
 */
public byte[] sign(byte[] privateKey, byte[] msg) throws CspException {
    if (null == privateKey) {
        throw new CspException("privateKey is null");
    }
    if (privateKey.length == 0) {
        throw new CspException("privateKey's length is 0");
    }
    if (null==msg) {
        throw new CspException("plainText is null");
    }
    if (msg.length == 0) {
        throw new CspException("plainText's length is 0");
    }
    SM2Signer signer = new SM2Signer();
    BigInteger d = byte2BigInteger(privateKey);
    ECPrivateKeyParameters privateKeyParameters = new ECPrivateKeyParameters(d, ecc_bc_spec);
    signer.init(true, privateKeyParameters);
    signer.update(msg, 0, msg.length);
    byte[] sig = new byte[0];
    try {
        sig = signer.generateSignature();
    } catch (CryptoException e) {
        log.error(e.getMessage());
        throw new CspException(e);
    }
    return sig;
}
 
Example #3
Source File: P12ContentSignerBuilder.java    From xipki with Apache License 2.0 5 votes vote down vote up
protected Signer createSigner(AlgorithmIdentifier sigAlgId, AlgorithmIdentifier digAlgId)
        throws OperatorCreationException {
  if (!AlgorithmUtil.isSM2SigAlg(sigAlgId)) {
    throw new OperatorCreationException("the given algorithm is not a valid EC signature "
        + "algorithm '" + sigAlgId.getAlgorithm().getId() + "'");
  }

  return new SM2Signer();
}
 
Example #4
Source File: SM2Util.java    From javasdk with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * get signature by sm2 key pair, use default userID.
 *
 * @param keyPair ECC key pair
 * @param srcData source data
 * @return signature bytes
 * @throws CryptoException -
 */
public static byte[] sign(AsymmetricCipherKeyPair keyPair, byte[] srcData) throws CryptoException {
    SM2Signer signer = new SM2Signer();
    CipherParameters param = new ParametersWithRandom(keyPair.getPrivate(), new SecureRandom());
    signer.init(true, param);
    signer.update(srcData, 0, srcData.length);
    return signer.generateSignature();
}
 
Example #5
Source File: SM2Util.java    From javasdk with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * verify sm2 signature.
 *
 * @param ecPublicKeyParameters ecPublicKey param
 * @param sourceData source data
 * @param signature signature
 * @return is legal
 */
public static boolean verify(byte[] sourceData, byte[] signature, ECPublicKeyParameters ecPublicKeyParameters) {
    SM2Signer signer = new SM2Signer();
    signer.init(false, ecPublicKeyParameters);
    signer.update(sourceData, 0, sourceData.length);
    return signer.verifySignature(signature);
}