Search in sources :

Example 1 with SM3Digest

use of org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest in project web3sdk by FISCO-BCOS.

the class EncryptType method setEncryptInterface.

private static void setEncryptInterface(int encryptType) {
    SignInterface signInterface;
    HashInterface hashInterface;
    if (encryptType == SM2_TYPE) {
        signInterface = new SM2Sign();
        hashInterface = new SM3Digest();
    } else {
        signInterface = new ECDSASign();
        hashInterface = new SHA3Digest();
    }
    Sign.setSignInterface(signInterface);
    Hash.setHashInterface(hashInterface);
}
Also used : SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest) SM2Sign(org.fisco.bcos.web3j.crypto.gm.sm2.SM2Sign)

Example 2 with SM3Digest

use of org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest in project web3sdk by FISCO-BCOS.

the class GenGmAccount method deduceAccountFromPublic.

private static String deduceAccountFromPublic(BigInteger publicKey) {
    try {
        SM3Digest sm3Digest = new SM3Digest();
        System.out.println("===GEN COUNT :" + publicKey.toString(16));
        String publicKeyNoPrefix = Numeric.cleanHexPrefix(publicKey.toString(16));
        String hashSM3String = sm3Digest.hash(publicKeyNoPrefix);
        String account = hashSM3String.substring(24);
        return "0x" + account;
    } catch (Exception e) {
        System.out.println("DeduceAccountFromPublic failed, error message:" + e.getMessage());
        return null;
    }
}
Also used : SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest)

Example 3 with SM3Digest

use of org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest 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);
}
Also used : ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest) BigInteger(java.math.BigInteger) CryptoException(org.bouncycastle.crypto.CryptoException)

Example 4 with SM3Digest

use of org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest in project web3sdk by FISCO-BCOS.

the class SM2Sign method sign.

public static Sign.SignatureData sign(byte[] message, ECKeyPair ecKeyPair) {
    SM3Digest sm3Digest = new SM3Digest();
    BigInteger[] rs = null;
    byte[] r = null;
    byte[] s = null;
    byte[] pub = null;
    byte v = 0;
    byte[] messageHash = sm3Digest.hash(message);
    try {
        byte[] signByte = SM2Algorithm.sign(messageHash, ecKeyPair.getPrivateKey());
        logger.debug("signData:{}", signByte);
        ASN1Sequence as = (ASN1Sequence) ASN1Primitive.fromByteArray(signByte);
        rs = new BigInteger[] { ((ASN1Integer) as.getObjectAt(0)).getValue(), ((ASN1Integer) as.getObjectAt(1)).getValue() };
    } catch (IOException ex) {
        logger.error("SM2 Sign ERROR");
    }
    if (rs != null) {
        r = SM2Algorithm.getEncoded(rs[0]);
        s = SM2Algorithm.getEncoded(rs[1]);
        /*System.out.println("publicKey:" + Hex.toHexString(Numeric.toBytesPadded(ecKeyPair.getPublicKey(),64)));
            System.out.println("publicKeyLen:" + ecKeyPair.getPublicKey().bitLength());
            System.out.println("privateKey:" + Hex.toHexString(Numeric.toBytesPadded(ecKeyPair.getPrivateKey(),32)));
            System.out.println("privateKey:" + ecKeyPair.getPrivateKey().bitLength());*/
        pub = Numeric.toBytesPadded(ecKeyPair.getPublicKey(), 64);
        logger.debug("SM2 SignPublic:{},SM2SignPublicLen:{}", Hex.toHexString(pub), pub.length);
        logger.debug("SM2 SignR:{},SM2SignRLen{}", Hex.toHexString(r), r.length);
        logger.debug("SM2 SignS:{},SM2SignSLen{}", Hex.toHexString(s), s.length);
    // System.out.println("SM2 SignPublic:" + Hex.toHexString(pub));
    }
    return new Sign.SignatureData(v, r, s, pub);
}
Also used : ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest) BigInteger(java.math.BigInteger) IOException(java.io.IOException)

Aggregations

SM3Digest (org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest)4 BigInteger (java.math.BigInteger)2 IOException (java.io.IOException)1 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)1 CryptoException (org.bouncycastle.crypto.CryptoException)1 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)1 ParametersWithID (org.bouncycastle.crypto.params.ParametersWithID)1 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)1 SM2Sign (org.fisco.bcos.web3j.crypto.gm.sm2.SM2Sign)1