db and internal's implementation

This commit is contained in:
ChessNineeee committed 2021-04-02 20:41:37 +08:00
1 parent 402267fa57
commit cf6717b169
3 files changed
+172 -7

No files matched your search

@@ -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;
}
}