Java Code Examples for org.bouncycastle.asn1.gm.GMObjectIdentifiers

Following code examples demonstrate how to use org.bouncycastle.asn1.gm.GMObjectIdentifiersfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.bouncycastle.asn1.gm.GMObjectIdentifiersand various code implementation of this class.

        public void configure(ConfigurableProvider configurableProvider) {
            configurableProvider.addAlgorithm("Signature.SM3WITHSM2", "org.bouncycastle.jcajce.provider.asymmetric.ec.GMSignatureSpi$sm3WithSM2");
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Alg.Alias.Signature.");
            stringBuilder.append(GMObjectIdentifiers.sm2sign_with_sm3);
            configurableProvider.addAlgorithm(stringBuilder.toString(), "SM3WITHSM2");
        } 


        }

        public void configure(ConfigurableProvider provider)
        {
            provider.addAlgorithm("MessageDigest.SM3", PREFIX + "$Digest");
            provider.addAlgorithm("Alg.Alias.MessageDigest.SM3", "SM3");
            provider.addAlgorithm("Alg.Alias.MessageDigest." + GMObjectIdentifiers.sm3, "SM3");
        } 

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, SignatureException {
        X9ECParameters ecParameters = GMNamedCurves.getByName("sm2p256v1");
        ECNamedCurveParameterSpec sm2Spec = new ECNamedCurveParameterSpec(
                GMObjectIdentifiers.sm2p256v1.toString()
                , ecParameters.getCurve()
                , ecParameters.getG()
                , ecParameters.getN());
        KeyPairGenerator gen = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());

        gen.initialize(sm2Spec, new SecureRandom());
        KeyPair keyPair = gen.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        ECPoint pubKeyPointQ = ((BCECPublicKey) publicKey).getQ();
        System.out.println("X: \n" + pubKeyPointQ.getXCoord());
        System.out.println("Y: \n" + pubKeyPointQ.getYCoord());
        System.out.println("SM2 public key: \n"
                + "04"
                + pubKeyPointQ.getXCoord().toString()
                + pubKeyPointQ.getYCoord().toString()
        );

        System.out.println("Public key: \n" + Hex.toHexString(publicKey.getEncoded()));
        System.out.println("Private key: \n" + Hex.toHexString(privateKey.getEncoded()));

        Signature signature = Signature.getInstance("SM3withSm2", new BouncyCastleProvider());

        
        signature.initSign(privateKey);
        byte[] plainText = "你好".getBytes(StandardCharsets.UTF_8);
        signature.update(plainText);
        byte[] signatureValue = signature.sign();
        System.out.println("signature: \n" + Hex.toHexString(signatureValue));

        
        signature.initVerify(publicKey);
        signature.update(plainText);
        System.out.println("Signature verify result: " + signature.verify(signatureValue));
    } 

    public static KeyPairGenerator generator() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
        X9ECParameters ecParameters = GMNamedCurves.getByName("sm2p256v1");
        ECNamedCurveParameterSpec sm2Spec = new ECNamedCurveParameterSpec(
                GMObjectIdentifiers.sm2p256v1.toString()
                , ecParameters.getCurve()
                , ecParameters.getG()
                , ecParameters.getN());
        KeyPairGenerator gen = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());

        gen.initialize(sm2Spec, new SecureRandom());
        return gen;
    } 

    if (sm2) {
      AlgorithmIdentifier digAlg = digestAlgorithmFinder.find(sigAlgId);
      if (GMObjectIdentifiers.sm3.equals(digAlg.getAlgorithm())) {
        return new SM2Signer();
      } else {
        throw new OperatorCreationException("cannot create SM2 signer for hash algorithm "
            + digAlg.getAlgorithm().getId());
      }
    } 

Advertisement
Advertisement