Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing random signatures #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions app/src/main/java/io/plactal/eoscommander/crypto/ec/EcDsa.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,7 +37,11 @@
*/

public class EcDsa {
private static final SecureRandom mSecRandom;

static {
mSecRandom = new SecureRandom();
}
private static class SigChecker {
BigInteger e;
BigInteger privKey;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand Down Expand Up @@ -132,15 +141,19 @@ 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);

CurveParam curveParam = key.getCurveParam();

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);
Expand Down
79 changes: 66 additions & 13 deletions app/src/main/java/io/plactal/eoscommander/crypto/ec/EosEcUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -170,6 +171,45 @@ public static byte[] getBytesIfMatchedSha256(String base58Data,RefValue<Long> 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 ) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}