mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-17 21:50:25 +08:00
Merge branch 'master' of gitee.com:willfree/min-dev-java
This commit is contained in:
@@ -94,6 +94,15 @@
|
||||
<version>3.0.8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Sqlite Driver-->
|
||||
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.28.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,5 +1,8 @@
|
||||
package minsecurity.identity.persist.sqlite;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
|
||||
/*
|
||||
* @Author: zhengqi wu
|
||||
* @Description: Sqlite封装
|
||||
@@ -13,6 +16,20 @@ public class Sqlite {
|
||||
|
||||
private String passwd = "2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99"; // 初始数据库密码
|
||||
private String db_file = "identity.db"; // 数据库文件名
|
||||
private String db_path = "";
|
||||
private String table_create = "CREATE TABLE IF NOT EXISTS \"identityinfo\" (\n" +
|
||||
"\t\"name\"\tvarchar(255) NOT NULL,\n" +
|
||||
"\t\"pubkey\"\tvarchar(255),\n" +
|
||||
"\t\"prikey\"\tvarchar(255),\n" +
|
||||
"\t\"pubkey_algo\"\tint,\n" +
|
||||
"\t\"signature_algo\"int,\n" +
|
||||
"\t\"pass\"\tvarchar(255),\n" +
|
||||
"\t\"cert\"\tTEXT,\n" +
|
||||
"\t\"is_default\"\tint DEFAULT 0,\n" +
|
||||
"\t\"prikey_raw_byte\" varchar(255),\n" +
|
||||
"\tPRIMARY KEY(\"name\")\n" +
|
||||
");";
|
||||
|
||||
private Sqlite(){
|
||||
|
||||
}
|
||||
@@ -37,18 +54,68 @@ public class Sqlite {
|
||||
passwd = pass;
|
||||
}
|
||||
|
||||
public void open(String filePath){
|
||||
/**
|
||||
* 进行数据库文件的连接测试,如果表不存在则新建
|
||||
* 数据库文件默认写入地址为~/.min/identity/identity.db
|
||||
* @throws Exception
|
||||
*/
|
||||
public void openDefault() throws Exception {
|
||||
Connection c = null;
|
||||
Statement stmt = null;
|
||||
try{
|
||||
String homePath = SqliteUtil.home();
|
||||
homePath += "/min/identity/";
|
||||
String dbPath = homePath;
|
||||
if (!SqliteUtil.pathExists(dbPath)){
|
||||
new File(dbPath).mkdirs();
|
||||
}
|
||||
String real_db_file = dbPath + db_file;
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file);
|
||||
stmt = c.createStatement();
|
||||
stmt.executeUpdate(table_create);
|
||||
stmt.close();
|
||||
c.close();
|
||||
}catch (Exception ex){
|
||||
if (c != null)
|
||||
c.close();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
//自定义文件地址,注意要用/结尾,比如:/min/identity/
|
||||
public void open(String filePath) throws Exception {
|
||||
Connection c = null;
|
||||
Statement stmt = null;
|
||||
try{
|
||||
boolean db_exists = SqliteUtil.pathExists(filePath);
|
||||
if (!db_exists){
|
||||
|
||||
new File(filePath).mkdirs();
|
||||
}
|
||||
String real_db_file = filePath + db_file;
|
||||
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file);
|
||||
stmt = c.createStatement();
|
||||
stmt.executeUpdate(table_create);
|
||||
stmt.close();
|
||||
c.close();
|
||||
}catch (Exception ex){
|
||||
// TODO 出错时关闭数据库
|
||||
// DONE 出错时关闭数据库
|
||||
if (c != null)
|
||||
c.close();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Connection getConn() throws Exception {
|
||||
Connection c = null;
|
||||
try{
|
||||
String real_db_file = db_path + db_file;
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file);
|
||||
// TODO 设置数据库最大连接数
|
||||
return c;
|
||||
}catch (Exception ex){
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,170 @@
|
||||
package minsecurity.identity.persist.sqlite.db;
|
||||
|
||||
public class Db {
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.cert.CertUtils;
|
||||
import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.KeyUtils;
|
||||
import minsecurity.crypto.PrivateKeyInterface;
|
||||
import minsecurity.crypto.PublicKeyInterface;
|
||||
import minsecurity.identity.Identity;
|
||||
import minsecurity.identity.KeyParam;
|
||||
import minsecurity.identity.persist.sqlite.Sqlite;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
public final class Db {
|
||||
private static Identity getIdentityByNameFromStorage(String name, Connection c) throws Exception {
|
||||
Statement stmt = c.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(String.format("SELECT * FROM identityinfo WHERE name = %s LIMIT 1", name)); // LIMIT 1代替Queryrow
|
||||
stmt.close();
|
||||
return getIdentityFromSqlRow(rs);
|
||||
}
|
||||
|
||||
private static Identity getIdentityFromSqlRow(ResultSet rs) throws Exception {
|
||||
if (!rs.isBeforeFirst())
|
||||
return null;
|
||||
|
||||
String pubStr = "", priStr = "", pass = "", certStr = "", name = "", prikeyRawByte = "";
|
||||
int algo = 0, sign = 0, def = 0;
|
||||
|
||||
while (rs.next()){
|
||||
pubStr = rs.getString("pubKey");
|
||||
priStr = rs.getString("priKey");
|
||||
pass = rs.getString("pass");
|
||||
certStr = rs.getString("cert");
|
||||
name = rs.getString("name");
|
||||
prikeyRawByte = rs.getString("prikey_raw_byte");
|
||||
algo = rs.getInt("pubkey_algo");
|
||||
sign = rs.getInt("signature_algo");
|
||||
def = rs.getInt("is_default");
|
||||
}
|
||||
|
||||
byte[] pubByte = Base64.getDecoder().decode(pubStr);
|
||||
PublicKeyInterface pubKey = KeyUtils.unMarshalPublicKey(pubByte, algo);
|
||||
byte[] priByte = Base64.getDecoder().decode(priStr);
|
||||
PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo);
|
||||
|
||||
Certificate cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
byte[] priKeyByte = Base64.getDecoder().decode(prikeyRawByte);
|
||||
KeyParam keyParam = new KeyParam(algo, sign);
|
||||
return new Identity(name, keyParam, priKey, priKeyByte, pubKey, pass, cert, def == 1);
|
||||
}
|
||||
|
||||
private static List<Identity> getIdentityFromSqlRows(ResultSet rs) throws Exception {
|
||||
if (!rs.isBeforeFirst())
|
||||
return new ArrayList<>();
|
||||
|
||||
List<Identity> res = new ArrayList<>();
|
||||
String pubStr = "", priStr = "", pass = "", certStr = "", name = "", prikeyRawByte = "";
|
||||
int algo = 0, sign = 0, def = 0;
|
||||
|
||||
while (rs.next()){
|
||||
pubStr = rs.getString("pubKey");
|
||||
priStr = rs.getString("priKey");
|
||||
pass = rs.getString("pass");
|
||||
certStr = rs.getString("cert");
|
||||
name = rs.getString("name");
|
||||
prikeyRawByte = rs.getString("prikey_raw_byte");
|
||||
algo = rs.getInt("pubkey_algo");
|
||||
sign = rs.getInt("signature_algo");
|
||||
def = rs.getInt("is_default");
|
||||
byte[] pubByte = Base64.getDecoder().decode(pubStr);
|
||||
PublicKeyInterface pubKey = KeyUtils.unMarshalPublicKey(pubByte, algo);
|
||||
byte[] priByte = Base64.getDecoder().decode(priStr);
|
||||
PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo);
|
||||
|
||||
Certificate cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
byte[] priKeyByte = Base64.getDecoder().decode(prikeyRawByte);
|
||||
KeyParam keyParam = new KeyParam(algo, sign);
|
||||
res.add(new Identity(name, keyParam, priKey, priKeyByte, pubKey, pass, cert, def == 1));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private static Identity getDefaultIdentityFromStorage(ResultSet rs) throws Exception{
|
||||
if (rs == null)
|
||||
return null;
|
||||
|
||||
String pubStr = "", priStr = "", pass = "", certStr = "", name = "", prikeyRawByte = "";
|
||||
int algo = 0, sign = 0, def = 0;
|
||||
|
||||
while (rs.next()){
|
||||
pubStr = rs.getString("pubKey");
|
||||
priStr = rs.getString("priKey");
|
||||
pass = rs.getString("pass");
|
||||
certStr = rs.getString("cert");
|
||||
name = rs.getString("name");
|
||||
prikeyRawByte = rs.getString("prikey_raw_byte");
|
||||
algo = rs.getInt("pubkey_algo");
|
||||
sign = rs.getInt("signature_algo");
|
||||
def = rs.getInt("is_default");
|
||||
}
|
||||
|
||||
byte[] pubByte = Base64.getDecoder().decode(pubStr);
|
||||
PublicKeyInterface pubKey = KeyUtils.unMarshalPublicKey(pubByte, algo);
|
||||
byte[] priByte = Base64.getDecoder().decode(priStr);
|
||||
PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo);
|
||||
|
||||
Certificate cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
byte[] priKeyByte = Base64.getDecoder().decode(prikeyRawByte);
|
||||
KeyParam keyParam = new KeyParam(algo, sign);
|
||||
return new Identity(name, keyParam, priKey, priKeyByte, pubKey, pass, cert, def == 1);
|
||||
}
|
||||
|
||||
|
||||
private static boolean setDefaultIdentityFromStorage(String name, Connection c) throws Exception{
|
||||
PreparedStatement stmt = c.prepareStatement("update identityinfo set is_default = 1 where name= ?");
|
||||
stmt.setString(1, name);
|
||||
stmt.executeUpdate();
|
||||
stmt.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean cancelDefaultIdentityFromStorage(String name, Connection c) throws Exception{
|
||||
PreparedStatement stmt = c.prepareStatement("update identityinfo set is_default = 0 where name= ?");
|
||||
stmt.setString(1, name);
|
||||
stmt.executeUpdate();
|
||||
stmt.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
public static List<Identity> getAllIdentityFromStorage(String name) throws Exception {
|
||||
Connection c = Sqlite.getInstance().getConn();
|
||||
Statement stmt = c.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT * from identityinfo");
|
||||
stmt.close();
|
||||
c.close();
|
||||
List<Identity> res = getIdentityFromSqlRows(rs);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Identity getIdentityByNameFromStorage(String name) throws Exception {
|
||||
Connection c = Sqlite.getInstance().getConn();
|
||||
c.close();
|
||||
return getIdentityByNameFromStorage(name, c);
|
||||
}
|
||||
|
||||
private static boolean setDefaultIdentityByNameInStorage(String name) throws Exception {
|
||||
Connection c = Sqlite.getInstance().getConn();
|
||||
return false;
|
||||
}
|
||||
public static boolean SetDefaultIdentityByNameInStorage(String name) throws Exception {
|
||||
for (int i = 0; i < 4; i++){
|
||||
boolean res = false;
|
||||
try{
|
||||
res = setDefaultIdentityByNameInStorage(name);
|
||||
return res;
|
||||
}catch (Exception ex){
|
||||
if (ex.getMessage().equals("database is locked"))
|
||||
Thread.sleep(50);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package minsecurity.identity.persist.sqlite.db;
|
||||
|
||||
public class Internal {
|
||||
}
|
||||
Reference in New Issue
Block a user