Merge branch 'master' of gitee.com:willfree/min-dev-java

This commit is contained in:
free will
2021-04-16 13:28:15 +08:00
8 changed files with 523 additions and 3 deletions
+12
View File
@@ -106,6 +106,18 @@
<artifactId>sqlite-jdbc</artifactId>
<version>3.35.4</version>
</dependency>
<!--jmh 基准测试 -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.23</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.23</version>
<scope>provided</scope>
</dependency>
</dependencies>
@@ -83,8 +83,7 @@ public class Sqlite {
// 兼容sqlcipher
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
stmt = c.createStatement();
int res = stmt.executeUpdate(table_create);
System.out.println(res);
stmt.executeUpdate(table_create);
stmt.close();
c.close();
}catch (Exception ex){
@@ -0,0 +1,107 @@
package minsecurity.identity;
import com.fasterxml.jackson.core.JsonProcessingException;
import minsecurity.Common;
import minsecurity.certificate.cert.CertException;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.AsymKeyException;
import minsecurity.crypto.sm2.SM2Base;
import minsecurity.crypto.sm2.SM2PrivateKey;
import minsecurity.crypto.sm2.SM2PublicKey;
import minsecurity.identity.sqlite.SqliteBenchmark;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertArrayEquals;
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class IdentityBenchmark {
private Identity createIdentity() 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,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);
return identity;
}
@Benchmark
public void testSignAndVerify() throws Exception {
Identity id = createIdentity();
String text = "2020-03-09";
// test sign and verify
byte[] signature = id.sign(text.getBytes());
boolean flag = id.verify(text.getBytes(), signature);
}
@Benchmark
public void testEncAndDec() throws Exception {
Identity id = createIdentity();
String text = "2020-03-09";
// test enc and dec
byte[] cipher = id.encrypt(text.getBytes());
byte[] bytesOfDec = id.decrypt(cipher);
// assertArrayEquals(bytesOfDec, text.getBytes());
}
@Benchmark
public void testDumpAndLoad() throws Exception {
Identity id = createIdentity();
// test dump and load
byte[] bytesOfDump = id.dump(id.getPasswd());
Identity idFromBytes = Identity.load(bytesOfDump, id.getPasswd());
}
@Benchmark
public void testDumpAndLoad2() throws Exception {
Identity id = createIdentity();
// test dump and load without passwd
byte[] bytesOfDump = id.dump(null);
Identity idFromBytes = Identity.load(bytesOfDump, null);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(IdentityBenchmark.class.getSimpleName())
.build();
new Runner(opt).run();
}
}
@@ -0,0 +1,79 @@
package minsecurity.identity;
import minsecurity.Common;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.sm2.SM2KeyPair;
import minsecurity.identity.persist.Persist;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class PersistBenchmark {
/**
* 随机生成身份数据
* @return Identity
*/
private Identity createRandomIdentity() throws Exception{
// 测试PersistIdentity
SM2KeyPair pair = SM2KeyPair.generateKeyPair();
Identity identity = new Identity();
identity.setName("wzq"+Math.random());
KeyParam keyParam = new KeyParam();
keyParam.PublicKeyAlgorithm = 0;
keyParam.SignatureAlgorithm = 0;
identity.setKeyParam(keyParam);
identity.setPrikey(pair.getSm2PrivateKey());
identity.setPubkey(pair.getSm2PublicKey());
identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
// identity.lock("0123456789abcdef", Common.SM4ECB);
Certificate cert = new Certificate();
cert.setVersion(1);
cert.setSerialNumber(1);
cert.setPublicKey(pair.getSm2PublicKey());
cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3?
cert.setPublicKeyAlgorithm(Common.SM2);
cert.setIssueTo("root");
cert.setIssuer("root");
long timestamp = System.currentTimeMillis() / 1000;
cert.setTimestamp(timestamp); // 10bit timestamp
cert.setNotAfter(timestamp); // 10bit timestamp
cert.setNotBefore(timestamp + 1000); // 10bit timestamp
cert.setKeyUsage(Common.CertSign);
cert.setCA(true);
CertUtils.signCert(cert, pair.getSm2PrivateKey());
identity.setCert(cert);
return identity;
}
@Benchmark
public void testPersistAndDeleteIdentity() throws Exception {
Identity id = createRandomIdentity();
Persist.persistIdentity(id);
Persist.deleteIdentityByNameFromStorage(id.getName());
}
@Benchmark
public void testSetIdentity() throws Exception {
String name = "wzq0.6597381351293033"; // 预先存储好的身份名
Persist.setDefaultIdentityByNameInStorage(name);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(PersistBenchmark.class.getSimpleName())
.build();
new Runner(opt).run();
}
}
@@ -52,7 +52,7 @@ public class TestPersist {
identity.setPrikey(pair.getSm2PrivateKey());
identity.setPubkey(pair.getSm2PublicKey());
identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
identity.lock("0123456789abcdef", Common.SM4ECB);
// identity.lock("0123456789abcdef", Common.SM4ECB);
Certificate cert = new Certificate();
cert.setVersion(1);
cert.setSerialNumber(1);
@@ -0,0 +1,34 @@
package minsecurity.identity.sqlite;
import minsecurity.identity.persist.sqlite.Sqlite;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class SqliteBenchmark {
/**
* Sqlite基准测试
* @throws Exception
*/
@Benchmark
public void testOpenDefault() throws Exception {
Sqlite.getInstance().openDefault();
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(SqliteBenchmark.class.getSimpleName())
.build();
new Runner(opt).run();
}
}
@@ -0,0 +1,111 @@
package security;
import minsecurity.Common;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.sm2.SM2KeyPair;
import minsecurity.identity.Identity;
import minsecurity.identity.IdentityBenchmark;
import minsecurity.identity.KeyParam;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class IdentifyManagerBenchmark {
/**
* 随机生成身份数据
* @return Identity
*/
private Identity createRandomIdentity() throws Exception{
// 测试PersistIdentity
SM2KeyPair pair = SM2KeyPair.generateKeyPair();
Identity identity = new Identity();
identity.setName("wzq"+Math.random());
KeyParam keyParam = new KeyParam();
keyParam.PublicKeyAlgorithm = 0;
keyParam.SignatureAlgorithm = 0;
identity.setKeyParam(keyParam);
identity.setPrikey(pair.getSm2PrivateKey());
identity.setPubkey(pair.getSm2PublicKey());
identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
// identity.lock("0123456789abcdef", Common.SM4ECB);
Certificate cert = new Certificate();
cert.setVersion(1);
cert.setSerialNumber(1);
cert.setPublicKey(pair.getSm2PublicKey());
cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3?
cert.setPublicKeyAlgorithm(Common.SM2);
cert.setIssueTo("root");
cert.setIssuer("root");
long timestamp = System.currentTimeMillis() / 1000;
cert.setTimestamp(timestamp); // 10bit timestamp
cert.setNotAfter(timestamp); // 10bit timestamp
cert.setNotBefore(timestamp + 1000); // 10bit timestamp
cert.setKeyUsage(Common.CertSign);
cert.setCA(true);
CertUtils.signCert(cert, pair.getSm2PrivateKey());
identity.setCert(cert);
return identity;
}
@Benchmark
public void testIdentifyManagerInit(){
IdentifyManager manager = new IdentifyManager();
}
@Benchmark
public void testGetIdentityByName(){
IdentifyManager manager = new IdentifyManager();
String name = "wzq0.6597381351293033"; // 预先存储好的身份名
manager.getIdentityByName(name);
}
@Benchmark
public void testSaveAndDelete() throws Exception {
IdentifyManager manager = new IdentifyManager();
Identity id = createRandomIdentity();
manager.saveIdentity(id, true, true);
manager.deleteIdentityByName(id.getName(), true);
}
@Benchmark
public void testSetDefault() throws Exception {
IdentifyManager manager = new IdentifyManager();
String name = "wzq0.6597381351293033"; // 预先存储好的身份名
manager.setDefaultIdentity(manager.getIdentifies().get(name), true);
}
@Benchmark
public void testExistIdentity(){
IdentifyManager manager = new IdentifyManager();
String name = "wzq0.6597381351293033"; // 预先存储好的身份名
manager.existIdentity(name);
manager.existIdentity(name + "test");
}
@Benchmark
public void testCreateIdentityByNameAndDelete() throws Exception {
IdentifyManager manager = new IdentifyManager();
String s = "wzq"+Math.random();
manager.createIdentityByName(s, "1234", true);
manager.deleteIdentityByName(s, true);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(IdentifyManagerBenchmark.class.getSimpleName())
.build();
new Runner(opt).run();
}
}
@@ -0,0 +1,178 @@
package security;
import component.Identifier;
import component.TTL;
import encoding.VlInt;
import minsecurity.Common;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.sm2.SM2KeyPair;
import minsecurity.identity.Identity;
import minsecurity.identity.KeyParam;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import packet.CPacket;
import packet.Data;
import packet.Interest;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class KeychainBenchmark {
/**
* 随机生成身份数据
* @return Identity
*/
private Identity createRandomIdentity() throws Exception{
// 测试PersistIdentity
SM2KeyPair pair = SM2KeyPair.generateKeyPair();
Identity identity = new Identity();
identity.setName("wzq"+Math.random());
KeyParam keyParam = new KeyParam();
keyParam.PublicKeyAlgorithm = 0;
keyParam.SignatureAlgorithm = 0;
identity.setKeyParam(keyParam);
identity.setPrikey(pair.getSm2PrivateKey());
identity.setPubkey(pair.getSm2PublicKey());
identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
// identity.lock("0123456789abcdef", Common.SM4ECB);
Certificate cert = new Certificate();
cert.setVersion(1);
cert.setSerialNumber(1);
cert.setPublicKey(pair.getSm2PublicKey());
cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3?
cert.setPublicKeyAlgorithm(Common.SM2);
cert.setIssueTo("root");
cert.setIssuer("root");
long timestamp = System.currentTimeMillis() / 1000;
cert.setTimestamp(timestamp); // 10bit timestamp
cert.setNotAfter(timestamp); // 10bit timestamp
cert.setNotBefore(timestamp + 1000); // 10bit timestamp
cert.setKeyUsage(Common.CertSign);
cert.setCA(true);
CertUtils.signCert(cert, pair.getSm2PrivateKey());
identity.setCert(cert);
return identity;
}
@Benchmark
public void testCreateKeyChain() throws Exception {
KeyChain keyChain = new KeyChain();
}
@Benchmark
public void testInitialKeyChain() throws Exception {
// 测试设置新身份为当前身份
KeyChain keyChain = new KeyChain();
// Identity id = createRandomIdentity();
// keyChain.getIdentifyManager().setDefaultIdentity(id, true);
// 输入密码用于解锁身份
// keyChain.setCurrentIdentity(keyChain.getIdentifyManager().getDefaultIdentity(), "0123456789abcdef");
}
@Benchmark
public void testSignAndVerifyInterest() throws Exception {
// 测试设置新身份为当前身份
KeyChain keyChain = new KeyChain();
// Identity id = createRandomIdentity();
// keyChain.getIdentifyManager().setDefaultIdentity(id, true);
// 输入密码用于解锁身份
// keyChain.setCurrentIdentity(keyChain.getIdentifyManager().getDefaultIdentity(), "0123456789abcdef");
// 身份允许签名与验证签名
Identifier id1 = new Identifier("/min/pku/sz");
Identifier id2 = new Identifier("/install");
//////////////////// 构建兴趣包 ////////////////////////
Interest interest = new Interest();
interest.minPacket.packetType = new VlInt(3);
interest.setName(id1);
interest.canBePrefix.setCanBePrefix(true);
interest.mustBeRefresh.setMustBeRefresh(true);
interest.nonce.setNonce(1234);
interest.hopLimit.setHopLimit(1234);
interest.congestionMark.setCongestionLevel(1234);
interest.ttl.setTtl(1234);
interest.payload.setValue(new byte[]{1, 2, 3, 4, 5});
// 正常测试签名与验签
keyChain.signInterest(interest);
keyChain.verifyInterest(interest);
}
@Benchmark
public void testSignAndVerifyCPacket() throws Exception {
// 测试设置新身份为当前身份
KeyChain keyChain = new KeyChain();
// Identity id = createRandomIdentity();
// keyChain.getIdentifyManager().setDefaultIdentity(id, true);
// 输入密码用于解锁身份
// keyChain.setCurrentIdentity(keyChain.getIdentifyManager().getDefaultIdentity(), "0123456789abcdef");
// 身份允许签名与验证签名
Identifier id1 = new Identifier("/min/pku/sz");
Identifier id2 = new Identifier("/install");
CPacket cPacket = new CPacket();
cPacket.minPacket.packetType = new VlInt(3);
cPacket.setSrcIdentifier(id1);
cPacket.setDstIdentifier(id2);
cPacket.setTtl(new TTL(16546418374324163L));
cPacket.payload.setValue(new byte[]{1, 2, 3, 4, 6});
// 正常测试签名与验签
keyChain.signCPacket(cPacket);
keyChain.verifyCPacket(cPacket);
}
@Benchmark
public void testSignAndVerifyData() throws Exception {
// 测试设置新身份为当前身份
KeyChain keyChain = new KeyChain();
// Identity id = createRandomIdentity();
// keyChain.getIdentifyManager().setDefaultIdentity(id, true);
// 输入密码用于解锁身份
// keyChain.setCurrentIdentity(keyChain.getIdentifyManager().getDefaultIdentity(), "0123456789abcdef");
//////////////////// 构建Data包 ////////////////////////
Data data = new Data();
data.freshnessPeriod.setFreshnessPeriod(02346345465453L);
data.payload.setValue(new byte[]{1, 2, 3, 4, 5});
data.congestionMark.setCongestionLevel(1234L);
Identifier id2 = new Identifier("/wzq");
data.setName(id2);
data.minPacket.packetType = new VlInt(3);
// 正常测试签名与验签
keyChain.signData(data);
keyChain.verifyData(data);
}
@Benchmark
public void testExportAndImportSafeBag() throws Exception {
// 测试设置新身份为当前身份
KeyChain keyChain = new KeyChain();
// Identity id = createRandomIdentity();
// keyChain.getIdentifyManager().setDefaultIdentity(id, true);
// 输入密码用于解锁身份
// keyChain.setCurrentIdentity(keyChain.getIdentifyManager().getDefaultIdentity(), "0123456789abcdef");
// 正常导入导出 通过
Identity id2 = createRandomIdentity();
SafeBag bag = keyChain.exportSafeBag(id2, "1234");
keyChain.importSafeBag(bag, "1234", true);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(KeychainBenchmark.class.getSimpleName())
.build();
new Runner(opt).run();
}
}