mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
修复bytehelper & VlInt中的已知bug
This commit is contained in:
1 parent
227d10a391
commit
2ad446ca2c
29 files changed
+315
-80
No files matched your search
@@ -16,7 +16,7 @@ import static org.junit.Assert.*;
|
||||
public class BlockTest {
|
||||
|
||||
@Test
|
||||
public void TestCreateBlockByTypeLengthBuffer() {
|
||||
public void TestCreateBlockByTypeLengthBuffer() throws VlIntException {
|
||||
byte[] buf = new byte[10000];
|
||||
Block block = Block.createBlockByTypeLengthBuffer(new VlInt(241), new VlInt(8000),ByteHelper.getLenBytes(buf, new VlInt(0),8000),true);
|
||||
//测试type与size
|
||||
@@ -42,7 +42,7 @@ public class BlockTest {
|
||||
|
||||
//error:block.Size().size()错误。
|
||||
@Test
|
||||
public void TestCreateBlockByBuffer() {
|
||||
public void TestCreateBlockByBuffer() throws VlIntException {
|
||||
byte[] buf = new byte[10000];
|
||||
buf[0] = (byte)212;
|
||||
buf[1] = (byte)241;
|
||||
@@ -59,7 +59,7 @@ public class BlockTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestBuildByBlock(){
|
||||
public void TestBuildByBlock() throws VlIntException {
|
||||
//Block构造方法测试
|
||||
byte[] buf = new byte[10000];
|
||||
Block block1 = Block.createBlockByTypeLengthBuffer(new VlInt(241), new VlInt(8000),ByteHelper.getLenBytes(buf, new VlInt(0),8000),true);
|
||||
@@ -85,7 +85,7 @@ public class BlockTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestBuildByTypeLengthBuffer(){
|
||||
public void TestBuildByTypeLengthBuffer() throws VlIntException {
|
||||
//Block构造方法测试
|
||||
byte[] buf = new byte[10000];
|
||||
Block block = new Block(new VlInt(241),new VlInt(8000),ByteHelper.getLenBytes(buf,new VlInt(0),8000),true);
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package encoding;
|
||||
|
||||
|
||||
import minsecurity.crypto.TestSM2;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
@@ -17,6 +19,8 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
|
||||
public class VlIntTest {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(VlIntTest.class);
|
||||
|
||||
|
||||
// @Test
|
||||
// public void size() {
|
||||
@@ -41,7 +45,7 @@ public class VlIntTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getVlIntValue2Int() {
|
||||
public void getVlIntValue2Int() throws VlIntException {
|
||||
//vlint(int)的size小于4时,测试是否能准确转为int输出
|
||||
int a = 222;
|
||||
int a2 = 22222222;
|
||||
@@ -66,18 +70,25 @@ public class VlIntTest {
|
||||
|
||||
//error:以bytes[]形式构造的VlInt,2^31-1 到 2^32-1之间的数会出错
|
||||
@Test
|
||||
public void getVlIntValue2Long() {
|
||||
public void getVlIntValue2Long() throws VlIntException {
|
||||
long l1 = new Long(0);
|
||||
long l2 = new Long(1);
|
||||
long l3 = new Long(4294967295L); //2^32-1
|
||||
byte[] b1 = new byte[]{0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2^31-1
|
||||
byte[] b2= new byte[]{0x00,(byte) 0x80,0x00,0x00,0x00};//2^31
|
||||
|
||||
byte[] b2 = new byte[]{0x00,(byte) 0x80,0x00,0x00,0x00};//2^31
|
||||
byte[] b3 = new byte[]{0,0,0,0,-128,0,0,0};
|
||||
BigInteger bigInteger = new BigInteger(b2);
|
||||
System.out.println(bigInteger.longValue());
|
||||
System.out.println(new BigInteger(b3).longValue());
|
||||
VlInt vlInt_long1 = new VlInt(l1); // size = 1
|
||||
VlInt vlInt_long2 = new VlInt(l2); // size = 1
|
||||
VlInt vlInt_long3 = new VlInt(l3); // size = 5
|
||||
VlInt vlInt_long4 = new VlInt(b1);
|
||||
VlInt vlInt_long5 = new VlInt(b2);
|
||||
System.out.println(vlInt_long2.toString());
|
||||
System.out.println(vlInt_long3.toString());
|
||||
System.out.println(vlInt_long4.toString());
|
||||
System.out.println(vlInt_long5.toString());
|
||||
//以VlInt(long)形式构造的VlInt
|
||||
assertEquals(vlInt_long1.getVlIntValue2Long(),l1);
|
||||
assertEquals(vlInt_long2.getVlIntValue2Long(),l2);
|
||||
@@ -85,6 +96,12 @@ public class VlIntTest {
|
||||
///以VlInt(bytes[])形式构造的VlInt
|
||||
assertEquals(vlInt_long4.getVlIntValue2Long(),2147483647L);//2^31-1
|
||||
assertEquals(vlInt_long5.getVlIntValue2Long(),2147483648L);//2^31
|
||||
|
||||
|
||||
assertEquals(vlInt_long5.getVlIntValue2Long(),2147483648L);//2^31
|
||||
byte[] bytes1 = new byte[]{127,-128,-128,-128,-128,-128,-128,-128};
|
||||
VlInt vlInt_6 = new VlInt(new BigInteger("21474836481"));
|
||||
assertEquals(vlInt_6.getVlIntValue2Long(), new BigInteger("21474836481").longValue());
|
||||
}
|
||||
|
||||
//error:以bytes[]形式构造的VlInt,在大于2^31-1的数会出错
|
||||
@@ -157,21 +174,25 @@ public class VlIntTest {
|
||||
int a1 = 0,a2 = 65535;
|
||||
long l = 4294967296L;
|
||||
int s1 = a2-a1,s2 = a1-a2;
|
||||
// long r1 = l-a1,r2 = l-a2; //差值越界int最大值
|
||||
long r1 = l-a1,r2 = l - (long)a2; //差值越界int最大值
|
||||
VlInt vlInt1 = new VlInt(a1);
|
||||
VlInt vlInt2 = new VlInt(a2);
|
||||
VlInt vlInt3 = new VlInt(l);
|
||||
logger.debug(vlInt1.toString());
|
||||
logger.debug(vlInt2.toString());
|
||||
logger.debug(vlInt3.toString());
|
||||
|
||||
assertEquals(vlInt2.subtract(vlInt1),s1);
|
||||
assertEquals(vlInt1.subtract(vlInt2),s2); //差为负数
|
||||
//error:差值越界int最大值
|
||||
// assertEquals(vlInt3.subtract(vlInt2),r1);
|
||||
assertEquals(vlInt3.subtract(vlInt2), r2);
|
||||
|
||||
|
||||
int sum1 = a1+a2;
|
||||
long sum2 = a2+l;
|
||||
//param:VlInt
|
||||
assertEquals(vlInt1.add(vlInt2),sum1);
|
||||
// assertEquals(vlInt1.add(vlInt3),sum2); //两者之和为long,越界int最大值
|
||||
assertEquals(vlInt2.add(vlInt3),sum2); //两者之和为long,越界int最大值
|
||||
//param:int
|
||||
assertEquals(vlInt1.add(a2),sum1);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ public class TestCert {
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
@@ -94,7 +93,7 @@ public class TestCert {
|
||||
|
||||
String pem2 = CertUtils.toPem(certificate, new byte[]{1,2,3,4,5,6,7,8},Common.SM4ECB);
|
||||
Certificate certFromPem2 = CertUtils.fromPem(pem2, new byte[]{1,2,3,4,5,6,7,8},Common.SM4ECB);
|
||||
logger.debug(certFromPem.toString());
|
||||
logger.debug(certFromPem2.toString());
|
||||
assertTrue(CertUtils.verifyCert(certFromPem2,certFromPem2));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import minsecurity.crypto.sm2.SM2KeyPair;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:22 2021/03/15
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class testPrivate {
|
||||
private void func1(){
|
||||
System.out.println("private func1");
|
||||
}
|
||||
|
||||
private void func2(int a, SM2KeyPair sm2KeyPair){
|
||||
System.out.println("a = " + a);
|
||||
System.out.println("private key = " + ByteUtils.toHexString(sm2KeyPair.getSm2PrivateKey().getBytes()));
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,7 @@ import static org.junit.Assert.*;
|
||||
*/public class ByteHelperTest {
|
||||
|
||||
@Test
|
||||
public void getTailBytes(){
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
public void getTailBytes() throws ByteHelperException {
|
||||
//偶数字节
|
||||
byte[] buffer = new byte[]{0x00,0x01,(byte) 0x80,0x7f}; //1
|
||||
byte[] test1 = new byte[]{0x01,(byte) 0x80,0x7f};
|
||||
@@ -23,16 +22,15 @@ import static org.junit.Assert.*;
|
||||
byte[] buffer2 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
|
||||
byte[] test2 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
|
||||
int startIndex = 1;
|
||||
byte[] b = byteHelper.getTailBytes(buffer,startIndex);
|
||||
byte[] b2 = byteHelper.getTailBytes(buffer2,startIndex);
|
||||
byte[] b = ByteHelper.getTailBytes(buffer,startIndex);
|
||||
byte[] b2 = ByteHelper.getTailBytes(buffer2,startIndex);
|
||||
assertArrayEquals(b,test1);
|
||||
assertArrayEquals(b2,test2);
|
||||
}
|
||||
|
||||
//todo:对start值进行限定,应当小于Byte数组的length
|
||||
@Test
|
||||
public void getFirstByte() {
|
||||
byte[] b0 = new byte[]{0x00,0x00,0x01};
|
||||
public void getFirstByte() throws ByteHelperException {
|
||||
byte[] b0 = new byte[]{0x00,0x00,1};
|
||||
byte[] test = new byte[]{0x7f,0x7f,0x7f,0x7f};
|
||||
VlInt start0 = new VlInt(b0);
|
||||
byte firstbyte = ByteHelper.getFirstByte(test,start0);
|
||||
@@ -41,7 +39,7 @@ import static org.junit.Assert.*;
|
||||
|
||||
|
||||
@Test
|
||||
public void getLenBytes() {
|
||||
public void getLenBytes() throws ByteHelperException {
|
||||
byte[] buffer = new byte[]{0x00,0x00,0x01, (byte) 0xff}; //255
|
||||
byte[] test = new byte[]{0x00,0x00,0x01};
|
||||
VlInt start = new VlInt(0);
|
||||
|
||||
Reference in new issue
Block a user