From 12a1670d3721fdcbc8f37f65a9c99ae9282b88fa Mon Sep 17 00:00:00 2001 From: Allen Date: Wed, 23 Mar 2022 16:26:16 +0800 Subject: [PATCH] fix sm2 --- .../com/github/ontio/crypto/Signature.java | 20 ++++++------------- .../com/github/ontio/account/AccountTest.java | 18 ++++++++++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/github/ontio/crypto/Signature.java b/src/main/java/com/github/ontio/crypto/Signature.java index 51ec4ad0..29e9cf44 100644 --- a/src/main/java/com/github/ontio/crypto/Signature.java +++ b/src/main/java/com/github/ontio/crypto/Signature.java @@ -27,24 +27,16 @@ public Signature(byte[] data) throws Exception { throw new SDKException(ErrorCode.ParamError); } - if (data.length == 65) { - SignatureScheme[] schemes = SignatureScheme.values(); - if (data[0] > schemes.length) { - throw new SDKException(ErrorCode.UnsupportedSignatureScheme); - } - this.scheme = schemes[data[0]]; - } else if (data.length == 64) { // use default scheme - byte[] temp = new byte[65]; - temp[0] = 1; - System.arraycopy(data, 0, temp, 1, 64); - data = temp; + if (data.length == 64) { + this.value = data; this.scheme = SignatureScheme.SHA256WITHECDSA; - } else { - throw new Exception(ErrorCode.InvalidSignatureDataLen); + return; } + + this.scheme = SignatureScheme.values()[data[0]]; if (scheme == SignatureScheme.SM3WITHSM2) { int i = 0; - while (i < data.length && data[i] != 0) { + while (i < data.length && data[i] != 0){ i++; } if (i >= data.length) { diff --git a/src/test/java/com/github/ontio/account/AccountTest.java b/src/test/java/com/github/ontio/account/AccountTest.java index b74f3cd5..25b2db20 100644 --- a/src/test/java/com/github/ontio/account/AccountTest.java +++ b/src/test/java/com/github/ontio/account/AccountTest.java @@ -11,9 +11,17 @@ public class AccountTest { @Test public void generateSignature() throws Exception { + Account account = new Account(SignatureScheme.SM3WITHSM2); + byte[] signature = account.generateSignature("hello".getBytes(), SignatureScheme.SM3WITHSM2, null); + boolean b = account.verifySignature("hello".getBytes(), signature); + assertTrue(b); + } + + @Test + public void testSHA256WITHECDSA() throws Exception { Account account = new Account(SignatureScheme.SHA256WITHECDSA); - byte[] signature = account.generateSignature("hello".getBytes(),SignatureScheme.SHA256WITHECDSA,null); - boolean b = account.verifySignature("hello".getBytes(),signature); + byte[] signature = account.generateSignature("hello".getBytes(), SignatureScheme.SHA256WITHECDSA, null); + boolean b = account.verifySignature("hello".getBytes(), signature); assertTrue(b); } @@ -42,9 +50,9 @@ public void compareTo() throws Exception { @Test public void exportCtrEncryptedPrikey1() throws Exception { Account account = new Account(SignatureScheme.SHA256WITHECDSA); - String encruPri = account.exportCtrEncryptedPrikey("111111",16384); - String privateKey = Account.getCtrDecodedPrivateKey(encruPri,"111111",account.getAddressU160().toBase58(),16384,SignatureScheme.SHA256WITHECDSA); - assertEquals(privateKey,Helper.toHexString(account.serializePrivateKey())); + String encruPri = account.exportCtrEncryptedPrikey("111111", 16384); + String privateKey = Account.getCtrDecodedPrivateKey(encruPri, "111111", account.getAddressU160().toBase58(), 16384, SignatureScheme.SHA256WITHECDSA); + assertEquals(privateKey, Helper.toHexString(account.serializePrivateKey())); } } \ No newline at end of file