mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 00:10:25 +08:00
fix: keyChain init function
This commit is contained in:
+11
-4
@@ -35,7 +35,7 @@ import java.util.concurrent.ConcurrentMap;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
// TODO: 测试
|
||||
public class IdentifyManager {
|
||||
public class IdentityManager {
|
||||
|
||||
public static String DefaultIdentityDBPath = "/usr/local/.mir/identity/";
|
||||
public void setIdentifies(ConcurrentMap<String, Identity> identifies) {
|
||||
@@ -61,11 +61,11 @@ public class IdentifyManager {
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/11
|
||||
**/
|
||||
public IdentifyManager(){
|
||||
public IdentityManager(){
|
||||
init();
|
||||
}
|
||||
|
||||
public IdentifyManager(String dbPath) {
|
||||
public IdentityManager(String dbPath) {
|
||||
try {
|
||||
Sqlite.getInstance().open(dbPath);
|
||||
} catch (Exception e) {
|
||||
@@ -420,7 +420,14 @@ public class IdentifyManager {
|
||||
public Identity getDefaultIdentity() {
|
||||
return defaultIdentity;
|
||||
}
|
||||
|
||||
/**
|
||||
* setter方法!
|
||||
* @param defaultIdentity
|
||||
* @return void
|
||||
* @throws
|
||||
* @author hongyu guo
|
||||
* @date 2021/5/21
|
||||
**/
|
||||
public void setDefaultIdentity(Identity defaultIdentity) {
|
||||
this.defaultIdentity = defaultIdentity;
|
||||
}
|
||||
@@ -13,11 +13,9 @@ import packet.MINPacket;
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.IdentityHashMap;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
@@ -27,24 +25,9 @@ import java.util.IdentityHashMap;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class KeyChain {
|
||||
private static final String defaultIdentifyName = "/localhost/operator";
|
||||
public IdentifyManager getIdentifyManager() {
|
||||
return identifyManager;
|
||||
}
|
||||
private static final String defaultIdentityName = "/localhost/operator";
|
||||
|
||||
public void setIdentifyManager(IdentifyManager identifyManager) {
|
||||
this.identifyManager = identifyManager;
|
||||
}
|
||||
|
||||
public Identity getCurrentIdentity() {
|
||||
return currentIdentity;
|
||||
}
|
||||
|
||||
public void setCurrentIdentity(Identity currentIdentity) {
|
||||
this.currentIdentity = currentIdentity;
|
||||
}
|
||||
|
||||
private IdentifyManager identifyManager;
|
||||
private IdentityManager identityManager;
|
||||
private Identity currentIdentity;
|
||||
|
||||
/**
|
||||
@@ -55,13 +38,40 @@ public class KeyChain {
|
||||
* @date 2021/3/11
|
||||
**/
|
||||
public KeyChain() throws Exception{
|
||||
identifyManager = new IdentifyManager();
|
||||
currentIdentity = identifyManager.getDefaultIdentity();
|
||||
// DONE: 考虑是否需要在没有默认身份的时候创建一个缺省的本地网络身份
|
||||
if (currentIdentity == null){
|
||||
Identity newId = this.identifyManager.createIdentityByName(defaultIdentifyName, "", true);
|
||||
this.identifyManager.setDefaultIdentity(newId);
|
||||
this.currentIdentity = newId;
|
||||
// identifyManager = new IdentifyManager();
|
||||
// currentIdentity = identifyManager.getDefaultIdentity();
|
||||
// // DONE: 考虑是否需要在没有默认身份的时候创建一个缺省的本地网络身份
|
||||
// if (currentIdentity == null){
|
||||
// Identity newId = this.identifyManager.createIdentityByName(defaultIdentifyName, "", true);
|
||||
// this.identifyManager.setDefaultIdentity(newId);
|
||||
// this.currentIdentity = newId;
|
||||
// }
|
||||
init(IdentityManager.DefaultIdentityDBPath);
|
||||
}
|
||||
|
||||
public KeyChain(String dbPath) throws Exception {
|
||||
init(dbPath);
|
||||
}
|
||||
|
||||
private void init(String dbPath) throws Exception {
|
||||
identityManager = new IdentityManager(dbPath);
|
||||
currentIdentity = identityManager.getDefaultIdentity();
|
||||
if(identityManager.getDefaultIdentity() == null) {
|
||||
Identity defaultIdentity = identityManager.getIdentityByName(defaultIdentityName);
|
||||
if(defaultIdentity != null) {
|
||||
currentIdentity = defaultIdentity;
|
||||
return;
|
||||
}
|
||||
|
||||
Identity newIdentity = identityManager.createIdentityByName(defaultIdentityName, "", true);
|
||||
if(newIdentity == null) {
|
||||
throw new KeyChainException("init failed: can not create identity [" + defaultIdentityName + "]");
|
||||
}
|
||||
boolean succ = identityManager.setDefaultIdentity(newIdentity, true);
|
||||
if(!succ) {
|
||||
throw new KeyChainException("init failed: can not set default identity [" + newIdentity.getName() + "]");
|
||||
}
|
||||
currentIdentity = newIdentity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,23 +85,15 @@ public class KeyChain {
|
||||
**/
|
||||
public void setCurrentIdentity(Identity identity, String passwd) {
|
||||
try {
|
||||
if(passwd != null && !passwd.equals("")){
|
||||
identity.unLock(passwd, identifyManager.getPrivateKeyEncryptionAlgorithm());
|
||||
currentIdentity = identity;
|
||||
if(!"".equals(passwd) && identity.isLocked()){
|
||||
boolean success = identity.unLock(passwd, identityManager.getPrivateKeyEncryptionAlgorithm());
|
||||
if(!success) {
|
||||
throw new KeyChainException("Unlock " + identity.getName() + " by " + passwd + " failed!!");
|
||||
}
|
||||
}
|
||||
} catch (IdentityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchPaddingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchProviderException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
currentIdentity = identity;
|
||||
} catch (IdentityException | NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException
|
||||
| BadPaddingException | NoSuchProviderException | IllegalBlockSizeException | KeyChainException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -225,7 +227,7 @@ public class KeyChain {
|
||||
Signature signature = minPacket.signatureField.getSignature(0);
|
||||
|
||||
String identityName = signature.getSigInfo().getKeyLocator().getIdentifier().toUri();
|
||||
Identity identity = this.identifyManager.getIdentityByName(identityName);
|
||||
Identity identity = this.identityManager.getIdentityByName(identityName);
|
||||
|
||||
if (identity == null){
|
||||
throw new KeyChainException("Verify failed, could not find the identity");
|
||||
@@ -289,13 +291,29 @@ public class KeyChain {
|
||||
*/
|
||||
public void importSafeBag(SafeBag safeBag, String passwd, boolean force) throws Exception{
|
||||
if (safeBag == null)
|
||||
throw new KeyChainException(String.format("SafeBag is %s", safeBag));
|
||||
throw new KeyChainException("SafeBag is null");
|
||||
Identity identity = Identity.load(safeBag.getValue(), passwd);
|
||||
|
||||
if (!this.identifyManager.existIdentity(identity.getName()) || force){
|
||||
this.identifyManager.saveIdentity(identity, force, false);
|
||||
if (!this.identityManager.existIdentity(identity.getName()) || force){
|
||||
this.identityManager.saveIdentity(identity, force, false);
|
||||
}else {
|
||||
throw new KeyChainException(String.format("Identify %s is already exists!", identity.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
public IdentityManager getIdentifyManager() {
|
||||
return identityManager;
|
||||
}
|
||||
|
||||
public void setIdentifyManager(IdentityManager identityManager) {
|
||||
this.identityManager = identityManager;
|
||||
}
|
||||
|
||||
public Identity getCurrentIdentity() {
|
||||
return currentIdentity;
|
||||
}
|
||||
|
||||
// public void setCurrentIdentity(Identity currentIdentity) {
|
||||
// this.currentIdentity = currentIdentity;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import minsecurity.certificate.cert.CertUtils;
|
||||
import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.sm2.SM2KeyPair;
|
||||
import minsecurity.identity.Identity;
|
||||
import minsecurity.identity.IdentityBenchmark;
|
||||
import minsecurity.identity.KeyParam;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
@@ -61,19 +60,19 @@ public class IdentifyManagerBenchmark {
|
||||
|
||||
@Benchmark
|
||||
public void testIdentifyManagerInit(){
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testGetIdentityByName(){
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
String name = "wzq0.6597381351293033"; // 预先存储好的身份名
|
||||
manager.getIdentityByName(name);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testSaveAndDelete() throws Exception {
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
Identity id = createRandomIdentity();
|
||||
manager.saveIdentity(id, true, true);
|
||||
manager.deleteIdentityByName(id.getName(), true);
|
||||
@@ -81,14 +80,14 @@ public class IdentifyManagerBenchmark {
|
||||
|
||||
@Benchmark
|
||||
public void testSetDefault() throws Exception {
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
String name = "wzq0.6597381351293033"; // 预先存储好的身份名
|
||||
manager.setDefaultIdentity(manager.getIdentifies().get(name), true);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testExistIdentity(){
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
String name = "wzq0.6597381351293033"; // 预先存储好的身份名
|
||||
manager.existIdentity(name);
|
||||
manager.existIdentity(name + "test");
|
||||
@@ -96,7 +95,7 @@ public class IdentifyManagerBenchmark {
|
||||
|
||||
@Benchmark
|
||||
public void testCreateIdentityByNameAndDelete() throws Exception {
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
String s = "wzq"+Math.random();
|
||||
manager.createIdentityByName(s, "1234", true);
|
||||
manager.deleteIdentityByName(s, true);
|
||||
|
||||
+16
-19
@@ -7,16 +7,13 @@ import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.sm2.SM2KeyPair;
|
||||
import minsecurity.identity.Identity;
|
||||
import minsecurity.identity.KeyParam;
|
||||
import minsecurity.identity.TestIdentity;
|
||||
import minsecurity.identity.persist.sqlite.Sqlite;
|
||||
import org.junit.Test;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public class IdentifyManagerTest {
|
||||
public class IdentityManagerTest {
|
||||
// private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestIdentity.class);
|
||||
/**
|
||||
* 随机生成身份数据
|
||||
@@ -59,7 +56,7 @@ public class IdentifyManagerTest {
|
||||
try{
|
||||
// 打开数据库
|
||||
// Sqlite.getInstance().openDefault();
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("默认身份:%s", manager.getDefaultIdentity().getName()));
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
LoggerHelper.info(String.format("私钥加密算法:%d", manager.getPrivateKeyEncryptionAlgorithm()));
|
||||
@@ -71,7 +68,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testGetIdentityByName(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
|
||||
Set<String> keySet = hashMap.keySet();
|
||||
// 查询所有manager中的用户
|
||||
@@ -87,7 +84,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testGetIdentityByName2(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
|
||||
Set<String> keySet = hashMap.keySet();
|
||||
@@ -109,7 +106,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testDeleteIdentityByName2(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
|
||||
Set<String> keySet = hashMap.keySet();
|
||||
@@ -131,7 +128,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testDeleteIdentityByName(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
|
||||
Set<String> keySet = hashMap.keySet();
|
||||
@@ -150,7 +147,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testSaveIdentity(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
// 随机生成身份并保存 重复五次
|
||||
for (int i = 0; i < 5; i++) {
|
||||
@@ -167,7 +164,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testSaveIdentity2(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
// 插入 null 重复五次
|
||||
for (int i = 0; i < 5; i++) {
|
||||
@@ -183,7 +180,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testCreateIdentityByNameAndKeyParam(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
// 随机生成名称并保存 重复五次
|
||||
for (int i = 0; i < 5; i++) {
|
||||
@@ -203,7 +200,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testCreateIdentityByName(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
// 随机生成名称并保存 重复五次
|
||||
for (int i = 0; i < 5; i++) {
|
||||
@@ -219,7 +216,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testCreateIdentityByName2(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
// 保存null 重复五次
|
||||
for (int i = 0; i < 5; i++) {
|
||||
@@ -236,7 +233,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testCreateIdentityByNameAndKeyParam2(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
// 保存null 重复五次
|
||||
for (int i = 0; i < 5; i++) {
|
||||
@@ -253,7 +250,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testSetDefaultIdentity(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
|
||||
Set<String> keySet = hashMap.keySet();
|
||||
@@ -272,7 +269,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testSetDefaultIdentity2(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
|
||||
Set<String> keySet = hashMap.keySet();
|
||||
@@ -294,7 +291,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testExistIdentity(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
|
||||
Set<String> keySet = hashMap.keySet();
|
||||
@@ -314,7 +311,7 @@ public class IdentifyManagerTest {
|
||||
@Test
|
||||
public void testExistIdentity2(){
|
||||
try{
|
||||
IdentifyManager manager = new IdentifyManager();
|
||||
IdentityManager manager = new IdentityManager();
|
||||
LoggerHelper.info(String.format("身份数量:%d", manager.getIdentifies().size()));
|
||||
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
|
||||
Set<String> keySet = hashMap.keySet();
|
||||
Reference in New Issue
Block a user