mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 02:30:25 +08:00
使用mapDB持久化identity(50%)
This commit is contained in:
@@ -76,6 +76,14 @@
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.json/json -->
|
||||
|
||||
<!-- mapDB kv存储 -->
|
||||
<!-- https://mvnrepository.com/artifact/org.mapdb/mapdb -->
|
||||
<dependency>
|
||||
<groupId>org.mapdb</groupId>
|
||||
<artifactId>mapdb</artifactId>
|
||||
<version>3.0.8</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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 com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class TestIdentity {
|
||||
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);
|
||||
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,
|
||||
@@ -73,6 +73,8 @@ public class TestIdentity {
|
||||
idFromBytes = Identity.load(bytesOfDump, null);
|
||||
assertEquals(ByteUtils.toHexString(identity.getPrikey().getBytes()), ByteUtils.toHexString(idFromBytes.getPrikey().getBytes()));
|
||||
assertEquals(ByteUtils.toHexString(identity.getPubkey().getBytes()), ByteUtils.toHexString(idFromBytes.getPubkey().getBytes()));
|
||||
|
||||
logger.debug(identity.getPrikey().getBytes().length + " " + identity.getPubkey().getBytes().length);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package minsecurity.identity;
|
||||
|
||||
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.cert.CertUtils;
|
||||
import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import minsecurity.identity.persist.IdentitySerializer;
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.junit.Test;
|
||||
import org.mapdb.BTreeMap;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.Serializer;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:35 2021/03/09
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestPersist {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestIdentity.class);
|
||||
|
||||
@Test
|
||||
public void testMapDB() throws Exception {
|
||||
|
||||
|
||||
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);
|
||||
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();
|
||||
logger.debug(identity.toString());
|
||||
Identity id = map.get("/min/test06");
|
||||
logger.debug(id.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user