org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest Java Examples

The following examples show how to use org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest. 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: SM3PasswordEncoder.java    From WeBASE-Node-Manager with Apache License 2.0 5 votes vote down vote up
@Override
public String encode(CharSequence rawPassword) {
    SM3Digest sm3Digest = new SM3Digest();
    byte[] pwdInput = rawPassword.toString().getBytes();
    byte[] hashed = sm3Digest.hash(pwdInput);
    return Numeric.toHexString(hashed);
}
 
Example #2
Source File: GenGmAccount.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
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: EncryptTest.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetSMEncryptTest() {
    EncryptType.setEncryptType(EncryptType.SM2_TYPE);
    HashInterface hashInterface = Hash.getHashInterface();
    SignInterface signInterface = Sign.getSignInterface();
    assertTrue(hashInterface instanceof SM3Digest);
    assertTrue(signInterface instanceof SM2Sign);
}
 
Example #6
Source File: BeanConfig.java    From WeBASE-Sign with Apache License 2.0 4 votes vote down vote up
@Bean
public SM3Digest getSM3Digest() {
	return new SM3Digest();
}