Merge branch 'master' of gitee.com:willfree/min-dev-java

This commit is contained in:
free will committed 2021-03-10 21:23:12 +08:00
commit 424fe59908
8 files changed
+866 -13

No files matched your search

+8
View File
@@ -76,6 +76,14 @@
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<!-- mapDB kv存储 -->
<!-- https://mvnrepository.com/artifact/org.mapdb/mapdb -->
<dependency>
<groupId>org.mapdb</groupId>
<artifactId>mapdb</artifactId>
<version>3.0.8</version>
</dependency>
</dependencies>
</project>
@@ -38,15 +38,7 @@ public class SM2PrivateKey implements PrivateKeyInterface {
* @date 2021/3/7
**/
public SM2PrivateKey(byte[] d) {
if(d.length == 32 || d.length == 33)
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
else {
try {
throw new SM2Exception("SM2私钥中,参数d长度应为32/33");
} catch (SM2Exception e) {
e.printStackTrace();
}
}
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
}
public SM2PrivateKey(byte[] d, ECDomainParameters parameters){
if(d.length == 32 || d.length == 33)
@@ -20,6 +20,7 @@ import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.Serializable;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@@ -43,10 +44,12 @@ public class Identity {
private PublicKeyInterface Pubkey;
private String Passwd;
private Certificate Cert;
private boolean IsDefault;
public Identity(String name, KeyParam keyParam,
PrivateKeyInterface prikey, byte[] prikeyRawByte,
PublicKeyInterface pubkey, String passwd, Certificate cert) {
PublicKeyInterface pubkey, String passwd, Certificate cert,
boolean isDefault) {
Name = name;
KeyParam = keyParam;
Prikey = prikey;
@@ -54,6 +57,7 @@ public class Identity {
Pubkey = pubkey;
Passwd = passwd;
Cert = cert;
IsDefault = isDefault;
}
@@ -240,16 +244,27 @@ public class Identity {
Cert = cert;
}
public boolean isDefault() {
return IsDefault;
}
public void setDefault(boolean aDefault) {
IsDefault = aDefault;
}
@Override
public String toString() {
return "Identity{" +
"Name='" + Name + '\'' +
", KeyParam=" + KeyParam +
", Prikey=" + Prikey +
", KeyParam.signAlgo=" + KeyParam.SignatureAlgorithm +
", KeyParam.pubAlgo=" + KeyParam.PublicKeyAlgorithm +
", Prikey=" + ByteUtils.toHexString(Prikey.getBytes()) +
", pubKey=" + ByteUtils.toHexString(Pubkey.getBytes()) +
", PrikeyRawByte=" + (PrikeyRawByte == null ? "null":ByteUtils.toHexString(PrikeyRawByte)) +
", Pubkey=" + Pubkey +
", Passwd='" + Passwd + '\'' +
", Cert=" + Cert +
", isDefault=" + IsDefault +
'}';
}
}
@@ -0,0 +1,97 @@
package minsecurity.identity.persist;
import minsecurity.Common;
import minsecurity.certificate.cert.CertException;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.sm2.SM2PrivateKey;
import minsecurity.crypto.sm2.SM2PublicKey;
import minsecurity.identity.Identity;
import minsecurity.identity.KeyParam;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.jetbrains.annotations.NotNull;
import org.mapdb.DataInput2;
import org.mapdb.DataOutput2;
import org.mapdb.Serializer;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
/*
* @Author: hongyu guo
* @Description: mapDB需要自定义序列化、反序列化方法
* @Version: 1.0.0
* @Date: 20:50 2021/03/09
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
// TODO: 加密版本的序列化
public class IdentitySerializer implements Serializer<Identity> {
@Override
public void serialize(@NotNull DataOutput2 dataOutput2, @NotNull Identity identity) throws IOException {
String name = identity.getName();
String pub = ByteUtils.toHexString(identity.getPubkey().getBytes());
String priv = ByteUtils.toHexString(identity.getPrikey().getBytes());
int pubAlgo = identity.getKeyParam().PublicKeyAlgorithm;
int signAlgo = identity.getKeyParam().SignatureAlgorithm;
String passwd = identity.getPasswd();
String cert = "";
try {
cert = CertUtils.toPem(identity.getCert(),null, Common.SM4CBC);
} catch (CertException | NoSuchPaddingException | InvalidAlgorithmParameterException |
NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException |
NoSuchProviderException | InvalidKeyException e) {
e.printStackTrace();
}
boolean isDefault = identity.isDefault();
String prikeyRawByte = identity.getPrikeyRawByte()== null ?
"" :ByteUtils.toHexString(identity.getPrikeyRawByte());
dataOutput2.writeUTF(name);
dataOutput2.writeUTF(pub);
dataOutput2.writeUTF(priv);
dataOutput2.writeInt(pubAlgo);
dataOutput2.writeInt(signAlgo);
dataOutput2.writeUTF(passwd);
dataOutput2.writeUTF(cert);
dataOutput2.writeBoolean(isDefault);
dataOutput2.writeUTF(prikeyRawByte);
}
@Override
public Identity deserialize(@NotNull DataInput2 dataInput2, int i) throws IOException {
String name = dataInput2.readUTF();
byte[] pub = ByteUtils.fromHexString(dataInput2.readUTF());
byte[] priv = ByteUtils.fromHexString(dataInput2.readUTF());
int pubAlgo = dataInput2.readInt();
int signAlgo = dataInput2.readInt();
String passwd = dataInput2.readUTF();
String certString = dataInput2.readUTF();
Certificate certificate = null;
try {
certificate = CertUtils.fromPem(certString, null, Common.SM4CBC);
} catch (CertException | BadPaddingException | NoSuchPaddingException |
InvalidAlgorithmParameterException | NoSuchAlgorithmException |
IllegalBlockSizeException | NoSuchProviderException | InvalidKeyException e) {
e.printStackTrace();
}
boolean isDefault = dataInput2.readBoolean();
byte[] prikeyRawByte = dataInput2.readUTF().equals("") ?
null : ByteUtils.fromHexString(dataInput2.readUTF());
KeyParam keyParam = new KeyParam(pubAlgo, signAlgo);
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey();
sm2PrivateKey.setBytes(priv);
SM2PublicKey sm2PublicKey = new SM2PublicKey();
sm2PublicKey.setBytes(pub);
return new Identity(name,keyParam, sm2PrivateKey,prikeyRawByte, sm2PublicKey, passwd, certificate, isDefault);
}
}
@@ -0,0 +1,152 @@
package minsecurity.identity.persist;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import minsecurity.identity.Identity;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.Serializer;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentMap;
/*
* @Author: hongyu guo
* @Description: 封装MapDB中需要的方法
* @Version: 1.0.0
* @Date: 10:34 2021/03/10
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class MapDB {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MapDB.class);
// TODO: 如有需要, 后续添加其他map
// TODO: 数据库加密?
public static final String defaultPath = "./target/test.db";
public static final String defaultIdentity = "identity";
public static final String defaultName = "/default";
private DB db;
private ConcurrentMap<String, Identity> identityMap;
private Serializer<Identity> customSerializer;
private static MapDB mapDB = null;
private MapDB(){
db = DBMaker.fileDB(defaultPath).closeOnJvmShutdown().transactionEnable().make();
customSerializer = new IdentitySerializer();
identityMap = db.hashMap(defaultIdentity)
.keySerializer(Serializer.STRING)
.valueSerializer(customSerializer)
.createOrOpen();
}
private MapDB(String filePath){
db = DBMaker.fileDB(filePath).closeOnJvmShutdown().transactionEnable().make();
customSerializer = new IdentitySerializer();
identityMap = db.hashMap(defaultIdentity)
.keySerializer(Serializer.STRING)
.valueSerializer(customSerializer)
.createOrOpen();
}
public static MapDB getInstance(){
if(mapDB == null){
mapDB = new MapDB();
}
return mapDB;
}
public static MapDB getInstance(String filePath){
if(mapDB == null){
mapDB = new MapDB(filePath);
}
return mapDB;
}
public Identity addIdentity(String name, Identity identity, boolean commit){
Identity id = identityMap.put(name,identity);
if(identityMap.size() == 1){
// 首次添加default Identity
setDefaultIdentity(name, commit);
}
if(commit){
db.commit();
}
return id;
}
public void closeDB(){
db.close();
mapDB = null;
}
public void commit(){
db.commit();
}
public Identity getIdentityByName(String name){
return identityMap.get(name);
}
public ArrayList<Identity> getAllIdentity(){
ArrayList<Identity> list = new ArrayList<>();
for(String key : identityMap.keySet()){
list.add(identityMap.get(key));
}
return list;
}
public boolean setDefaultIdentity(String name, boolean commit){
Identity identity = identityMap.get(name);
if(identity == null)
return false;
// 将所有identity的设置为非默认
for(String key : identityMap.keySet()){
Identity id = identityMap.get(key);
if(id.isDefault()) {
id.setDefault(false);
identityMap.put(key, id);
}
}
identity.setDefault(true);
identityMap.put(name, identity);
// set /default
identityMap.put(defaultName, identity);
if(commit){
db.commit();
}
return true;
}
public Identity getDefaultIdentity(){
return identityMap.get(defaultName);
}
public Identity deleteIdentity(String name, boolean commit){
// 不能删除默认Identity
if(name.equals(defaultName)){
logger.error("不能删除default identity");
return null;
}
Identity identity = identityMap.remove(name);
if(commit){
db.commit();
}
return identity;
}
public DB getDb() {
return db;
}
public void setDb(DB db) {
this.db = db;
}
public ConcurrentMap<String, Identity> getIdentityMap() {
return identityMap;
}
public void setIdentityMap(ConcurrentMap<String, Identity> identityMap) {
this.identityMap = identityMap;
}
}
@@ -41,7 +41,7 @@ public class TestIdentity {
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null);
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null, false);
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
Common.SM3withSM2, Common.SM2, "root", "root",
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
@@ -73,6 +73,8 @@ public class TestIdentity {
idFromBytes = Identity.load(bytesOfDump, null);
assertEquals(ByteUtils.toHexString(identity.getPrikey().getBytes()), ByteUtils.toHexString(idFromBytes.getPrikey().getBytes()));
assertEquals(ByteUtils.toHexString(identity.getPubkey().getBytes()), ByteUtils.toHexString(idFromBytes.getPubkey().getBytes()));
logger.debug(identity.getPrikey().getBytes().length + " " + identity.getPubkey().getBytes().length);
}
@@ -0,0 +1,167 @@
package minsecurity.identity;
import minsecurity.Common;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.sm2.SM2Base;
import minsecurity.crypto.sm2.SM2PrivateKey;
import minsecurity.crypto.sm2.SM2PublicKey;
import minsecurity.identity.persist.IdentitySerializer;
import minsecurity.identity.persist.MapDB;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.junit.Test;
import org.mapdb.BTreeMap;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.Serializer;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
/*
* @Author: hongyu guo
* @Description:
* @Version: 1.0.0
* @Date: 17:35 2021/03/09
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TestPersist {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestIdentity.class);
@Test
public void testMapDB() throws Exception {
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
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();
// BigInteger bigInteger = priKey.getD();
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null, false);
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
Common.SM3withSM2, Common.SM2, "root", "root",
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
Common.CertSign, true, System.currentTimeMillis());
CertUtils.signCert(certificate, sm2PrivateKey);
identity.setCert(certificate);
MapDB mapDB = MapDB.getInstance("./target/test.db");
int random = new Random().nextInt();
mapDB.addIdentity("/test" + random, identity, false);
mapDB.commit();
mapDB.closeDB();
mapDB = MapDB.getInstance("./target/test.db");
ArrayList<Identity> arrayList = mapDB.getAllIdentity();
logger.debug("db size: {}",arrayList.size());
Identity id = mapDB.getIdentityByName("/test" + random);
logger.debug(identity.toString());
logger.debug(id.toString());
}
@Test
public void testMapDB2() throws Exception {
MapDB mapDB = MapDB.getInstance("./target/test.db");
for(int i = 0; i < 100; i++){
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
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();
// BigInteger bigInteger = priKey.getD();
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null, false);
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
Common.SM3withSM2, Common.SM2, "root", "root",
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
Common.CertSign, true, System.currentTimeMillis());
CertUtils.signCert(certificate, sm2PrivateKey);
identity.setCert(certificate);
int random = new Random().nextInt();
mapDB.addIdentity("/test" + random, identity, false);
}
mapDB.commit();
ArrayList<Identity> arrayList = mapDB.getAllIdentity();
logger.debug("size = {}", arrayList.size());
for(Identity id : arrayList){
logger.debug(id.toString());
}
}
@Test
public void testHashmap(){
HashMap<String, Integer> primeNumbers = new HashMap<>();
// 往HasMap中添加映射
primeNumbers.put("Two", 2);
primeNumbers.put("Three", 3);
primeNumbers.put("Five", 5);
System.out.println("HashMap: " + primeNumbers);
// 得到value
Integer value = primeNumbers.get("Three");
System.out.println("key Three 对应的 value: " + value);
}
@Test
public void testDefault() throws Exception {
MapDB mapDB = MapDB.getInstance("./target/test.db");
for(int i = 0; i < 20; i++){
int random = new Random().nextInt();
Identity identity = generateIdentity("/test/" + random);
mapDB.addIdentity(identity.getName(), identity, false);
}
// mapDB.commit();
ArrayList<Identity> arrayList = mapDB.getAllIdentity();
logger.debug("size = {}", arrayList.size());
for(Identity id : arrayList){
logger.debug(id.getName() + " " + id.isDefault());
}
logger.debug("----setDefault----");
Identity identity = generateIdentity("/abc");
identity.setDefault(true);
mapDB.addIdentity("/abc", identity, false);
mapDB.setDefaultIdentity("/abc",false);
mapDB.commit();
arrayList = mapDB.getAllIdentity();
for(Identity id : arrayList){
logger.debug(id.getName() + " " + id.isDefault());
}
logger.debug("get default");
logger.debug(mapDB.getDefaultIdentity().toString());
}
private Identity generateIdentity(String name) throws Exception {
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
byte[] d = priKey.getD().toByteArray();
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
Identity identity = new Identity(name,keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null, false);
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
Common.SM3withSM2, Common.SM2, "root", "root",
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
Common.CertSign, true, System.currentTimeMillis());
CertUtils.signCert(certificate, sm2PrivateKey);
identity.setCert(certificate);
return identity;
}
}
+420
View File
@@ -0,0 +1,420 @@
package util;
import org.junit.Test;
import static org.junit.Assert.*;
/*
* @Author: feng Zhao
* @Description:
* @Version: 1.0.0
* @Date: 16:20 2021/3/9
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/public class ByteHelperTest {
// @Test
// public void getFirstByte() {
//
// }
//
// @Test
// public void getLenBytes() {
// }
//
// @Test
// public void testGetLenBytes() {
// }
// 0 1 127 128 255
@Test
public void uint8ToInt() {
byte b1 = 0x00;
byte b2 = 0x01;
byte b3 = 0x7f;
byte b4 = (byte) 0x80;
byte b5 = (byte) 0xff;
ByteHelper byteHelper = new ByteHelper();
int a1 = byteHelper.uint8ToInt(b1);
int a2 = byteHelper.uint8ToInt(b2);
int a3 = byteHelper.uint8ToInt(b3);
int a4 = byteHelper.uint8ToInt(b4);
int a5 = byteHelper.uint8ToInt(b5);
assertEquals(a1,0);
assertEquals(a2,1);
assertEquals(a3,127);
assertEquals(a4,128);
assertEquals(a5,255);
}
@Test
public void uint8ToByte() {
byte b1 = 0x00;
byte b2 = 0x01;
byte b3 = 0x7f;
byte b4 = (byte) 0x80;
byte b5 = (byte) 0xff;
ByteHelper byteHelper = new ByteHelper();
byte a1 = byteHelper.uint8ToByte(0);
byte a2 = byteHelper.uint8ToByte(1);
byte a3 = byteHelper.uint8ToByte(127);
byte a4 = byteHelper.uint8ToByte(128);
byte a5 = byteHelper.uint8ToByte(255);
assertEquals(a1,b1);
assertEquals(a2,b2);
assertEquals(a3,b3);
assertEquals(a4,b4);
assertEquals(a5,b5);
}
@Test
public void uint8ToLong() {
byte b1 = 0x00;
byte b2 = 0x01;
byte b3 = 0x7f;
byte b4 = (byte) 0x80;
byte b5 = (byte) 0xff;
ByteHelper byteHelper = new ByteHelper();
long a1 = byteHelper.uint8ToLong(b1);
long a2 = byteHelper.uint8ToLong(b2);
long a3 = byteHelper.uint8ToLong(b3);
long a4 = byteHelper.uint8ToLong(b4);
long a5 = byteHelper.uint8ToLong(b5);
assertEquals(a1,0);
assertEquals(a2,1);
assertEquals(a3,127);
assertEquals(a4,128);
assertEquals(a5,255);
}
@Test
public void uint16ToInt() {
byte[] b1 = new byte[]{0x00,0x00}; //0
byte[] b2 = new byte[]{0x00,0x01}; //1
byte[] b3 = new byte[]{0x00,0x7f}; //127
byte[] b4 = new byte[]{0x00, (byte) 0x80}; //128
byte[] b5 = new byte[]{0x00, (byte) 0xff}; //255
byte[] b6 = new byte[]{0x01, 0x00}; //256
byte[] b7 = new byte[]{0x7f, (byte) 0xff}; //32767
byte[] b8 = new byte[]{(byte) 0x80, 0x00}; //32768
byte[] b9 = new byte[]{(byte) 0xff, (byte) 0xff};//65535
ByteHelper byteHelper = new ByteHelper();
int a1 = byteHelper.uint16ToInt(b1);
int a2 = byteHelper.uint16ToInt(b2);
int a3 = byteHelper.uint16ToInt(b3);
int a4 = byteHelper.uint16ToInt(b4);
int a5 = byteHelper.uint16ToInt(b5);
int a6 = byteHelper.uint16ToInt(b6);
int a7 = byteHelper.uint16ToInt(b7);
int a8 = byteHelper.uint16ToInt(b8);
int a9 = byteHelper.uint16ToInt(b9);
assertEquals(a1,0);
assertEquals(a2,1);
assertEquals(a3,127);
assertEquals(a4,128);
assertEquals(a5,255);
assertEquals(a6,256);
assertEquals(a7,32767);
assertEquals(a8,32768);
assertEquals(a9,65535);
}
//error
@Test
public void uint16ToByteArray() {
ByteHelper byteHelper = new ByteHelper();
byte[] b1 = new byte[]{0x00,0x00}; //0
byte[] b2 = new byte[]{0x00,0x01}; //1
byte[] b3 = new byte[]{0x00,0x7f}; //127
byte[] b4 = new byte[]{0x00, (byte) 0x80}; //128
byte[] b5 = new byte[]{0x00, (byte) 0xff}; //255
byte[] b6 = new byte[]{0x01, 0x00}; //256
byte[] b7 = new byte[]{0x7f, (byte) 0xff}; //32767
byte[] b8 = new byte[]{(byte) 0x80, 0x00}; //32768
byte[] b9 = new byte[]{(byte) 0xff, (byte) 0xff};//65535
byte [] a1 = byteHelper.uint16ToByteArray(0);
byte [] a2 = byteHelper.uint16ToByteArray(1);
byte [] a3 = byteHelper.uint16ToByteArray(127);
byte [] a4 = byteHelper.uint16ToByteArray(128);
byte [] a5 = byteHelper.uint16ToByteArray(255);
byte [] a6 = byteHelper.uint16ToByteArray(256);
byte [] a7 = byteHelper.uint16ToByteArray(32767);
byte [] a8 = byteHelper.uint16ToByteArray(32768);
byte [] a9 = byteHelper.uint16ToByteArray(65535);
assertEquals(a1,b1);
assertEquals(a2,b2);
assertEquals(a3,b3);
assertEquals(a4,b4);
assertEquals(a5,b5);
assertEquals(a6,b6);
assertEquals(a7,b7);
assertEquals(a8,b8);
assertEquals(a9,b9);
assertEquals(a6,b6);
assertEquals(a7,b7);
assertEquals(a8,b8);
assertEquals(a9,b9);
}
@Test
public void uint16ToLong() {
byte[] b1 = new byte[]{0x00,0x00}; //0
byte[] b2 = new byte[]{0x00,0x01}; //1
byte[] b3 = new byte[]{0x00,0x7f}; //127
byte[] b4 = new byte[]{0x00, (byte) 0x80}; //128
byte[] b5 = new byte[]{0x00, (byte) 0xff}; //255
byte[] b6 = new byte[]{0x01, 0x00}; //256
byte[] b7 = new byte[]{0x7f, (byte) 0xff}; //32767
byte[] b8 = new byte[]{(byte) 0x80, 0x00}; //32768
byte[] b9 = new byte[]{(byte) 0xff, (byte) 0xff};//65535
ByteHelper byteHelper = new ByteHelper();
long a1 = byteHelper.uint16ToLong(b1);
long a2 = byteHelper.uint16ToLong(b2);
long a3 = byteHelper.uint16ToLong(b3);
long a4 = byteHelper.uint16ToLong(b4);
long a5 = byteHelper.uint16ToLong(b5);
long a6 = byteHelper.uint16ToLong(b6);
long a7 = byteHelper.uint16ToLong(b7);
long a8 = byteHelper.uint16ToLong(b8);
long a9 = byteHelper.uint16ToLong(b9);
assertEquals(a1,0);
assertEquals(a2,1);
assertEquals(a3,127);
assertEquals(a4,128);
assertEquals(a5,255);
assertEquals(a6,256);
assertEquals(a7,32767);
assertEquals(a8,32768);
assertEquals(a9,65535);
}
//error
@Test
public void uint32ToLong() {
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
byte[] b2 = new byte[]{0x00,0x00,0x00,0x01}; //1
byte[] b3 = new byte[]{0x00,0x00,0x00,0x7f}; //127
byte[] b4 = new byte[]{0x00,0x00,0x00, (byte) 0x80}; //128
byte[] b5 = new byte[]{0x00,0x00,0x00, (byte) 0xff}; //255
byte[] b6 = new byte[]{0x00,0x00,0x01, 0x00}; //256
byte[] b7 = new byte[]{0x00,0x00,0x7f, (byte) 0xff}; //32767
byte[] b8 = new byte[]{0x00,0x00,(byte) 0x80, 0x00}; //32768
byte[] b9 = new byte[]{0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
byte[] b10 = new byte[]{0x00,0x01,0x00,0x00}; //65536
byte[] b11 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
byte[] b12 = new byte[]{(byte) 0x80,0x00,0x00,0x00}; //2147483648
byte[] b13 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//4294967295
ByteHelper byteHelper = new ByteHelper();
long a1 = byteHelper.uint32ToLong(b1);
long a2 = byteHelper.uint32ToLong(b2);
long a3 = byteHelper.uint32ToLong(b3);
long a4 = byteHelper.uint32ToLong(b4);
long a5 = byteHelper.uint32ToLong(b5);
long a6 = byteHelper.uint32ToLong(b6);
long a7 = byteHelper.uint32ToLong(b7);
long a8 = byteHelper.uint32ToLong(b8);
long a9 = byteHelper.uint32ToLong(b9);
long a10 = byteHelper.uint32ToLong(b10);
long a11 = byteHelper.uint32ToLong(b11);
long a12 = byteHelper.uint32ToLong(b12);
long a13 = byteHelper.uint32ToLong(b13);
assertEquals(a1,0);
assertEquals(a2,1);
assertEquals(a3,127);
assertEquals(a4,128);
assertEquals(a5,255);
assertEquals(a6,256);
assertEquals(a7,32767);
assertEquals(a8,32768);
assertEquals(a9,65535);
assertEquals(a10,65536);
assertEquals(a11,2147483647);
assertEquals(a12,2147483648L);
assertEquals(a13,4294967295L);
}
//error
@Test
public void uint32ToByteArray() {
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
byte[] b2 = new byte[]{0x00,0x00,0x00,0x01}; //1
byte[] b3 = new byte[]{0x00,0x00,0x00,0x7f}; //127
byte[] b4 = new byte[]{0x00,0x00,0x00, (byte) 0x80}; //128
byte[] b5 = new byte[]{0x00,0x00,0x00, (byte) 0xff}; //255
byte[] b6 = new byte[]{0x00,0x00,0x01, 0x00}; //256
byte[] b7 = new byte[]{0x00,0x00,0x7f, (byte) 0xff}; //32767
byte[] b8 = new byte[]{0x00,0x00,(byte) 0x80, 0x00}; //32768
byte[] b9 = new byte[]{0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
byte[] b10 = new byte[]{0x00,0x01,0x00,0x00}; //65536
byte[] b11 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
byte[] b12 = new byte[]{(byte) 0x80,0x00,0x00,0x00}; //2147483648
byte[] b13 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//4294967295
ByteHelper byteHelper = new ByteHelper();
byte [] a1 = byteHelper.uint32ToByteArray(0);
byte [] a2 = byteHelper.uint16ToByteArray(1);
byte [] a3 = byteHelper.uint32ToByteArray(127);
byte [] a4 = byteHelper.uint32ToByteArray(128);
byte [] a5 = byteHelper.uint32ToByteArray(255);
byte [] a6 = byteHelper.uint32ToByteArray(256);
byte [] a7 = byteHelper.uint32ToByteArray(32767);
byte [] a8 = byteHelper.uint32ToByteArray(32768);
byte [] a9 = byteHelper.uint32ToByteArray(65535);
byte [] a10 = byteHelper.uint32ToByteArray(65536);
byte [] a11 = byteHelper.uint32ToByteArray(2147483647);
byte [] a12 = byteHelper.uint32ToByteArray(2147483648l);
byte [] a13 = byteHelper.uint32ToByteArray(4294967295l);
assertEquals(a1,b1);
assertEquals(a2,b2);
assertEquals(a3,b3);
assertEquals(a4,b4);
assertEquals(a5,b5);
assertEquals(a6,b6);
assertEquals(a7,b7);
assertEquals(a8,b8);
assertEquals(a9,b9);
assertEquals(a6,b6);
assertEquals(a7,b7);
assertEquals(a8,b8);
assertEquals(a9,b9);
assertEquals(a10,b10);
assertEquals(a11,b11);
assertEquals(a12,b12);
assertEquals(a13,b13);
}
//error
//todo 大于2^64次方无法表示
@Test
public void uint64ToLong() {
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //0
byte[] b2 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; //1
byte[] b3 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f}; //127
byte[] b4 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00, (byte) 0x80}; //128
byte[] b5 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00, (byte) 0xff}; //255
byte[] b6 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x00}; //256
byte[] b7 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x7f, (byte) 0xff}; //32767
byte[] b8 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0x80, 0x00}; //32768
byte[] b9 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
byte[] b10 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00}; //65536
byte[] b11 = new byte[]{0x00,0x00,0x00,0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
byte[] b12 = new byte[]{0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x00}; //2147483648
byte[] b13 = new byte[]{0x00,0x00,0x00,0x00,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//4294967295
byte[] b14 = new byte[]{0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};//4294967296
byte[] b15 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//9223372036854775807
byte[] b16 = new byte[]{(byte) 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//9223372036854775808
byte[] b17 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//18446744073709551615
ByteHelper byteHelper = new ByteHelper();
// byte[] b_test = new byte[]{0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x01};//2147483649
// long a_test = byteHelper.uint64ToLong(b_test);
// assertEquals(a_test,2147483649l);
long a1 = byteHelper.uint64ToLong(b1);
long a2 = byteHelper.uint64ToLong(b2);
long a3 = byteHelper.uint64ToLong(b3);
long a4 = byteHelper.uint64ToLong(b4);
long a5 = byteHelper.uint64ToLong(b5);
long a6 = byteHelper.uint64ToLong(b6);
long a7 = byteHelper.uint64ToLong(b7);
long a8 = byteHelper.uint64ToLong(b8);
long a9 = byteHelper.uint64ToLong(b9);
long a10 = byteHelper.uint64ToLong(b10);
long a11 = byteHelper.uint64ToLong(b11);
long a12 = byteHelper.uint64ToLong(b12);
long a13 = byteHelper.uint64ToLong(b13);
long a14 = byteHelper.uint64ToLong(b14);
long a15 = byteHelper.uint64ToLong(b15);
long a16 = byteHelper.uint64ToLong(b16);
long a17 = byteHelper.uint64ToLong(b17);
assertEquals(a1,0);
assertEquals(a2,1);
assertEquals(a3,127);
assertEquals(a4,128);
assertEquals(a5,255);
assertEquals(a6,256);
assertEquals(a7,32767);
assertEquals(a8,32768);
assertEquals(a9,65535);
assertEquals(a10,65536);
assertEquals(a11,2147483647);
assertEquals(a12,2147483648L);
assertEquals(a13,4294967295L);
assertEquals(a14,4294967296L);
assertEquals(a15,9223372036854775807L);
//待解决:long number too large,暂无法表示
// assertEquals(a16,9223372036854775808L);
// assertEquals(a17,18446744073709551615L);
}
//error
//todo 大于2^64次方无法表示
@Test
public void uint64ToByteArray() {
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //0
byte[] b2 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; //1
byte[] b3 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f}; //127
byte[] b4 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00, (byte) 0x80}; //128
byte[] b5 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00, (byte) 0xff}; //255
byte[] b6 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x00}; //256
byte[] b7 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x7f, (byte) 0xff}; //32767
byte[] b8 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0x80, 0x00}; //32768
byte[] b9 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
byte[] b10 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00}; //65536
byte[] b11 = new byte[]{0x00,0x00,0x00,0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
byte[] b12 = new byte[]{0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x00}; //2147483648
byte[] b13 = new byte[]{0x00,0x00,0x00,0x00,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//4294967295
byte[] b14 = new byte[]{0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};//4294967296
byte[] b15 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//9223372036854775807
byte[] b16 = new byte[]{(byte) 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//9223372036854775808
byte[] b17 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//18446744073709551615
ByteHelper byteHelper = new ByteHelper();
byte [] a1 = byteHelper.uint64ToByteArray(0);
byte [] a2 = byteHelper.uint64ToByteArray(1);
byte [] a3 = byteHelper.uint64ToByteArray(127);
byte [] a4 = byteHelper.uint64ToByteArray(128);
byte [] a5 = byteHelper.uint64ToByteArray(255);
byte [] a6 = byteHelper.uint64ToByteArray(256);
byte [] a7 = byteHelper.uint64ToByteArray(32767);
byte [] a8 = byteHelper.uint64ToByteArray(32768);
byte [] a9 = byteHelper.uint64ToByteArray(65535);
byte [] a10 = byteHelper.uint64ToByteArray(65536);
byte [] a11 = byteHelper.uint64ToByteArray(2147483647);
byte [] a12 = byteHelper.uint64ToByteArray(2147483648l);
byte [] a13 = byteHelper.uint64ToByteArray(4294967295l);
byte [] a14 = byteHelper.uint64ToByteArray(4294967296L);
byte [] a15 = byteHelper.uint64ToByteArray(9223372036854775807L);
// byte [] a16 = byteHelper.uint64ToByteArray(9223372036854775808L);
// byte [] a17 = byteHelper.uint64ToByteArray(18446744073709551615L);
assertEquals(a1,b1);
assertEquals(a2,b2);
assertEquals(a3,b3);
assertEquals(a4,b4);
assertEquals(a5,b5);
assertEquals(a6,b6);
assertEquals(a7,b7);
assertEquals(a8,b8);
assertEquals(a9,b9);
assertEquals(a6,b6);
assertEquals(a7,b7);
assertEquals(a8,b8);
assertEquals(a9,b9);
assertEquals(a10,b10);
assertEquals(a11,b11);
assertEquals(a12,b12);
assertEquals(a13,b13);
assertEquals(a14,b14);
assertEquals(a15,b15);
// assertEquals(a16,b16);
// assertEquals(a17,b17);
}
}