mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
db and internal's implementation
This commit is contained in:
@@ -64,12 +64,13 @@ public class Sqlite {
|
||||
Statement stmt = null;
|
||||
try{
|
||||
String homePath = SqliteUtil.home();
|
||||
homePath += "/min/identity";
|
||||
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);
|
||||
@@ -91,6 +92,7 @@ public class Sqlite {
|
||||
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);
|
||||
@@ -104,10 +106,11 @@ public class Sqlite {
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConn() throws SQLException {
|
||||
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;
|
||||
|
||||
@@ -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