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

The following examples show how to use org.fisco.bcos.web3j.crypto.Sign. 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: SignUtils.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
/**
 * get signature data  by encrypt type
 * @param message message to be signed
 * @param keyPair keyPair for sign
 * @param encryptType 1: guomi, 0: standard
 * @return
 */
public Sign.SignatureData signMessageByType(byte[] message, ECKeyPair keyPair, int encryptType) {
	if (encryptType == EncryptTypes.GUOMI.getValue()) {
		return SM2Sign.sign(message, keyPair);
	} else {
		return ecdsaSign.signMessage(message, keyPair);
	}
}
 
Example #2
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 #3
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 #4
Source File: BcosApp.java    From evidenceSample with Apache License 2.0 5 votes vote down vote up
public Address newEvidence(String keyStoreFileName,String keyStorePassword, String keyPassword,String address,String evidenceId,String evidenceHash) throws Exception {
	Credentials credentials=loadkey(keyStoreFileName,keyStorePassword,keyPassword);
	if(credentials==null){
		return null;
	}
	if (web3j == null)
		return null;
	
	if (address != null) {
		evidenceSignersData = EvidenceSignersData.load(address.toString(), web3j,  credentials, new StaticGasProvider(gasPrice, gasLimited));
	}
	String evidence_id=evidenceId;
	String evidence_hash=evidenceHash;
	//通过hash和key算出一个用户机构签名数据
	Sign.SignatureData data = Sign.getSignInterface().signMessage(evidence_hash.getBytes(), credentials.getEcKeyPair());
	String sign_data=Tools.signatureDataToString(data);
	TransactionReceipt receipt = null;
	try {
		Sign.SignatureData signatureData = Tools.stringToSignatureData(sign_data);
		System.out.println("正在执行!");
		receipt = evidenceSignersData.newEvidence(evidence_hash, evidence_id,evidence_id, BigInteger.valueOf(signatureData.getV()),signatureData.getR(),signatureData.getS()).sendAsync().get();
		List<EvidenceSignersData.NewEvidenceEventEventResponse> newEvidenceList = evidenceSignersData.getNewEvidenceEventEvents(receipt);
		if (newEvidenceList.size() > 0) {
               return new Address(newEvidenceList.get(0).addr);
        } else {
               return null;
        }
	} catch (InterruptedException | ExecutionException e) {
		throw e;
	}
	
}
 
Example #5
Source File: BcosApp.java    From evidenceSample with Apache License 2.0 5 votes vote down vote up
public String verifySignedMessage(String message, String signatureData) throws SignatureException {
    Sign.SignatureData signatureData1 = Tools.stringToSignatureData(signatureData);
    try {
        return "0x" + Keys.getAddress(Sign.signedMessageToKey(message.getBytes(), signatureData1));
    } catch (SignatureException e) {
        throw e;
    }
}
 
Example #6
Source File: Tools.java    From evidenceSample with Apache License 2.0 5 votes vote down vote up
static public Sign.SignatureData stringToSignatureData(String signatureData)
{
    byte[] byte_3 = Numeric.hexStringToByteArray(signatureData);
    byte[] signR = new byte[32];
    System.arraycopy(byte_3, 1, signR, 0, signR.length);
    byte[] signS = new byte[32];
    System.arraycopy(byte_3, 1+signR.length, signS, 0, signS.length);
    return  new Sign.SignatureData(byte_3[0],signR,signS);
}
 
Example #7
Source File: Tools.java    From evidenceSample with Apache License 2.0 5 votes vote down vote up
static public  String  signatureDataToString(Sign.SignatureData signatureData)
{
    byte[] byte_3 = new byte[1+signatureData.getR().length+signatureData.getS().length];
    byte_3[0] = signatureData.getV();
    System.arraycopy(signatureData.getR(), 0, byte_3, 1, signatureData.getR().length);
    System.arraycopy(signatureData.getS(), 0, byte_3, signatureData.getR().length+1, signatureData.getS().length);
    return  Numeric.toHexString(byte_3,0,byte_3.length,false);
}
 
Example #8
Source File: SignDataTest.java    From WeBASE-Front with Apache License 2.0 4 votes vote down vote up
public String getLocalSignedData(String pri) {
    Credentials credentials = GenCredential.create(pri);
    Sign.SignatureData signatureData = Sign.getSignInterface().signMessage(
            ByteUtil.hexStringToBytes(Numeric.toHexString(rawData.getBytes())), credentials.getEcKeyPair());
    return CommonUtils.signatureDataToString(signatureData);
}
 
Example #9
Source File: SM2Sign.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
@Override
public Sign.SignatureData signMessage(byte[] message, ECKeyPair keyPair) {
    return sign2(message, keyPair);
}