Merge remote-tracking branch 'origin/master'

This commit is contained in:
zhaofeng233
2021-03-11 00:11:47 +08:00
26 changed files with 644 additions and 59 deletions
+1 -1
View File
@@ -76,7 +76,7 @@ public class CanBePrefix implements TlvComponentBase,InitialAble, IEncodingAble
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvCanBePrefix))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvCanBePrefix))){
return false;
}
+1 -1
View File
@@ -78,7 +78,7 @@ public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAb
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvCongestionMark))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvCongestionMark))){
return false;
}
+1 -1
View File
@@ -78,7 +78,7 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvFreshnessPeriod))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvFreshnessPeriod))){
return false;
}
+1 -1
View File
@@ -73,7 +73,7 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvHopLimit))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvHopLimit))){
return false;
}
+94
View File
@@ -0,0 +1,94 @@
package component;
import encoding.Block;
import encoding.Encoder;
import encoding.IEncodingAble;
import java.util.List;
/*
* @Author: Wang Feng
* @Description: 标识一个MIN网络标识
// 1. 身份标识为由单个名称组件组成的平面标识
// 2. 内容标识为一到多个名称组件组成的层级化标识
// 3. 包格式如下:
// Identifier = 101 TLV-LENGTH
// *IdentifierComponent
* @Version: 1.0.0
* @Date: 17:34 2021/3/10
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
class IdentifierComponentContainer{
private List<IdentifierComponent> identifierComponents;
}
public class Identifier implements TlvComponentBase, IEncodingAble {
private IdentifierComponentContainer components;
/**
* 根据一个 TLV Block 创建一个Identifier
* @param block
*/
public Identifier(Block block){
this.buildIdentifierByBlock(block);
}
/**
* 根据标识的字符串标识创建一个标识
* @param identifierString
*/
public Identifier(String identifierString){
this.buildIdentifierByString(identifierString);
}
/**
* 根据components构造一个Identifier
* @param container
*/
public Identifier(IdentifierComponentContainer container){
this.buildIdentifierByComponents(container);
}
/**
* 根据一个 TLV Block 初始化当前Identifier
* @param block
*/
public boolean buildIdentifierByBlock(Block block){
// 如果不存在子 TLV 则不可能是一个 Identifier
if(!block.parseSubElements()){
return false;
}
if(block.getSubElements().length()<1){
return false;
}
return this.wireDecode(block);
}
/**
* 根据标识字符串初始化当前Identifier
* @param identifierString
* @return
*/
public boolean buildIdentifierByString(String identifierString){
if((identifierString.length()<=0)||(!identifierString.startsWith("/"))){
return false;
}
String[] componentStrings=identifierString.split("/");
IdentifierComponentContainer identifierComponentContainer=new IdentifierComponentContainer();
//...
return true;
}
@Override
public int wireEncode(Encoder encoder) {
return 0;
}
@Override
public boolean wireDecode(Block block) {
return false;
}
}
@@ -3,14 +3,15 @@ package component;
/*
* @Author: Wang Feng
* @Description: 名称组件
* todo: toUri()函数待完善
* @Version: 1.0.0
* @Date: 21:26 2021/3/9
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
import encoding.Block;
import encoding.Encoder;
import encoding.IEncodingAble;
import encoding.*;
import util.ByteHelper;
import util.StringHelper;
// @Description:
// 1. 参考 => https://named-data.net/doc/tech-memos/naming-conventions.pdf ,借鉴但不是按这个实现的
@@ -25,23 +26,199 @@ import encoding.IEncodingAble;
// 0x01 非负整数(可以用1、2、4、8个字节表示)
// 0x02 任意字节数组
public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
private Block block;
private Block identifierBlock; // IdentifierComponent数据以TLV形式存储下来
private byte marker; // 标记 => 用来区分不同类型的 IdentifierComponent
private String stringValue; // 如果 Marker == 0x00,则本组件存储一个字符串
private long intValue; // 如果 Marker == 0x01,则本组件存储一个非负整型
private byte[] byteArrayValue; // 如果 Marker == 0x02,则本组件存储一个字节数组
public static final byte MarkerString = 0x00;
public static final byte MarkerNonNegativeInteger = 0x01;
public static final byte MarkerByteArray = 0x02;
private static final byte MarkerString = 0x00;
private static final byte MarkerNonNegativeInteger = 0x01;
private static final byte MarkerByteArray = 0x02;
/**
* 用于判断某个字节是否在已分配的Marker列表当中
* @param marker
* @return
*/
private boolean isInMarker(byte marker){
if((marker==MarkerString)||(marker==MarkerNonNegativeInteger)||(marker==MarkerByteArray)){
return true;
}else{
return false;
}
}
/**
* 根据一个字符串构造一个 IdentifierComponent
* @param nameComponent
*/
public IdentifierComponent(String nameComponent){
this.identifierBlock=new Block();
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
this.identifierBlock.setLength(new VlInt(nameComponent.length()+1));
byte[] value=new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
value[0]=MarkerString;
System.arraycopy(nameComponent.getBytes(),0,value,1,nameComponent.getBytes().length);
this.identifierBlock.setValue(value);
this.marker=MarkerString;
this.stringValue=nameComponent;
}
/**
* 根据一个非负整数构造一个 IdentifierComponent
* @param intValue
*/
public IdentifierComponent(long intValue){
this.identifierBlock=new Block();
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
this.identifierBlock.setLength(new VlInt(new VlInt(intValue).size()+1));
byte[] value=new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
value[0]=MarkerNonNegativeInteger;
System.arraycopy(new VlInt(intValue).getVlIntBytes(),0,value,1,new VlInt(intValue).size());
this.identifierBlock.setValue(value);
this.marker=MarkerNonNegativeInteger;
this.intValue=intValue;
}
/**
* 根据一个字节数组构造一个 IdentifierComponent
* @param arr
*/
public IdentifierComponent(byte[] arr){
this.identifierBlock=new Block();
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
this.identifierBlock.setLength(new VlInt(arr.length+1));
byte[] value=new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
value[0]=MarkerByteArray;
System.arraycopy(arr,0,value,1,arr.length);
this.identifierBlock.setValue(value);
this.marker=MarkerByteArray;
this.byteArrayValue=arr;
}
/**
* 根据一个 TLV Block 创建一个 IdentifierComponent
* @param block
* @return
*/
public IdentifierComponent(Block block){
this.wireDecode(block);
}
/**
* 以字符串形式获取本 IdentifierComponent 的值
* @return
*/
public String getString(){
return this.stringValue;
}
/**
* 以非负整数的形式获取本 IdentifierComponent 的值
* @return
*/
public long getNonNegativeInteger(){
return this.intValue;
}
/**
* 以字节数组的形式获取本 IdentifierComponent 的值
* @return
*/
public byte[] getByteArray(){
return this.byteArrayValue;
}
/**
* 判断当前 IdentifierComponent 是否是存储字符串的类型
* @return
*/
public boolean isString(){
return this.marker==MarkerString;
}
/**
* 判断当前 IdentifierComponent 是否是存储非负整数的类型
* @return
*/
public boolean isNonNegativeInteger(){
return this.marker==MarkerNonNegativeInteger;
}
/**
* 判断当前 IdentifierComponent 是否是存储字节数组的类型
* @return
*/
public boolean isByteArray(){
return this.marker==MarkerByteArray;
}
/**
* 线速编码,将 IdentifierComponent 编码成网络中传输的字节数组格式
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
return 0;
return this.identifierBlock.encode(encoder);
}
/**
* 线速解码,将TLV Block解码成 IdentifierComponent
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) {
return false;
this.identifierBlock=new Block(block);
//todo:越界问题
int length=this.identifierBlock.getLength().getVlIntValue2Int();
byte[] value=this.identifierBlock.getValue();
// 要求TLV-LENGTH至少为1,因为 IdentifierComponent 使用 TLV-VALUE 中的第一个字节作为Marker
if(length<1){
return false;
}
// 判断Marker是否有效
if(!isInMarker(value[0])){
return false;
}
this.marker=value[0];
// 根据不同类型的Marker,给不同的成员赋值
byte[] v=ByteHelper.getTailBytes(value,1);
if(isString()){
this.stringValue=new String(v);
}else if(isNonNegativeInteger()){
// 如果Marker指定是非负整型,则TLV-LENGTH的有效值为2、3、5、9
if((length!=2)&&(length!=3)&&(length!=5)&&(length!=9)){
return false;
}
this.intValue=TLV.readNonNegativeInteger(v,0,v.length);
}else{ // byte[]
this.byteArrayValue=v;
}
return true;
}
/**
* 将 IdentifierComponent 使用百分号编码规则编码
* @return
*/
public String toUri(){
if(isString()){
return StringHelper.escape(this.stringValue);
}else if(isNonNegativeInteger()){
// return StringHelper.FormatUint(this.intValue,10);
// todo: 需要对long类型按照指定编码规则编码成字符串
return "";
}else{
return StringHelper.escape(new String(this.byteArrayValue));
}
}
}
@@ -78,7 +78,7 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvInterestLifeTime))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvInterestLifeTime))){
return false;
}
+1 -1
View File
@@ -76,7 +76,7 @@ public class MustBeRefresh implements TlvComponentBase,InitialAble, IEncodingAbl
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvMustBeRefresh))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvMustBeRefresh))){
return false;
}
@@ -99,7 +99,7 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvReadOnlyField))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
return false;
}
+3 -3
View File
@@ -69,7 +69,7 @@ public class MutableField implements TlvComponentBase,IEncodingAble {
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvMutableField))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvMutableField))){
return false;
}
@@ -80,11 +80,11 @@ public class MutableField implements TlvComponentBase,IEncodingAble {
for (int i = 0; i < block.getSubElements().length(); i++) {
Block subBlock=block.getSubElements().getBlock(i);
if(subBlock.getTlvType().isEqual(TLV.TlvMutableProtectField)){
if(subBlock.getType().isEqual(TLV.TlvMutableProtectField)){
if(!this.mutableProtectField.wireDecode(subBlock)){
return false;
}
}else if(subBlock.getTlvType().isEqual(TLV.TlvMutableDangerousField)){
}else if(subBlock.getType().isEqual(TLV.TlvMutableDangerousField)){
if(!this.mutableDangerousField.wireDecode(subBlock)){
return false;
}
@@ -99,7 +99,7 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvReadOnlyField))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
return false;
}
+1 -1
View File
@@ -78,7 +78,7 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvNackHeader))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNackHeader))){
return false;
}
+1 -1
View File
@@ -88,7 +88,7 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvNonce))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNonce))){
return false;
}
+1 -1
View File
@@ -93,7 +93,7 @@ public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvPayload))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvPayload))){
return false;
}
+1 -1
View File
@@ -99,7 +99,7 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvReadOnlyField))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
return false;
}
+1 -1
View File
@@ -88,7 +88,7 @@ public class TTL implements IEncodingAble {
@Override
public boolean wireDecode(Block block) {
// 检查Type是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvTTL))){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvTTL))){
return false;
}
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
+13 -5
View File
@@ -20,6 +20,10 @@ public class Block {
private ElementContainer elements; // TLV-sub-elements
private byte[] raw; // TLV编码后的字节数组
public Block(){
this.tlvType=new VlInt(TLV.TlvInvalid);
}
/**
* 从一个包含TLV的字节数组中构造一个 Block 对象
* @param buffer
@@ -27,7 +31,7 @@ public class Block {
* 则会对比解析到的 TLV-LENGTH 与 TLV-VALUE的实际长度是否匹配
* @return
*/
public Block createBlockByBuffer(byte[] buffer,boolean verifyLength){
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength){
if(buffer.length==0){
return null;
}
@@ -57,7 +61,7 @@ public class Block {
* @param verifyLength
* @return
*/
public Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
public static Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
return new Block(tlvType,tlvLength,buffer,verifyLength);
}
@@ -103,14 +107,18 @@ public class Block {
}
public Block(Block block){
this.tlvType=block.tlvType;
this.length=block.length;
this.value=block.value;
this.elements=block.elements;
this.raw=block.raw;
}
public VlInt getTlvType() {
public VlInt getType() {
return tlvType;
}
public void setTlvType(VlInt tlvType) {
public void setType(VlInt tlvType) {
this.tlvType = tlvType;
}
+2 -2
View File
@@ -55,7 +55,7 @@ public class ElementContainer{
public void removeElements(VlInt tlvType){
List<Block> elementContainer=new LinkedList<>();
for(Block block: this.elementContainer){
if(!block.getTlvType().isEqual(tlvType)){
if(!block.getType().isEqual(tlvType)){
elementContainer.add(block);
}
}
@@ -69,7 +69,7 @@ public class ElementContainer{
*/
public Block getElement(VlInt tlvType){
for(Block block: this.elementContainer){
if(block.getTlvType().isEqual(tlvType)){
if(block.getType().isEqual(tlvType)){
return block;
}
}
+2 -2
View File
@@ -256,7 +256,7 @@ public class Encoder {
* @return
*/
public int prependBlock(Block block){
return this.prependByteArrayBlock(block.getTlvType(),block.getValue(),new SizeT(block.getLength()));
return this.prependByteArrayBlock(block.getType(),block.getValue(),new SizeT(block.getLength()));
}
/**
@@ -265,7 +265,7 @@ public class Encoder {
* @return
*/
public int appendBlock(Block block){
return this.appendByteArrayBlock(block.getTlvType(),block.getValue(),new SizeT(block.getLength()));
return this.appendByteArrayBlock(block.getType(),block.getValue(),new SizeT(block.getLength()));
}
/**
@@ -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)
@@ -1,6 +1,5 @@
package minsecurity.identity.persist;
import com.fasterxml.jackson.databind.ObjectMapper;
import minsecurity.Common;
import minsecurity.certificate.cert.CertException;
import minsecurity.certificate.cert.CertUtils;
@@ -31,8 +30,9 @@ import java.security.NoSuchProviderException;
* @Date: 20:50 2021/03/09
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class IdentitySerializer implements Serializer<Identity> {
// TODO: 加密版本的序列化
public class IdentitySerializer implements Serializer<Identity> {
@Override
public void serialize(@NotNull DataOutput2 dataOutput2, @NotNull Identity identity) throws IOException {
@@ -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;
}
}
+13
View File
@@ -12,6 +12,19 @@ import encoding.VlInt;
*/
public class ByteHelper {
/**
* 获取从start到末尾的字节数组
* @param buffer
* @param startIndex
* @return
*/
public static byte[] getTailBytes(byte[] buffer,int startIndex){
int len=buffer.length-startIndex;
byte[] newBytes=new byte[len];
System.arraycopy(buffer,startIndex,newBytes,0,len);
return newBytes;
}
/**
* 从byte[]数组指定开始位置获取第一个字节
* todo: start位置可能是大于int类型的数值
+40
View File
@@ -0,0 +1,40 @@
package util;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 11:27 2021/3/10
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class StringHelper {
/**
* 对字符串实施百分号编码
* // @Description:
* // 1. 具体编码的定义参见:RFC 3986 section 2
* // 2. 将除了字母('a'~'z', 'A'~'Z')、数字('0' ~ '9')和指定特殊字符('-', '.', '_', '~'
* // 之外的所有字符都进行百分号编码。
* // 3. 对字符串中的每个UTF-8字符c(用八位一个字节表示)进行如下规则的编码:
* // a. 首先将一个八位字节分为两个部分,hi = (c % 0xf0) >> 4 ; lo = c % 0xf。hi 和 lo 均为两个字节表示的数字,范围为(0~15)
* // b. 然后分别用 hi 和 lo 作为索引,从 "0123456789ABCDEF" 字符串取指定位置的字符,最后编码的结果如下表示:
* // '%' + "0123456789ABCDEF"[hi] + "0123456789ABCDEF"[lo]
* // c. 相当于每个需要编码的字符都编码成了一个%开头的三个字符表示
* @param str
* @return
*/
public static String escape(String str){
return "";
}
/**
* 对字符串实施百分号解码
* @param str
* @return
*/
public static String unescape(String str){
return "";
}
}
@@ -8,6 +8,7 @@ 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;
@@ -18,6 +19,7 @@ import org.mapdb.DBMaker;
import org.mapdb.Serializer;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
/*
@@ -33,7 +35,6 @@ public class TestPersist {
@Test
public void testMapDB() throws Exception {
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
@@ -52,17 +53,115 @@ public class TestPersist {
Common.CertSign, true, System.currentTimeMillis());
CertUtils.signCert(certificate, sm2PrivateKey);
identity.setCert(certificate);
DB db = DBMaker.fileDB("./target/test.db").closeOnJvmShutdown().transactionEnable().make();
Serializer<Identity> customSerializer = new IdentitySerializer();
ConcurrentMap<String, Identity> map = db.hashMap("identity")
.keySerializer(Serializer.STRING)
.valueSerializer(customSerializer)
.createOrOpen();
// map.put("/min/test05", new Identity());
map.put("/min/test06", identity);
db.commit();
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());
Identity id = map.get("/min/test06");
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;
}
}
+13 -3
View File
@@ -19,8 +19,18 @@ public class HumanImpl implements God,Animal {
}
public static void main(String[] args){
HumanImpl h=new HumanImpl();
h.sayHi();
h.makeWorld();
// HumanImpl h=new HumanImpl();
// h.sayHi();
// h.makeWorld();
// String ss="hello";
// System.out.println(ss.length());
// System.out.println(ss.getBytes().length);
String[] i="/1/wefree/a.avi".split("/");
System.out.println("-------------");
for (int j = 0; j < i.length; j++) {
System.out.println(i[j]);
}
System.out.println("-------------");
// System.out.println();
}
}