From be1219af2af8bc3d2f13e34bf585198aca65e7de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E6=96=87=E5=B9=B3?= Date: Thu, 10 Jan 2019 02:32:35 +0800 Subject: [PATCH] Providing random signatures --- .../plactal/eoscommander/crypto/ec/EcDsa.java | 21 ++++- .../eoscommander/crypto/ec/EosEcUtil.java | 79 ++++++++++++++++--- .../eoscommander/crypto/ec/EosPrivateKey.java | 5 +- .../eoscommander/crypto/ec/EosEcUtilTest.java | 24 ++++++ 4 files changed, 111 insertions(+), 18 deletions(-) create mode 100644 app/src/test/java/io/plactal/eoscommander/crypto/ec/EosEcUtilTest.java diff --git a/app/src/main/java/io/plactal/eoscommander/crypto/ec/EcDsa.java b/app/src/main/java/io/plactal/eoscommander/crypto/ec/EcDsa.java index 6687e36..c7556ae 100644 --- a/app/src/main/java/io/plactal/eoscommander/crypto/ec/EcDsa.java +++ b/app/src/main/java/io/plactal/eoscommander/crypto/ec/EcDsa.java @@ -25,6 +25,7 @@ package io.plactal.eoscommander.crypto.ec; import java.math.BigInteger; +import java.security.SecureRandom; import java.util.Arrays; import io.plactal.eoscommander.data.remote.model.types.EosByteWriter; @@ -36,7 +37,11 @@ */ public class EcDsa { + private static final SecureRandom mSecRandom; + static { + mSecRandom = new SecureRandom(); + } private static class SigChecker { BigInteger e; BigInteger privKey; @@ -73,7 +78,7 @@ public boolean isRSEachLength(int length) { } - private static BigInteger deterministicGenerateK(CurveParam curveParam, byte[] hash, BigInteger d, SigChecker checker, int nonce ){ + private static BigInteger deterministicGenerateK(CurveParam curveParam, byte[] hash, BigInteger d, SigChecker checker, int nonce,boolean random ){ if ( nonce > 0 ){ hash = Sha256.from(hash, BigInteger.valueOf(nonce).toByteArray()).getBytes(); } @@ -86,7 +91,11 @@ private static BigInteger deterministicGenerateK(CurveParam curveParam, byte[] h // Step c byte [] k = new byte[32]; - Arrays.fill(k, (byte)0x00); + if (random){ + mSecRandom.nextBytes(k); + }else { + Arrays.fill(k, (byte)0x00); + } // Step d EosByteWriter bwD = new EosByteWriter(32 + 1 + 32 + 32); @@ -132,7 +141,11 @@ private static BigInteger deterministicGenerateK(CurveParam curveParam, byte[] h return t; } - public static EcSignature sign(Sha256 hash, EosPrivateKey key ) { + public static EcSignature sign(Sha256 sha256,EosPrivateKey key){ + return sign(sha256, key,false); + } + + public static EcSignature sign(Sha256 hash, EosPrivateKey key,boolean random ) { BigInteger privAsBI = key.getAsBigInteger(); SigChecker checker = new SigChecker(hash.getBytes(), privAsBI); @@ -140,7 +153,7 @@ public static EcSignature sign(Sha256 hash, EosPrivateKey key ) { int nonce = 0; while ( true ) { - deterministicGenerateK(curveParam, hash.getBytes(), privAsBI, checker, nonce++); + deterministicGenerateK(curveParam, hash.getBytes(), privAsBI, checker, nonce++,random); if (checker.s.compareTo( curveParam.halfCurveOrder() ) > 0) {// Secp256k1Param.HALF_CURVE_ORDER) > 0) { checker.s = curveParam.n().subtract(checker.s);// Secp256k1Param.n.subtract(checker.s); diff --git a/app/src/main/java/io/plactal/eoscommander/crypto/ec/EosEcUtil.java b/app/src/main/java/io/plactal/eoscommander/crypto/ec/EosEcUtil.java index ff7b4f7..ba8ee69 100644 --- a/app/src/main/java/io/plactal/eoscommander/crypto/ec/EosEcUtil.java +++ b/app/src/main/java/io/plactal/eoscommander/crypto/ec/EosEcUtil.java @@ -30,6 +30,7 @@ import io.plactal.eoscommander.crypto.digest.Sha256; import io.plactal.eoscommander.crypto.util.Base58; import io.plactal.eoscommander.crypto.util.BitUtils; +import io.plactal.eoscommander.data.remote.model.types.EosByteWriter; import io.plactal.eoscommander.util.RefValue; import io.plactal.eoscommander.util.StringUtils; @@ -170,6 +171,45 @@ public static byte[] getBytesIfMatchedSha256(String base58Data,RefValue ch // return EOS_PREFIX + ( isR1 ? PREFIX_R1 : "") + Base58.encode( result ); // } +// public static String encodeEosCrypto(String prefix, CurveParam curveParam, byte[] data ) { +// String typePart = ""; +// if ( curveParam != null ) { +// if ( curveParam.isType( CurveParam.SECP256_K1)) { +// typePart = PREFIX_K1; +// } +// else +// if ( curveParam.isType( CurveParam.SECP256_R1)){ +// typePart = PREFIX_R1; +// } +// } +// +// byte[] toHashData = new byte[ data.length + typePart.length() ]; +// System.arraycopy( data, 0, toHashData, 0, data.length); +// if ( typePart.length() > 0 ) { +// System.arraycopy( typePart.getBytes(), 0, toHashData, data.length, typePart.length()); +// } +// +// byte[] dataToEncodeBase58 = new byte[ data.length + 4 ]; +// +// Ripemd160 ripemd160 = Ripemd160.from( toHashData); +// byte[] checksumBytes = ripemd160.bytes(); +// +// System.arraycopy( data, 0, dataToEncodeBase58, 0, data.length); // copy source data +// System.arraycopy( checksumBytes, 0, dataToEncodeBase58, data.length, 4); // copy checksum data +// +// +// String result; +// if ( StringUtils.isEmpty( typePart)) { +// result = prefix; +// } +// else { +// result = prefix + EOS_CRYPTO_STR_SPLITTER + typePart + EOS_CRYPTO_STR_SPLITTER; +// } +// +// return result + Base58.encode( dataToEncodeBase58 ); +// } + + public static String encodeEosCrypto(String prefix, CurveParam curveParam, byte[] data ) { String typePart = ""; if ( curveParam != null ) { @@ -182,20 +222,34 @@ public static String encodeEosCrypto(String prefix, CurveParam curveParam, byte[ } } - byte[] toHashData = new byte[ data.length + typePart.length() ]; - System.arraycopy( data, 0, toHashData, 0, data.length); - if ( typePart.length() > 0 ) { - System.arraycopy( typePart.getBytes(), 0, toHashData, data.length, typePart.length()); +// byte[] toHashData = new byte[ data.length + typePart.length() ]; +// System.arraycopy( data, 0, toHashData, 0, data.length); +// if ( typePart.length() > 0 ) { +// System.arraycopy( typePart.getBytes(), 0, toHashData, data.length, typePart.length()); +// } +// +// byte[] dataToEncodeBase58 = new byte[ data.length + 4 ]; +// +// Ripemd160 ripemd160 = Ripemd160.from( toHashData); +// byte[] checksumBytes = ripemd160.bytes(); +// +// System.arraycopy( data, 0, dataToEncodeBase58, 0, data.length); // copy source data +// System.arraycopy( checksumBytes, 0, dataToEncodeBase58, data.length, 4); // copy checksum data + + EosByteWriter first = new EosByteWriter(255); + first.putBytes(data); + if (typePart.length() > 0) + { + first.putBytes(typePart.getBytes()); } + Ripemd160 ripemd160 = Ripemd160.from(first.toBytes()); - byte[] dataToEncodeBase58 = new byte[ data.length + 4 ]; - - Ripemd160 ripemd160 = Ripemd160.from( toHashData); - byte[] checksumBytes = ripemd160.bytes(); - - System.arraycopy( data, 0, dataToEncodeBase58, 0, data.length); // copy source data - System.arraycopy( checksumBytes, 0, dataToEncodeBase58, data.length, 4); // copy checksum data + byte[] rmd = new byte[4]; + System.arraycopy(ripemd160.bytes(), 0, rmd, 0, rmd.length); + EosByteWriter last = new EosByteWriter(255); + last.putBytes(data); + last.putBytes(rmd); String result; if ( StringUtils.isEmpty( typePart)) { @@ -205,12 +259,11 @@ public static String encodeEosCrypto(String prefix, CurveParam curveParam, byte[ result = prefix + EOS_CRYPTO_STR_SPLITTER + typePart + EOS_CRYPTO_STR_SPLITTER; } - return result + Base58.encode( dataToEncodeBase58 ); + return result + Base58.encode( last.toBytes() ); } - private static final String EOS_CRYPTO_STR_SPLITTER = "_"; public static String[] safeSplitEosCryptoString( String cryptoStr ) { if ( StringUtils.isEmpty( cryptoStr)) { diff --git a/app/src/main/java/io/plactal/eoscommander/crypto/ec/EosPrivateKey.java b/app/src/main/java/io/plactal/eoscommander/crypto/ec/EosPrivateKey.java index 088640e..8a21706 100644 --- a/app/src/main/java/io/plactal/eoscommander/crypto/ec/EosPrivateKey.java +++ b/app/src/main/java/io/plactal/eoscommander/crypto/ec/EosPrivateKey.java @@ -141,8 +141,11 @@ public CurveParam getCurveParam(){ return mCurveParam; } + public EcSignature sign( Sha256 digest ,boolean random) { + return EcDsa.sign( digest, this,random); + } public EcSignature sign( Sha256 digest ) { - return EcDsa.sign( digest, this); + return sign(digest,false); } @Override diff --git a/app/src/test/java/io/plactal/eoscommander/crypto/ec/EosEcUtilTest.java b/app/src/test/java/io/plactal/eoscommander/crypto/ec/EosEcUtilTest.java new file mode 100644 index 0000000..09d120e --- /dev/null +++ b/app/src/test/java/io/plactal/eoscommander/crypto/ec/EosEcUtilTest.java @@ -0,0 +1,24 @@ +package io.plactal.eoscommander.crypto.ec; + +import org.junit.Test; + +import java.util.Locale; + +import io.plactal.eoscommander.crypto.digest.Sha256; + +import static org.junit.Assert.*; + +public class EosEcUtilTest { + + @Test + public void encodeEosCrypto() { + EosPrivateKey eosPrivateKey = new EosPrivateKey("5Jg3KtArcxdsk2opXpyBNqKeZ7ah9SFLPg2Xx8vHFGCnfRGffkD"); + Sha256 signData = Sha256.from("Starteos".getBytes()); + String signStr = eosPrivateKey.sign(signData,true).toString(); + System.out.println(String.format(Locale.CHINA,"sign string -> %s",signStr)); +// SIG_K1_KdxetgEKPZqXYgSFVTuDHFT1Z4fFiR8KfBRCSbyRmuxwQveeZQF2ZerV1KoEq8Shjh1PwTuS2q14zae4zgxFb2jj5kHRqA +// SIG_K1_KYomYJ8trBBTfQyxxVLWXvnz6Nm6RSfp3ufVBoTGsEqSRqtKvGyfo498v9bDvLWkvpQ1zWhH9AcebyurWXYsNYKQG3AUKF + EosPublicKey eosPublicKey = EcDsa.recoverPubKey(signData.getBytes(),new EcSignature(signStr)); + System.out.println(String.format(Locale.CHINA,"public key -> %s",eosPublicKey.toString())); + } +} \ No newline at end of file