Java Code Examples for org.fisco.bcos.web3j.utils.Numeric#toBytesPadded()

The following examples show how to use org.fisco.bcos.web3j.utils.Numeric#toBytesPadded() . 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: Keys.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static byte[] serialize(ECKeyPair ecKeyPair) {
    byte[] privateKey = Numeric.toBytesPadded(ecKeyPair.getPrivateKey(), PRIVATE_KEY_SIZE);
    byte[] publicKey = Numeric.toBytesPadded(ecKeyPair.getPublicKey(), PUBLIC_KEY_SIZE);

    byte[] result = Arrays.copyOf(privateKey, PRIVATE_KEY_SIZE + PUBLIC_KEY_SIZE);
    System.arraycopy(publicKey, 0, result, PRIVATE_KEY_SIZE, PUBLIC_KEY_SIZE);
    return result;
}
 
Example 2
Source File: TransactionDecoder.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static RawTransaction decode(String hexTransaction) {
    byte[] transaction = Numeric.hexStringToByteArray(hexTransaction);
    RlpList rlpList = RlpDecoder.decode(transaction);
    RlpList values = (RlpList) rlpList.getValues().get(0);
    BigInteger randomid = ((RlpString) values.getValues().get(0)).asPositiveBigInteger();
    BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger();
    BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger();
    BigInteger blockLimit = ((RlpString) values.getValues().get(3)).asPositiveBigInteger();
    String to = ((RlpString) values.getValues().get(4)).asString();
    BigInteger value = ((RlpString) values.getValues().get(5)).asPositiveBigInteger();
    String data = ((RlpString) values.getValues().get(6)).asString();
    if (values.getValues().size() > 7) {
        byte v = ((RlpString) values.getValues().get(7)).getBytes()[0];
        byte[] r =
                Numeric.toBytesPadded(
                        Numeric.toBigInt(((RlpString) values.getValues().get(8)).getBytes()),
                        32);
        byte[] s =
                Numeric.toBytesPadded(
                        Numeric.toBigInt(((RlpString) values.getValues().get(9)).getBytes()),
                        32);
        Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s);
        return new SignedRawTransaction(
                randomid, gasPrice, gasLimit, blockLimit, to, value, data, signatureData);
    } else {
        return RawTransaction.createTransaction(
                randomid, gasPrice, gasLimit, blockLimit, to, value, data);
    }
}
 
Example 3
Source File: SM2Sign.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: SM2Sign.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
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);
        // System.out.println("signData:" + Hex.toHexString(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);
}
 
Example 5
Source File: Wallet.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static WalletFile create(String password, ECKeyPair ecKeyPair, int n, int p)
        throws CipherException {

    byte[] salt = generateRandomBytes(32);

    byte[] derivedKey =
            generateDerivedScryptKey(password.getBytes(UTF_8), salt, n, R, p, DKLEN);

    byte[] encryptKey = Arrays.copyOfRange(derivedKey, 0, 16);
    byte[] iv = generateRandomBytes(16);

    byte[] privateKeyBytes =
            Numeric.toBytesPadded(ecKeyPair.getPrivateKey(), Keys.PRIVATE_KEY_SIZE);

    byte[] cipherText =
            performCipherOperation(Cipher.ENCRYPT_MODE, iv, encryptKey, privateKeyBytes);

    byte[] mac = generateMac(derivedKey, cipherText);

    return createWalletFile(ecKeyPair, cipherText, iv, salt, mac, n, p);
}
 
Example 6
Source File: ExtendedTransactionDecoder.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static ExtendedRawTransaction decode(String hexTransaction) {
    byte[] transaction = Numeric.hexStringToByteArray(hexTransaction);
    RlpList rlpList = RlpDecoder.decode(transaction);
    RlpList values = (RlpList) rlpList.getValues().get(0);
    BigInteger randomid = ((RlpString) values.getValues().get(0)).asPositiveBigInteger();
    BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger();
    BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger();
    BigInteger blockLimit = ((RlpString) values.getValues().get(3)).asPositiveBigInteger();
    String to = ((RlpString) values.getValues().get(4)).asString();
    BigInteger value = ((RlpString) values.getValues().get(5)).asPositiveBigInteger();
    String data = ((RlpString) values.getValues().get(6)).asString();

    // add extra data
    BigInteger chainId = ((RlpString) values.getValues().get(7)).asPositiveBigInteger();
    BigInteger groupId = ((RlpString) values.getValues().get(8)).asPositiveBigInteger();
    String extraData = ((RlpString) values.getValues().get(9)).asString();
    if (values.getValues().size() > 9) {
        byte v = ((RlpString) values.getValues().get(10)).getBytes()[0];
        byte[] r =
                Numeric.toBytesPadded(
                        Numeric.toBigInt(((RlpString) values.getValues().get(11)).getBytes()),
                        32);
        byte[] s =
                Numeric.toBytesPadded(
                        Numeric.toBigInt(((RlpString) values.getValues().get(12)).getBytes()),
                        32);
        Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s);
        return new SignedExtendedRawTransaction(
                randomid,
                gasPrice,
                gasLimit,
                blockLimit,
                to,
                value,
                data,
                chainId,
                groupId,
                extraData,
                signatureData);
    } else {
        return ExtendedRawTransaction.createTransaction(
                randomid,
                gasPrice,
                gasLimit,
                blockLimit,
                to,
                value,
                data,
                chainId,
                groupId,
                extraData);
    }
}