Search in sources :

Example 1 with SM2PublicKey

use of org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PublicKey in project web3sdk by FISCO-BCOS.

the class GenCredential method genEcPairFromKeyPair.

private static ECKeyPair genEcPairFromKeyPair(KeyPair keyPairData) {
    try {
        SM2PrivateKey vk = (SM2PrivateKey) keyPairData.getPrivate();
        SM2PublicKey pk = (SM2PublicKey) keyPairData.getPublic();
        final byte[] publicKey = pk.getEncoded();
        final byte[] privateKey = vk.getEncoded();
        BigInteger biPublic = new BigInteger(Hex.toHexString(publicKey), 16);
        BigInteger biPrivate = new BigInteger(Hex.toHexString(privateKey), 16);
        ECKeyPair keyPair = new ECKeyPair(biPrivate, biPublic);
        return keyPair;
    } catch (Exception e) {
        logger.error("create ec_keypair of guomi failed, error msg:" + e.getMessage());
        return null;
    }
}
Also used : SM2PrivateKey(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PrivateKey) SM2PublicKey(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PublicKey) ECKeyPair(org.fisco.bcos.web3j.crypto.ECKeyPair) BigInteger(java.math.BigInteger)

Example 2 with SM2PublicKey

use of org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PublicKey in project web3sdk by FISCO-BCOS.

the class Sign method smPublicKeyFromPrivate.

/**
 * Returns sm public key from the given private key.
 *
 * @param biPrivateKey the private key to derive the public key from
 * @return BigInteger encoded public key
 */
public static BigInteger smPublicKeyFromPrivate(BigInteger biPrivateKey) {
    String privateKey = biPrivateKey.toString(16);
    SM2KeyGenerator generator = new SM2KeyGenerator();
    final KeyPair keyPairData = generator.generateKeyPair(privateKey);
    SM2PublicKey pk = (SM2PublicKey) keyPairData.getPublic();
    final byte[] publicKey = pk.getEncoded();
    BigInteger biPublicKey = new BigInteger(Hex.toHexString(publicKey), 16);
    return biPublicKey;
}
Also used : KeyPair(java.security.KeyPair) SM2PublicKey(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PublicKey) SM2KeyGenerator(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2KeyGenerator) BigInteger(java.math.BigInteger)

Aggregations

BigInteger (java.math.BigInteger)2 SM2PublicKey (org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PublicKey)2 KeyPair (java.security.KeyPair)1 ECKeyPair (org.fisco.bcos.web3j.crypto.ECKeyPair)1 SM2KeyGenerator (org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2KeyGenerator)1 SM2PrivateKey (org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PrivateKey)1