Search in sources :

Example 1 with SM2PrivateKey

use of org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PrivateKey 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 SM2PrivateKey

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

the class SM2KeyGenerator method generatePrivateKey.

/*
     * 根据数据生成SM2私钥
     *
     * @param imei 手机序列号
     *
     * @param pin pin码
     *
     * @param rand 随机数
     *
     * @return String 私钥(16进制字符串,256bits)
     */
private SM2PrivateKey generatePrivateKey() {
    SecureRandom random = new SecureRandom();
    byte[] r = new byte[32];
    BigInteger k;
    do {
        random.nextBytes(r);
        k = new BigInteger(1, r);
    } while (!checkValidateK(k));
    byte[] in = k.toByteArray();
    SM3Digest digest = new SM3Digest();
    byte[] out = new byte[32];
    digest.update(in, 0, in.length);
    digest.doFinal(out, 0);
    String value = KeyUtils.bcdhex_to_aschex(out);
    return new SM2PrivateKey(new BigInteger(value, 16));
}
Also used : SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest) SecureRandom(java.security.SecureRandom) BigInteger(java.math.BigInteger)

Aggregations

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