mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
identity & identity基础功能测试
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user