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

fix sm2 #257

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
20 changes: 6 additions & 14 deletions src/main/java/com/github/ontio/crypto/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 13 additions & 5 deletions src/test/java/com/github/ontio/account/AccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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()));
}

}