org.fisco.bcos.web3j.crypto.Sign.SignatureData Java Examples

The following examples show how to use org.fisco.bcos.web3j.crypto.Sign.SignatureData. 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: CommonUtils.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
 * signatureDataToString. 19/12/24 support guomi: add byte[] pub in signatureData
 * 
 * @param signatureData signatureData
 */
public static String signatureDataToString(SignatureData signatureData) {
    byte[] byteArr;
    if (EncryptType.encryptType == 1) {
        byteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length
                + PUBLIC_KEY_LENGTH_64];
        byteArr[0] = signatureData.getV();
        System.arraycopy(signatureData.getR(), 0, byteArr, 1, signatureData.getR().length);
        System.arraycopy(signatureData.getS(), 0, byteArr, signatureData.getR().length + 1,
                signatureData.getS().length);
        System.arraycopy(signatureData.getPub(), 0, byteArr,
                signatureData.getS().length + signatureData.getR().length + 1,
                signatureData.getPub().length);
    } else {
        byteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length];
        byteArr[0] = signatureData.getV();
        System.arraycopy(signatureData.getR(), 0, byteArr, 1, signatureData.getR().length);
        System.arraycopy(signatureData.getS(), 0, byteArr, signatureData.getR().length + 1,
                signatureData.getS().length);
    }
    return Numeric.toHexString(byteArr, 0, byteArr.length, false);
}
 
Example #2
Source File: CommonUtils.java    From WeBASE-Transaction with Apache License 2.0 6 votes vote down vote up
/**
 * signatureDataToString.
 * 19/12/24 support guomi: add byte[] pub in signatureData
 * @param signatureData signatureData
 */
public static String signatureDataToString(SignatureData signatureData) {
    byte[] byteArr;
    if(EncryptType.encryptType == 1) {
        byteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length + publicKeyLength_64];
        byteArr[0] = signatureData.getV();
        System.arraycopy(signatureData.getR(), 0, byteArr, 1, signatureData.getR().length);
        System.arraycopy(signatureData.getS(), 0, byteArr, signatureData.getR().length + 1,
                signatureData.getS().length);
        System.arraycopy(signatureData.getPub(), 0, byteArr,
                signatureData.getS().length + signatureData.getR().length + 1,
                signatureData.getPub().length);
    } else {
        byteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length];
        byteArr[0] = signatureData.getV();
        System.arraycopy(signatureData.getR(), 0, byteArr, 1, signatureData.getR().length);
        System.arraycopy(signatureData.getS(), 0, byteArr, signatureData.getR().length + 1,
                signatureData.getS().length);
    }
    return Numeric.toHexString(byteArr, 0, byteArr.length, false);
}
 
Example #3
Source File: CommonUtils.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
/**
 * stringToSignatureData. 19/12/24 support guomi: add byte[] pub in signatureData
 * 
 * @param signatureData signatureData
 * @return
 */
public static SignatureData stringToSignatureData(String signatureData) {
    byte[] byteArr = Numeric.hexStringToByteArray(signatureData);
    byte[] signR = new byte[32];
    System.arraycopy(byteArr, 1, signR, 0, signR.length);
    byte[] signS = new byte[32];
    System.arraycopy(byteArr, 1 + signR.length, signS, 0, signS.length);
    if (EncryptType.encryptType == 1) {
        byte[] pub = new byte[64];
        System.arraycopy(byteArr, 1 + signR.length + signS.length, pub, 0, pub.length);
        return new SignatureData(byteArr[0], signR, signS, pub);
    } else {
        return new SignatureData(byteArr[0], signR, signS);
    }
}
 
Example #4
Source File: CommonUtils.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
/**
 * stringToSignatureData.
 * 19/12/24 support guomi: add byte[] pub in signatureData
 * @param signatureData signatureData
 * @return
 */
public static SignatureData stringToSignatureData(String signatureData) {
    byte[] byteArr = Numeric.hexStringToByteArray(signatureData);
    byte[] signR = new byte[32];
    System.arraycopy(byteArr, 1, signR, 0, signR.length);
    byte[] signS = new byte[32];
    System.arraycopy(byteArr, 1 + signR.length, signS, 0, signS.length);
    if (EncryptType.encryptType == 1) {
        byte[] pub = new byte[64];
        System.arraycopy(byteArr, 1 + signR.length + signS.length, pub, 0, pub.length);
        return new SignatureData(byteArr[0], signR, signS, pub);
    } else {
        return new SignatureData(byteArr[0], signR, signS);
    }
}
 
Example #5
Source File: SignService.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
/**
 * add sign.
 * @param req parameter
 */
public String sign(ReqEncodeInfoVo req) throws BaseException {
    String signUserId = req.getSignUserId();
    log.info("start sign. signUserId:{}", signUserId);
    Instant startTimeDB = Instant.now();
    // check exist
    UserInfoPo userRow = userService.findBySignUserId(signUserId);
   log.debug("end query db time: {}", Duration.between(startTimeDB, Instant.now()).toMillis());
    // check user name not exist.
    if (Objects.isNull(userRow)) {
        log.warn("fail sign, user not exists. signUserId:{}", signUserId);
        throw new BaseException(CodeMessageEnums.USER_NOT_EXISTS);
    }
    int encryptType = userRow.getEncryptType();
    // signature
    Credentials credentials = keyPairUtils.create(userRow.getPrivateKey(), encryptType);
    byte[] encodedData;
    try {
        encodedData = ByteUtil.hexStringToBytes(req.getEncodedDataStr());
    } catch (DecoderException e) {
        log.error("hexStringToBytes error: ", e);
        throw new BaseException(CodeMessageEnums.PARAM_ENCODED_DATA_INVALID);

    }
    Instant startTime = Instant.now();
    log.info("start sign. startTime:{}", startTime.toEpochMilli());
    // sign message by type
    SignatureData signatureData = signUtils.signMessageByType(
            encodedData, credentials.getEcKeyPair(), encryptType);
    log.info("end sign duration:{}", Duration.between(startTime, Instant.now()).toMillis());
    String signDataStr = CommonUtils.signatureDataToStringByType(signatureData, encryptType);
    log.info("end sign. signUserId:{}", signUserId);
    return signDataStr;
}
 
Example #6
Source File: CommonUtils.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
/**
 * signatureDataToString by type
 * @param signatureData
 * @param encryptType
 * @return
 */
public static String signatureDataToStringByType(SignatureData signatureData, int encryptType) {
    byte[] byteArr;
    if(encryptType == 1) {
        byteArr = sigData2ByteArrGuomi(signatureData);
    } else {
        byteArr = sigData2ByteArrECDSA(signatureData);
    }
    return Numeric.toHexString(byteArr, 0, byteArr.length, false);
}
 
Example #7
Source File: CommonUtils.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
private static byte[] sigData2ByteArrGuomi(SignatureData signatureData) {
    byte[] targetByteArr;
    targetByteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length + publicKeyLength_64];
    targetByteArr[0] = signatureData.getV();
    System.arraycopy(signatureData.getR(), 0, targetByteArr, 1, signatureData.getR().length);
    System.arraycopy(signatureData.getS(), 0, targetByteArr, signatureData.getR().length + 1,
            signatureData.getS().length);
    System.arraycopy(signatureData.getPub(), 0, targetByteArr,
            signatureData.getS().length + signatureData.getR().length + 1,
            signatureData.getPub().length);
    return targetByteArr;
}
 
Example #8
Source File: CommonUtils.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
private static byte[] sigData2ByteArrECDSA(SignatureData signatureData) {
    byte[] targetByteArr;
    targetByteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length];
    targetByteArr[0] = signatureData.getV();
    System.arraycopy(signatureData.getR(), 0, targetByteArr, 1, signatureData.getR().length);
    System.arraycopy(signatureData.getS(), 0, targetByteArr, signatureData.getR().length + 1,
            signatureData.getS().length);
    return targetByteArr;
}
 
Example #9
Source File: ECRecoverTest.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecoverAddressFromSignature() {
    // CHECKSTYLE:OFF
    String signature =
            "0x2c6401216c9031b9a6fb8cbfccab4fcec6c951cdf40e2320108d1856eb532250576865fbcd452bcdc4c57321b619ed7a9cfd38bd973c3e1e0243ac2777fe9d5b1b";
    // CHECKSTYLE:ON
    String address = "0x31b26e43651e9371c88af3d36c14cfd938baf4fd";
    String message = "v0G9u7huK4mJb2K1";

    String prefix = PERSONAL_MESSAGE_PREFIX + message.length();
    byte[] msgHash = Hash.sha3((prefix + message).getBytes());

    byte[] signatureBytes = Numeric.hexStringToByteArray(signature);
    byte v = signatureBytes[64];
    if (v < 27) {
        v += 27;
    }

    SignatureData sd =
            new SignatureData(
                    v,
                    (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32),
                    (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64));

    String addressRecovered = null;
    boolean match = false;

    // Iterate for each possible key to recover
    for (int i = 0; i < 4; i++) {
        BigInteger publicKey =
                Sign.recoverFromSignature(
                        (byte) i,
                        new ECDSASignature(
                                new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())),
                        msgHash);

        if (publicKey != null) {
            addressRecovered = "0x" + Keys.getAddress(publicKey);

            if (addressRecovered.equals(address)) {
                match = true;
                break;
            }
        }
    }

    assertThat(addressRecovered, is(address));
    assertTrue(match);
}