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);
}
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;
}
}
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);
}
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);
}
Aggregations