mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
Merge branch 'master' of gitee.com:willfree/min-dev-java
This commit is contained in:
commit
424fe59908
8 files changed
+866
-13
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user