mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
01ed6a2e56
50 files changed
+2581
-212
No files matched your search
@@ -1,9 +1,9 @@
|
||||
package minsecurity.certificate.cert;
|
||||
|
||||
import minsecurity.Common;
|
||||
import minsecurity.crypto.sm2.Sm2Base;
|
||||
import minsecurity.crypto.sm2.Sm2PrivateKey;
|
||||
import minsecurity.crypto.sm2.Sm2PublicKey;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
@@ -22,15 +22,15 @@ public class TestCert {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestCert.class);
|
||||
@Test
|
||||
public void testCertSignAndVerify() throws Exception {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
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();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey(d);
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey(x,y);
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
|
||||
|
||||
Certificate certificate = new Certificate(1,1,sm2PublicKey,
|
||||
@@ -46,15 +46,15 @@ public class TestCert {
|
||||
assertTrue(verify);
|
||||
|
||||
|
||||
AsymmetricCipherKeyPair keyPair2 = Sm2Base.generateKeyPairParameter();
|
||||
AsymmetricCipherKeyPair keyPair2 = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey2 = (ECPrivateKeyParameters) keyPair2.getPrivate();
|
||||
ECPublicKeyParameters pubKey2 = (ECPublicKeyParameters) keyPair2.getPublic();
|
||||
byte[] d2 = priKey2.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x2 = pubKey2.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y2 = pubKey2.getQ().getAffineYCoord().getEncoded();
|
||||
Sm2PrivateKey sm2PrivateKey2 = new Sm2PrivateKey(d2);
|
||||
Sm2PublicKey sm2PublicKey2 = new Sm2PublicKey(x2, y2);
|
||||
SM2PrivateKey sm2PrivateKey2 = new SM2PrivateKey(d2);
|
||||
SM2PublicKey sm2PublicKey2 = new SM2PublicKey(x2, y2);
|
||||
|
||||
Certificate sub = new Certificate(1,2,sm2PublicKey2,
|
||||
null, Common.SM3withSM2,Common.SM2,
|
||||
@@ -69,15 +69,15 @@ public class TestCert {
|
||||
|
||||
@Test
|
||||
public void testCert2Pem() throws Exception {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
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();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey(d);
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey(x,y);
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
|
||||
|
||||
Certificate certificate = new Certificate(1,1,sm2PublicKey,
|
||||
|
||||
@@ -3,7 +3,7 @@ package minsecurity.certificate.x509;
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.X509.X509CertMaker;
|
||||
import minsecurity.certificate.X509.X509CertUtil;
|
||||
import minsecurity.crypto.sm2.Sm2Base;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.bouncycastle.util.io.pem.PemObject;
|
||||
@@ -50,7 +50,7 @@ public class TestX509 {
|
||||
public void testRebuildKeyPair(){
|
||||
try {
|
||||
|
||||
KeyPair rootKP = Sm2Base.generateKeyPair();
|
||||
KeyPair rootKP = SM2Base.generateKeyPair();
|
||||
X509Certificate rootCA = makeRootCA(rootKP,"CN","MIN",
|
||||
"MIN-GROUP","MIN ROOT CA",
|
||||
365L * 24 * 60 * 60 * 1000, "SM3withSM2");
|
||||
@@ -77,7 +77,7 @@ public class TestX509 {
|
||||
logger.debug("origin: {}", ByteUtils.toHexString(rootKP.getPublic().getEncoded()));
|
||||
logger.debug("rebuild:{}", ByteUtils.toHexString(rootKeyPair.getPublic().getEncoded()));
|
||||
|
||||
KeyPair subKeyPair = Sm2Base.generateKeyPair();
|
||||
KeyPair subKeyPair = SM2Base.generateKeyPair();
|
||||
X509Certificate subCert =
|
||||
X509CertMaker.makeSubCACert(rootKeyPair,
|
||||
cert,
|
||||
|
||||
+14
-14
@@ -1,8 +1,8 @@
|
||||
package minsecurity.crypto;
|
||||
|
||||
import minsecurity.crypto.sm2.Sm2Base;
|
||||
import minsecurity.crypto.sm2.Sm2PrivateKey;
|
||||
import minsecurity.crypto.sm2.Sm2PublicKey;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
@@ -24,11 +24,11 @@ import java.util.List;
|
||||
* @Date: 20:15 2021/03/03
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestSm2 {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestSm2.class);
|
||||
public class TestSM2 {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestSM2.class);
|
||||
@Test
|
||||
public void testSetGet() throws InvalidCipherTextException {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
@@ -36,8 +36,8 @@ public class TestSm2 {
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
logger.debug("d.len = {}, x.len = {}, y.len = {}",d.length, x.length, y.length);
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey();
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey();
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey();
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey();
|
||||
|
||||
sm2PrivateKey.setBytes(d);
|
||||
|
||||
@@ -62,7 +62,7 @@ public class TestSm2 {
|
||||
|
||||
@Test
|
||||
public void testEncAndDec() throws InvalidCipherTextException {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
@@ -71,8 +71,8 @@ public class TestSm2 {
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
logger.debug("d.len = {}, x.len = {}, y.len = {}",d.length, x.length, y.length);
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey(d);
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey(x,y);
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
|
||||
String content = "hello world";
|
||||
byte[] encBytes = sm2PublicKey.encrypt(content.getBytes());
|
||||
@@ -83,7 +83,7 @@ public class TestSm2 {
|
||||
|
||||
@Test
|
||||
public void testSignAndVerify() throws CryptoException {
|
||||
AsymmetricCipherKeyPair keyPair = Sm2Base.generateKeyPairParameter();
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
@@ -93,8 +93,8 @@ public class TestSm2 {
|
||||
logger.debug("d.len = {}, x.len = {}, y.len = {}",d.length, x.length, y.length);
|
||||
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
Sm2PrivateKey sm2PrivateKey = new Sm2PrivateKey(d);
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey(x,y);
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
String content = "7b227462734365727469666963617465223a7b2276657273696f6e223a312c2273657269616c4e756d626572223a312c227075626c69634b6579223a22424a304d7047714e5958706b695879575a4d6e5838776d6b694a51326f576d37336257615932525473706b695058342f37705443666332764e7272596e766c6a644d3448647850537531753272616363506c74485245593d222c227369676e6174757265416c676f726974686d223a302c227075626c69634b6579416c676f726974686d223a302c226973737565546f223a226973737565546f222c22697373756572223a22697373756572222c226e6f744265666f7265223a313631353032303130393339352c226e6f744166746572223a313631353032303130393539352c226b65795573616765223a322c2274696d657374616d70223a313631353032303130393339352c226361223a747275657d2c227369676e6174757265416c676f726974686d223a302c227369676e617475726556616c7565223a6e756c6c7d";
|
||||
byte[] digest = sm2PrivateKey.sign(ByteUtils.fromHexString(content));
|
||||
boolean flag = sm2PublicKey.verify(ByteUtils.fromHexString(content), digest);
|
||||
+11
-12
@@ -2,7 +2,6 @@ package minsecurity.crypto;
|
||||
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -14,7 +13,7 @@ import java.util.Arrays;
|
||||
* @Date: 16:28 2021/03/04
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestSm4 {
|
||||
public class TestSM4 {
|
||||
public static final byte[] SRC_DATA = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
public static final byte[] SRC_DATA_16B = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
|
||||
public static final byte[] SRC_DATA_24B = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
|
||||
@@ -25,42 +24,42 @@ public class TestSm4 {
|
||||
@Test
|
||||
public void testEncryptAndDecrypt() {
|
||||
try {
|
||||
byte[] key = Sm4.generateKey();
|
||||
byte[] iv = Sm4.generateKey();
|
||||
byte[] key = SM4.generateKey();
|
||||
byte[] iv = SM4.generateKey();
|
||||
byte[] cipherText = null;
|
||||
byte[] decryptedData = null;
|
||||
System.out.println("key: " + ByteUtils.toHexString(key));
|
||||
System.out.println("iv: " + ByteUtils.toHexString(iv));
|
||||
|
||||
cipherText = Sm4.encrypt_ECB_NoPadding(key, SRC_DATA_16B);
|
||||
cipherText = SM4.encrypt_ECB_NoPadding(key, SRC_DATA_16B);
|
||||
System.out.println("SM4 ECB NoPadding encrypt result:\n" + Arrays.toString(cipherText));
|
||||
System.out.println(ByteUtils.toHexString(cipherText));
|
||||
decryptedData = Sm4.decrypt_ECB_NoPadding(key, cipherText);
|
||||
decryptedData = SM4.decrypt_ECB_NoPadding(key, cipherText);
|
||||
System.out.println("SM4 ECB NoPadding decrypt result:\n" + Arrays.toString(decryptedData));
|
||||
if (!Arrays.equals(decryptedData, SRC_DATA_16B)) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
cipherText = Sm4.encrypt_ECB_Padding(key, SRC_DATA_16B);
|
||||
cipherText = SM4.encrypt_ECB_Padding(key, SRC_DATA_16B);
|
||||
System.out.println("SM4 ECB Padding encrypt result:\n" + Arrays.toString(cipherText));
|
||||
System.out.println(ByteUtils.toHexString(cipherText));
|
||||
decryptedData = Sm4.decrypt_ECB_Padding(key, cipherText);
|
||||
decryptedData = SM4.decrypt_ECB_Padding(key, cipherText);
|
||||
System.out.println("SM4 ECB Padding decrypt result:\n" + Arrays.toString(decryptedData));
|
||||
if (!Arrays.equals(decryptedData, SRC_DATA_16B)) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
cipherText = Sm4.encrypt_CBC_Padding(key, iv, SRC_DATA);
|
||||
cipherText = SM4.encrypt_CBC_Padding(key, iv, SRC_DATA);
|
||||
System.out.println("SM4 CBC Padding encrypt result:\n" + Arrays.toString(cipherText));
|
||||
decryptedData = Sm4.decrypt_CBC_Padding(key, iv, cipherText);
|
||||
decryptedData = SM4.decrypt_CBC_Padding(key, iv, cipherText);
|
||||
System.out.println("SM4 CBC Padding decrypt result:\n" + Arrays.toString(decryptedData));
|
||||
if (!Arrays.equals(decryptedData, SRC_DATA)) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
cipherText = Sm4.encrypt_CBC_NoPadding(key, iv, SRC_DATA_16B);
|
||||
cipherText = SM4.encrypt_CBC_NoPadding(key, iv, SRC_DATA_16B);
|
||||
System.out.println("SM4 CBC NoPadding encrypt result:\n" + Arrays.toString(cipherText));
|
||||
decryptedData = Sm4.decrypt_CBC_NoPadding(key, iv, cipherText);
|
||||
decryptedData = SM4.decrypt_CBC_NoPadding(key, iv, cipherText);
|
||||
System.out.println("SM4 CBC NoPadding decrypt result:\n" + Arrays.toString(decryptedData));
|
||||
if (!Arrays.equals(decryptedData, SRC_DATA_16B)) {
|
||||
Assert.fail();
|
||||
@@ -0,0 +1,81 @@
|
||||
package minsecurity.identity;
|
||||
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.cert.CertUtils;
|
||||
import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.TestSM2;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 15:24 2021/03/09
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestIdentity {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestIdentity.class);
|
||||
|
||||
|
||||
@Test
|
||||
public void testIdentity1() 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();
|
||||
logger.debug("d.len = {}, x.len = {}, y.len = {}",d.length, x.length, y.length);
|
||||
|
||||
// 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);
|
||||
|
||||
String text = "2020-03-09";
|
||||
// test sign and verify
|
||||
byte[] signature = identity.sign(text.getBytes());
|
||||
boolean flag = identity.verify(text.getBytes(), signature);
|
||||
logger.debug("identity verify: {}", flag);
|
||||
assertTrue(flag);
|
||||
|
||||
|
||||
// test enc and dec
|
||||
byte[] cipher = identity.encrypt(text.getBytes());
|
||||
byte[] bytesOfDec = identity.decrypt(cipher);
|
||||
assertArrayEquals(bytesOfDec, text.getBytes());
|
||||
|
||||
|
||||
// test dump and load
|
||||
byte[] bytesOfDump = identity.dump(identity.getPasswd());
|
||||
Identity idFromBytes = Identity.load(bytesOfDump, identity.getPasswd());
|
||||
assertEquals(ByteUtils.toHexString(identity.getPrikey().getBytes()), ByteUtils.toHexString(idFromBytes.getPrikey().getBytes()));
|
||||
assertEquals(ByteUtils.toHexString(identity.getPubkey().getBytes()), ByteUtils.toHexString(idFromBytes.getPubkey().getBytes()));
|
||||
// test dump and load without passwd
|
||||
bytesOfDump = identity.dump(null);
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package others;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 9:15 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
interface Animal {
|
||||
// private String name="灵长类动物";
|
||||
|
||||
|
||||
public void sayHi();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package others;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 9:15 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
interface God {
|
||||
|
||||
public void makeWorld();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package others;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 10:10 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class HumanImpl implements God,Animal {
|
||||
@Override
|
||||
public void sayHi() {
|
||||
System.out.println("hi");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeWorld() {
|
||||
System.out.println("world");
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
HumanImpl h=new HumanImpl();
|
||||
h.sayHi();
|
||||
h.makeWorld();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user