数据库加密和初始化分开写
This commit is contained in:
parent
bfcfa4ee9b
commit
88cea58dc3
@ -1,69 +0,0 @@
|
|||||||
using Falcon.SugarCore.DbContextExtensions;
|
|
||||||
using PrivateBox.DataContext;
|
|
||||||
using SqlSugar;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace PrivateBox
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 数据库上下文
|
|
||||||
/// </summary>
|
|
||||||
public class DbContext:Falcon.SugarCore.DbContextExtensions.DbContext
|
|
||||||
{
|
|
||||||
public AppConfig Config { get; init; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 数据库是否已经初始化
|
|
||||||
/// </summary>
|
|
||||||
private static bool IsInited = false;
|
|
||||||
|
|
||||||
public DbContext(DbContextConfig config):base(config) {
|
|
||||||
DbInit();
|
|
||||||
}
|
|
||||||
|
|
||||||
//public DbContext(AppConfig config) : base(new ConnectionConfig {
|
|
||||||
// DbType = DbType.Sqlite,
|
|
||||||
// ConnectionString = config.SqliteFileName,
|
|
||||||
// IsAutoCloseConnection = false,
|
|
||||||
|
|
||||||
//}) {
|
|
||||||
// this.Config = config;
|
|
||||||
// DbInit();
|
|
||||||
//}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化数据库
|
|
||||||
/// </summary>
|
|
||||||
public void DbInit() {
|
|
||||||
if(IsInited) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.CodeFirst.SetStringDefaultLength(200);
|
|
||||||
this.CodeFirst.InitTables<CryptionKey>();
|
|
||||||
this.CodeFirst.InitTables<CrytionItem>();
|
|
||||||
this.CodeFirst.InitTables<AppSettingsTable>();
|
|
||||||
IsInited = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取加密用的key
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>加密用的key</returns>
|
|
||||||
public string GetCrytionKey() {
|
|
||||||
var qu = this.Queryable<CryptionKey>().ToList();
|
|
||||||
if(qu.Any() && !string.IsNullOrEmpty(qu.First().Key)) {
|
|
||||||
return qu.First().Key;
|
|
||||||
}
|
|
||||||
var keyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnobqrstuvwxyz1234567890".ToArray();
|
|
||||||
StringBuilder key = new(36);
|
|
||||||
Random rd = new Random();
|
|
||||||
for(int i = 0;i < key.Capacity;i++) {
|
|
||||||
int index = (int)rd.NextInt64(0,keyChars.Length);
|
|
||||||
var k = keyChars[index];
|
|
||||||
key.Append(k);
|
|
||||||
}
|
|
||||||
this.Insertable(new CryptionKey { Key = key.ToString() }).ExecuteCommand();
|
|
||||||
return key.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
31
PrivateBox/DataContext/DbContextExtension.cs
Normal file
31
PrivateBox/DataContext/DbContextExtension.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
using Falcon.SugarCore.DbContextExtensions;
|
||||||
|
using PrivateBox.DataContext;
|
||||||
|
using SqlSugar;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PrivateBox
|
||||||
|
{
|
||||||
|
public static class DbContextExtension
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取加密用的key
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>加密用的key</returns>
|
||||||
|
public static string GetCrytionKey(this DbContext db) {
|
||||||
|
var qu = db.Queryable<CryptionKey>().ToList();
|
||||||
|
if(qu.Any() && !string.IsNullOrEmpty(qu.First().Key)) {
|
||||||
|
return qu.First().Key;
|
||||||
|
}
|
||||||
|
var keyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnobqrstuvwxyz1234567890".ToArray();
|
||||||
|
StringBuilder key = new(36);
|
||||||
|
Random rd = new Random();
|
||||||
|
for(int i = 0;i < key.Capacity;i++) {
|
||||||
|
int index = (int)rd.NextInt64(0,keyChars.Length);
|
||||||
|
var k = keyChars[index];
|
||||||
|
key.Append(k);
|
||||||
|
}
|
||||||
|
db.Insertable(new CryptionKey { Key = key.ToString() }).ExecuteCommand();
|
||||||
|
return key.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
PrivateBox/DataContext/DbInitializer.cs
Normal file
26
PrivateBox/DataContext/DbInitializer.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using Falcon.SugarCore.DbContextExtensions;
|
||||||
|
|
||||||
|
namespace PrivateBox.DataContext
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据库初始化
|
||||||
|
/// </summary>
|
||||||
|
public static class DbInitializer
|
||||||
|
{
|
||||||
|
private static int Inited = 0;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行初始化,全局仅运行一次
|
||||||
|
/// </summary>
|
||||||
|
public static void Init(DbContext db) {
|
||||||
|
|
||||||
|
if(Interlocked.CompareExchange(ref Inited,1,0) == 0) {
|
||||||
|
db.CodeFirst.SetStringDefaultLength(200);
|
||||||
|
db.CodeFirst.InitTables<CryptionKey>();
|
||||||
|
db.CodeFirst.InitTables<CrytionItem>();
|
||||||
|
db.CodeFirst.InitTables<AppSettingsTable>();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user