mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
fix:兼容go版本的sm2
This commit is contained in:
1 parent
74358fbc5e
commit
f69ab1703e
5 files changed
+103
-55
No files matched your search
@@ -177,5 +177,43 @@ public class SM2Base {
|
||||
return SM2KeyPair.generateKeyPair();
|
||||
}
|
||||
|
||||
/**
|
||||
* SM2字节数组->16进制字节数组 辅助函数
|
||||
* @param bytes byte[]
|
||||
* @return byte[]
|
||||
* @throws
|
||||
* @author zhengqi wu
|
||||
* @date 2021/4/12
|
||||
**/
|
||||
public static byte[] byteArraytoHexByteArray(byte[] bytes){
|
||||
// 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 16进制字节数组->SM2字节数组 辅助函数
|
||||
* @param bytes byte[]
|
||||
* @return byte[]
|
||||
* @throws
|
||||
* @author zhengqi wu
|
||||
* @date 2021/4/12
|
||||
**/
|
||||
public static byte[] hexByteArrayToByteArray(byte[] bytes){
|
||||
String hex = new String(bytes);
|
||||
if (hex.length() < 1) {
|
||||
return null;
|
||||
} else {
|
||||
byte[] result = new byte[hex.length() / 2];
|
||||
int j = 0;
|
||||
for(int i = 0; i < hex.length(); i+=2) {
|
||||
result[j++] = (byte)Integer.parseInt(hex.substring(i,i+2), 16);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.bouncycastle.math.ec.ECCurve;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
@@ -46,18 +47,32 @@ public class SM2PublicKey implements PublicKeyInterface {
|
||||
|
||||
|
||||
@Override
|
||||
// TODO 实现不同
|
||||
public byte[] getBytes() {
|
||||
if(publicKey != null)
|
||||
return publicKey.getQ().getEncoded(false); //不压缩
|
||||
if(publicKey != null){
|
||||
byte[] bytes = publicKey.getQ().getEncoded(false); //不压缩
|
||||
byte[] basic = SM2Base.byteArraytoHexByteArray(bytes);
|
||||
return basic;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
// TODO 实现不同
|
||||
public boolean setBytes(byte[] bytes) {
|
||||
if(bytes.length != 65)
|
||||
return false;
|
||||
switch (bytes.length){
|
||||
case 65:
|
||||
createPub(bytes);
|
||||
break;
|
||||
case 130:
|
||||
bytes = SM2Base.hexByteArrayToByteArray(bytes); // 16进制表示法转为十进制表示法
|
||||
createPub(bytes);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void createPub(byte[] bytes){
|
||||
ByteBuffer encoded = ByteBuffer.wrap(bytes);
|
||||
byte[] x = new byte[32];
|
||||
byte[] y = new byte[32];
|
||||
@@ -65,7 +80,6 @@ public class SM2PublicKey implements PublicKeyInterface {
|
||||
encoded.get(x,0,32);
|
||||
encoded.get(y,0,32);
|
||||
publicKey = createPublicKey(x,y);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user