mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
使用mapDB持久化identity(50%)
This commit is contained in:
1 parent
9827414ac5
commit
f693a7a120
5 files changed
+194
-4
No files matched your search
@@ -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