fix:兼容go版本的sm2

This commit is contained in:
ChessNineeee committed 2021-04-12 17:33:15 +08:00
1 parent 74358fbc5e
commit f69ab1703e
5 files changed
+103 -55

No files matched your search

@@ -12,6 +12,7 @@ import org.bouncycastle.crypto.signers.SM2Signer;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arrays;
/*
* @Author: hongyu guo
@@ -55,13 +56,7 @@ public class SM2PrivateKey implements PrivateKeyInterface {
@Override
public byte[] getBytes() {
byte[] bytes = privateKey.getD().toByteArray();
// 16进制表示法
StringBuilder sb = new StringBuilder();
for(int i=0;i < bytes.length;i++) {
sb.append(String.format("%02x", bytes[i]));
}
String hexStr = sb.toString();
return hexStr.getBytes();
return SM2Base.byteArraytoHexByteArray(bytes);
}
@Override
@@ -84,11 +79,12 @@ public class SM2PrivateKey implements PrivateKeyInterface {
return false;
switch (d.length){
case 32:
case 33:
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
break;
case 64:
String priStr = new String(d);
privateKey = new ECPrivateKeyParameters(new BigInteger(priStr, 16), SM2Base.DOMAIN_PARAMS);
d = SM2Base.hexByteArrayToByteArray(d);
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
break;
}
return true;