fix:兼容go版本的sm2

This commit is contained in:
ChessNineeee committed 2021-04-12 16:13:48 +08:00
1 parent bcd906e1a2
commit 74358fbc5e
7 files changed
+99 -56

No files matched your search

@@ -54,7 +54,14 @@ public class SM2PrivateKey implements PrivateKeyInterface {
@Override
public byte[] getBytes() {
return privateKey.getD().toByteArray();
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();
}
@Override
@@ -71,11 +78,19 @@ public class SM2PrivateKey implements PrivateKeyInterface {
* @params d {byte[]}
* @return boolean
*/
// TODO 实现不同
public boolean setBytes(byte[] d){
// if (d.length != 64)
// return false;
String priStr = new String(d);
privateKey = new ECPrivateKeyParameters(new BigInteger(priStr, 16), SM2Base.DOMAIN_PARAMS);
if (d.length != 64 && d.length != 32 && d.length != 33)
return false;
switch (d.length){
case 32:
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);
break;
}
return true;
}