mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
db unittest completed(annotations added to be compatible with go version's json)
This commit is contained in:
1 parent
f727a5c505
commit
c43a9a3a1c
7 files changed
+173
-27
No files matched your search
@@ -1,6 +1,5 @@
|
||||
package minsecurity.identity.persist.sqlite;
|
||||
|
||||
import org.sqlite.mc.SQLiteMCConfig;
|
||||
import org.sqlite.mc.SQLiteMCSqlCipherConfig;
|
||||
|
||||
import java.io.File;
|
||||
@@ -72,15 +71,16 @@ public class Sqlite {
|
||||
try{
|
||||
String homePath = SqliteUtil.home();
|
||||
homePath += "/min/identity/";
|
||||
String dbPath = homePath;
|
||||
if (!SqliteUtil.pathExists(dbPath)){
|
||||
new File(dbPath).mkdirs();
|
||||
db_path = homePath;
|
||||
if (!SqliteUtil.pathExists(db_path)){
|
||||
new File(db_path).mkdirs();
|
||||
}
|
||||
String real_db_file = dbPath + db_file;
|
||||
String real_db_file = db_path + db_file;
|
||||
// 兼容sqlcipher
|
||||
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
|
||||
stmt = c.createStatement();
|
||||
stmt.executeUpdate(table_create);
|
||||
int res = stmt.executeUpdate(table_create);
|
||||
System.out.println(res);
|
||||
stmt.close();
|
||||
c.close();
|
||||
}catch (Exception ex){
|
||||
@@ -94,11 +94,12 @@ public class Sqlite {
|
||||
Connection c = null;
|
||||
Statement stmt = null;
|
||||
try{
|
||||
boolean db_exists = SqliteUtil.pathExists(filePath);
|
||||
db_path = filePath;
|
||||
boolean db_exists = SqliteUtil.pathExists(db_path);
|
||||
if (!db_exists){
|
||||
new File(filePath).mkdirs();
|
||||
new File(db_path).mkdirs();
|
||||
}
|
||||
String real_db_file = filePath + db_file;
|
||||
String real_db_file = db_path + db_file;
|
||||
// 兼容sqlcipher
|
||||
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
|
||||
stmt = c.createStatement();
|
||||
@@ -117,7 +118,7 @@ public class Sqlite {
|
||||
Connection c = null;
|
||||
try{
|
||||
String real_db_file = db_path + db_file;
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
// Class.forName("org.sqlite.JDBC");
|
||||
// 兼容sqlcipher
|
||||
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
|
||||
// TODO 设置数据库最大连接数
|
||||
|
||||
@@ -20,9 +20,13 @@ public final class Db {
|
||||
private static Identity getIdentityByNameFromStorage(String name, Connection c) throws Exception {
|
||||
PreparedStatement pstmt = c.prepareStatement("SELECT * FROM identityinfo WHERE name = ? LIMIT 1");// LIMIT 1代替Queryrow
|
||||
pstmt.setString(1, name);
|
||||
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
Identity id = getIdentityFromSqlRow(rs);
|
||||
|
||||
rs.close();
|
||||
pstmt.close();
|
||||
return getIdentityFromSqlRow(rs);
|
||||
return id;
|
||||
}
|
||||
|
||||
private static Identity getIdentityFromSqlRow(ResultSet rs) throws Exception {
|
||||
@@ -49,7 +53,10 @@ public final class Db {
|
||||
byte[] priByte = Base64.getDecoder().decode(priStr);
|
||||
PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo);
|
||||
|
||||
Certificate cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
Certificate cert = new Certificate();
|
||||
if (!("".equals(certStr))){
|
||||
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);
|
||||
@@ -78,7 +85,10 @@ public final class Db {
|
||||
byte[] priByte = Base64.getDecoder().decode(priStr);
|
||||
PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo);
|
||||
|
||||
Certificate cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
Certificate cert = new Certificate();
|
||||
if (!("".equals(certStr))){
|
||||
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));
|
||||
@@ -111,7 +121,10 @@ public final class Db {
|
||||
byte[] priByte = Base64.getDecoder().decode(priStr);
|
||||
PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo);
|
||||
|
||||
Certificate cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
Certificate cert = new Certificate();
|
||||
if (!("".equals(certStr))){
|
||||
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);
|
||||
@@ -136,16 +149,19 @@ public final class Db {
|
||||
Connection c = Sqlite.getInstance().getConn();
|
||||
Statement stmt = c.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT * from identityinfo");
|
||||
List<Identity> res = getIdentityFromSqlRows(rs);
|
||||
rs.close();
|
||||
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();
|
||||
Identity id = getIdentityByNameFromStorage(name, c);
|
||||
c.close();
|
||||
return getIdentityByNameFromStorage(name, c);
|
||||
return id;
|
||||
}
|
||||
|
||||
private static void setDefaultIdentityByNameInStorage(String name) throws Exception {
|
||||
@@ -154,19 +170,22 @@ public final class Db {
|
||||
try{
|
||||
PreparedStatement pstmt = c.prepareStatement("select * from identityinfo where name = ? LIMIT 1");
|
||||
pstmt.setString(1, name);
|
||||
ResultSet rs = pstmt.executeQuery(String.format("select * from identityinfo where name = %s LIMIT 1", name));
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
getDefaultIdentityFromStorage(rs);
|
||||
rs.close();
|
||||
pstmt.close();
|
||||
|
||||
PreparedStatement pstmt2 = c.prepareStatement("SELECT * from identityinfo where is_default = ?");
|
||||
pstmt2.setInt(1, 1);
|
||||
rs = pstmt2.executeQuery();
|
||||
Identity id = getDefaultIdentityFromStorage(rs);
|
||||
ResultSet rs2 = pstmt2.executeQuery();
|
||||
Identity id = getDefaultIdentityFromStorage(rs2);
|
||||
if (id != null){
|
||||
cancelDefaultIdentityFromStorage(id.getName(), c);
|
||||
}
|
||||
setDefaultIdentityFromStorage(name, c);
|
||||
rs2.close();
|
||||
pstmt2.close();
|
||||
|
||||
c.commit();
|
||||
c.close();
|
||||
}catch (Exception ex){
|
||||
@@ -211,7 +230,7 @@ public final class Db {
|
||||
String pubStr = "", priStr = "", certStr, prikeyRawByte = "";
|
||||
int algo, sign;
|
||||
|
||||
if (identity.hasPrivateKey()){
|
||||
if (identity.getPrikey() != null){
|
||||
byte[] priByte = identity.getPrikey().getBytes();
|
||||
priStr = Base64.getEncoder().encodeToString(priByte);
|
||||
}
|
||||
@@ -230,8 +249,8 @@ public final class Db {
|
||||
|
||||
PreparedStatement pstmt = c.prepareStatement("INSERT INTO identityinfo(name, pubkey, prikey, pubkey_algo, signature_algo, pass, cert,prikey_raw_byte) values(?,?,?,?,?,?,?,?)");
|
||||
pstmt.setString(1, identity.getName());
|
||||
pstmt.setString(2, priStr);
|
||||
pstmt.setString(3, pubStr);
|
||||
pstmt.setString(2, pubStr);
|
||||
pstmt.setString(3, priStr);
|
||||
pstmt.setInt(4, algo);
|
||||
pstmt.setInt(5, sign);
|
||||
pstmt.setString(6, identity.getPasswd());
|
||||
@@ -248,6 +267,10 @@ public final class Db {
|
||||
PreparedStatement pstmt = c.prepareStatement("SELECT * from identityinfo where is_default= ? LIMIT 1");
|
||||
pstmt.setInt(1, 1);
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
return getDefaultIdentityFromStorage(rs);
|
||||
Identity id = getDefaultIdentityFromStorage(rs);
|
||||
rs.close();
|
||||
pstmt.close();
|
||||
c.close();
|
||||
return id;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user