mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
identity manager (without TEST)
This commit is contained in:
1 parent
16416fecf4
commit
dbeb27abe7
4 files changed
+130
No files matched your search
@@ -166,5 +166,9 @@ public class SM2Base {
|
||||
return new ECPublicKeyParameters(ecPubKey.getQ(), domainParameters);
|
||||
}
|
||||
|
||||
public static SM2KeyPair generateSM2KeyPair(){
|
||||
return SM2KeyPair.generateKeyPair();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package minsecurity.crypto.sm2;
|
||||
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 09:57 2021/03/11
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SM2KeyPair {
|
||||
private final SM2PublicKey sm2PublicKey;
|
||||
private final SM2PrivateKey sm2PrivateKey;
|
||||
|
||||
public SM2KeyPair(SM2PublicKey sm2PublicKey, SM2PrivateKey sm2PrivateKey) {
|
||||
this.sm2PublicKey = sm2PublicKey;
|
||||
this.sm2PrivateKey = sm2PrivateKey;
|
||||
}
|
||||
|
||||
public static SM2KeyPair generateKeyPair(){
|
||||
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();
|
||||
return new SM2KeyPair(new SM2PublicKey(x,y), new SM2PrivateKey(d));
|
||||
}
|
||||
|
||||
|
||||
public SM2PublicKey getSm2PublicKey() {
|
||||
return sm2PublicKey;
|
||||
}
|
||||
|
||||
public SM2PrivateKey getSm2PrivateKey() {
|
||||
return sm2PrivateKey;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ public class MapDB {
|
||||
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;
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package security;
|
||||
|
||||
import minsecurity.Common;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import minsecurity.crypto.sm2.SM2KeyPair;
|
||||
import minsecurity.identity.Identity;
|
||||
import minsecurity.identity.IdentityException;
|
||||
import minsecurity.identity.KeyParam;
|
||||
import minsecurity.identity.persist.MapDB;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 15:45 2021/03/10
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class IdentifyManager {
|
||||
|
||||
private final ConcurrentMap<String, Identity> identifies;
|
||||
private Identity defaultIdentity;
|
||||
private int privateKeyEncryptionAlgorithm;
|
||||
|
||||
public IdentifyManager(){
|
||||
MapDB mapDB = MapDB.getInstance();
|
||||
// TODO: 目前使用浅拷贝, 是否需要修改为深拷贝?
|
||||
this.identifies = mapDB.getIdentityMap();
|
||||
this.defaultIdentity = MapDB.getInstance().getDefaultIdentity();
|
||||
this.privateKeyEncryptionAlgorithm = Common.SM4ECB;
|
||||
}
|
||||
|
||||
|
||||
public Identity getIdentityByName(String name){
|
||||
return MapDB.getInstance().getIdentityByName(name);
|
||||
}
|
||||
|
||||
public boolean deleteIdentityByName(String name, boolean commit){
|
||||
Identity identity = MapDB.getInstance().deleteIdentity(name, commit);
|
||||
return identity != null;
|
||||
}
|
||||
|
||||
public boolean saveIdentity(Identity newIdentity, boolean force, boolean commit){
|
||||
// identifies.con
|
||||
if(identifies.containsKey(newIdentity.getName()) && !force) {
|
||||
return false;
|
||||
}
|
||||
MapDB.getInstance().addIdentity(newIdentity.getName(), newIdentity, commit);
|
||||
return true;
|
||||
}
|
||||
|
||||
public Identity createIdentityByNameAndKeyParam(String name, KeyParam keyParam, String passwd, boolean commit){
|
||||
if(identifies.containsKey(name)){
|
||||
return null;
|
||||
}
|
||||
SM2KeyPair sm2KeyPair = SM2Base.generateSM2KeyPair();
|
||||
Identity newIdentity = new Identity(name, keyParam, sm2KeyPair.getSm2PrivateKey(), null,
|
||||
sm2KeyPair.getSm2PublicKey(), passwd, null,false);
|
||||
if(passwd != null && !passwd.equals("")){
|
||||
try {
|
||||
if(!newIdentity.lock(passwd, Common.SM4ECB))
|
||||
return null;
|
||||
} catch (IdentityException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return MapDB.getInstance().addIdentity(name, newIdentity, commit);
|
||||
}
|
||||
|
||||
public boolean setDefaultIdentity(Identity identity, boolean commit) {
|
||||
if(identity == null)
|
||||
return false;
|
||||
this.defaultIdentity = identity;
|
||||
return MapDB.getInstance().setDefaultIdentity(identity.getName(), commit);
|
||||
}
|
||||
|
||||
public boolean existIdentity(String name){
|
||||
return identifies.containsKey(name);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user