private static DatabaseDescription OpenDescription(string location, CryptographyCredential credential) { DatabaseDescription description = new DatabaseDescription(); if (credential == null) { var c = File.ReadAllText(location); var l = c.Split(':'); description.isPublic = bool.Parse(l[1]); } else if (credential.Key == "") { var c = File.ReadAllText(location); var l = c.Split(':'); description.isPublic = bool.Parse(l[1]); } else { var c = File.ReadAllText(location); CustomedAES aes = new CustomedAES(); aes.Key = credential.Key; aes.IV = credential.IV; var l = (aes.Decrypt(c)).Split(':'); description.isPublic = bool.Parse(l[1]); } return(new DatabaseDescription()); }
public Database(String Home = "./Databases/", DatabaseMode loadMode = DatabaseMode.OnDemand, CryptographyCredential cryptographyCredential = null) { givenHome = Home; if (cryptographyCredential == null) { this.cryptographyCredential = new CryptographyCredential(); } else { this.cryptographyCredential = cryptographyCredential; aes.Key = cryptographyCredential.Key; aes.IV = cryptographyCredential.IV; } HomeDirectory = new DirectoryInfo(Home); LoadMode = loadMode; switch (LoadMode) { case DatabaseMode.OnDemand: { //Do nothing. //Things will only be loaded when they are needed. } break; case DatabaseMode.Cache: break; case DatabaseMode.SemiCache: break; default: break; } }
void InitBasicCommands() { { //Init BasicCommandSet.Functions.Add("OpenDatabase", (List <string> para, string content, Session session) => { if (CheckName(para[0]) == false) { return("L2KDB:Basic:IllegalName"); } string calculatedID = ""; { calculatedID = Authentication.ObtainDBID(para[0], para[1], para[2]); } foreach (var item in VariablesPool.Databases) { if (item.realDB.GeneratedID == calculatedID) { if (FindPermission(session, "Database->" + para[0])) { session.DatabaseAES = new CustomedAES(); session.DatabaseAES.Key = para[1]; session.DatabaseAES.IV = para[2]; session.operatingBD = item; return("L2KDB:Basic:DatabaseOpen"); } else if (FindPermission(session, "FullDBAccess")) { session.DatabaseAES = new CustomedAES(); session.DatabaseAES.Key = para[1]; session.DatabaseAES.IV = para[2]; session.operatingBD = item; return("L2KDB:Basic:DatabaseOpen"); } else { return("L2KDB:Basic:AccessForbidden"); } } } if (Directory.Exists(BasePath + para[0])) { if (FindPermission(session, "Database->" + para[0])) { //session.operatingBD = item;if (para[1] != "") CryptographyCredential cc = null; if (para[1] != "") { cc = new CryptographyCredential(CryptographtType.AES, para[1], para[2]); } Database database = Database.OpenDatabase(BasePath + para[0], credential: cc); VariablesPool.Databases.Add(database); database.realDB.GeneratedID = calculatedID; session.operatingBD = database; session.DatabaseAES = new CustomedAES(); session.DatabaseAES.Key = para[1]; session.DatabaseAES.IV = para[2]; return("L2KDB:Basic:DatabaseOpen"); } else if (FindPermission(session, "FullDBAccess")) { CryptographyCredential cc = null; if (para[1] != "") { cc = new CryptographyCredential(CryptographtType.AES, para[1], para[2]); } Database database = Database.OpenDatabase(BasePath + para[0], credential: cc); VariablesPool.Databases.Add(database); database.realDB.GeneratedID = calculatedID; session.operatingBD = database; session.DatabaseAES = new CustomedAES(); session.DatabaseAES.Key = para[1]; session.DatabaseAES.IV = para[2]; return("L2KDB:Basic:DatabaseOpen"); } else { return("L2KDB:Basic:AccessForbidden"); } } else { return("L2KDB:Basic:DatabaseNoFound"); } }); BasicCommandSet.Functions.Add("OpenForm", (List <string> para, string content, Session session) => { if (CheckName(para[0]) == false) { return("L2KDB:Basic:IllegalName"); } if (FindPermission(session, "FullDBAccess") || FindPermission(session, "Database->" + session.operatingBD.realDB.HomeDirectory.Name)) { try { session.operatingBD.realDB.OpenForm(para[0]); } catch (Exception) { return("L2KDB:Basic:UnknownError"); } return("L2KDB:Basic:FromOpen"); } else { return("L2KDB:Basic:AccessForbidden"); } }); BasicCommandSet.Functions.Add("CreateDatabase", (List <string> para, string content, Session session) => { if (CheckName(para[0]) == false) { return("L2KDB:Basic:IllegalName"); } if (FindPermission(session, "FullDBAccess") || FindPermission(session, "CreateDatabase")) { //L2KDB:Basic:CreateDatabase,Name,Key,IV try { bool willPub = false; try { if (para[3].ToUpper() == "PUBLIC") { willPub = true; } } catch (Exception) { } Database.CreateDatabase(Path.Combine(BasePath, para[0]), credential: new CryptographyCredential() { Key = para[1], IV = para[2] }, isPublic: willPub); SetPermission(session.AuthID, "Database->" + para[0], "" + true); //Directory.CreateDirectory(BasePath + para[0]); } catch (Exception) { return("L2KDB:Basic:ErrorOnDatabaseCreation"); } return("L2KDB:Basic:DatabaseCreated"); } else { return("L2KDB:Basic:AccessForbidden"); } }); BasicCommandSet.Functions.Add("Query", (List <string> para, string content, Session session) => { if (CheckName(para[0]) == false) { return("L2KDB:Basic:IllegalName"); } if (FindPermission(session, "Database->" + session.operatingBD.realDB.HomeDirectory.Name) || FindPermission(session, "FullDBAccess")) { var data = session.operatingBD.realDB.Query(para[0], para[1]); session.SendData("L2KDB:Basic:DatabaseQueryResult", "" + data); return("-1"); } return("L2KDN:Basic:UnknownError"); }); BasicCommandSet.Functions.Add("GetForms", (List <string> para, string content, Session session) => { if (FindPermission(session, "Database->" + session.operatingBD.realDB.HomeDirectory.Name) || FindPermission(session, "FullDBAccess")) { var data = session.operatingBD.realDB.GetForms(); var combineD = ""; foreach (var item in data) { //Diagnotor.CurrentDiagnotor.Log("Form:"+combineD); if (combineD == "") { combineD = item; } else { combineD += Environment.NewLine + item; } } if (combineD == "") { combineD = "L2KDB:Basic:NoForms"; } session.SendData("L2KDB:Basic:DatabaseGetFormsResult", "" + combineD); return("-1"); } else { return("L2KDB:Basic:AccessForbidden"); } }); BasicCommandSet.Functions.Add("GetID1", (List <string> para, string content, Session session) => { if (FindPermission(session, "Database->" + session.operatingBD.realDB.HomeDirectory.Name) || FindPermission(session, "FullDBAccess")) { var data = session.operatingBD.realDB.GetID1(); var combineD = ""; foreach (var item in data) { //Diagnotor.CurrentDiagnotor.Log("Form:"+combineD); if (combineD == "") { combineD = item; } else { combineD += Environment.NewLine + item; } } if (combineD == "") { combineD = "L2KDB:Basic:NoID1"; } session.SendData("L2KDB:Basic:DatabaseGetID1Result", "" + combineD); return("-1"); } else { return("L2KDB:Basic:AccessForbidden"); } }); BasicCommandSet.Functions.Add("GetID2", (List <string> para, string content, Session session) => { if (CheckName(para[0]) == false) { return("L2KDB:Basic:IllegalName"); } if (FindPermission(session, "Database->" + session.operatingBD.realDB.HomeDirectory.Name) || FindPermission(session, "FullDBAccess")) { var data = session.operatingBD.realDB.GetID2(para[0]); var combineD = ""; foreach (var item in data) { //Diagnotor.CurrentDiagnotor.Log("Form:"+combineD); if (combineD == "") { combineD = item; } else { combineD += Environment.NewLine + item; } } if (combineD == "") { combineD = "L2KDB:Basic:NoID2"; } session.SendData("L2KDB:Basic:DatabaseGetID2Result", "" + combineD); return("-1"); } else { return("L2KDB:Basic:AccessForbidden"); } }); BasicCommandSet.Functions.Add("Save", (List <string> para, string content, Session session) => { //L2KDB:Basic:Save,|[Session]| if (CheckName(para[0]) == false) { return("L2KDB:Basic:IllegalName"); } if (FindPermission(session, "Database->" + session.operatingBD.realDB.HomeDirectory.Name) || FindPermission(session, "FullDBAccess")) { session.operatingBD.realDB.Save(para[0], para[1], content); return("L2KDB:Basic:OperationCompleted"); } return("L2KDB:Basic:AccessForbidden"); }); BasicCommandSet.Functions.Add("DeleteID2", (List <string> para, string content, Session session) => { if (CheckName(para[0]) == false) { return("L2KDB:Basic:IllegalName"); } if (FindPermission(session, "Database->" + session.operatingBD.realDB.HomeDirectory.Name) || FindPermission(session, "FullDBAccess")) { try { session.operatingBD.realDB.Remove(para[0], para[1]); return("L2KDB:Basic:OperationCompleted"); } catch (Exception) { return("L2KDB:Basic:OperationError"); } } return("L2KDB:Basic:AccessForbidden"); }); BasicCommandSet.Functions.Add("DeleteID1", (List <string> para, string content, Session session) => { if (CheckName(para[0]) == false) { return("L2KDB:Basic:IllegalName"); } if (FindPermission(session, "Database->" + session.operatingBD.realDB.HomeDirectory.Name) || FindPermission(session, "FullDBAccess")) { try { session.operatingBD.realDB.RemoveID1(para[0]); return("L2KDB:Basic:OperationCompleted"); } catch (Exception) { return("L2KDB:Basic:OperationError"); } } return("L2KDB:Basic:AccessForbidden"); }); BasicCommandSet.Functions.Add("DeleteForm", (List <string> para, string content, Session session) => { if (CheckName(para[0]) == false) { return("L2KDB:Basic:IllegalName"); } if (FindPermission(session, "Database->" + session.operatingBD.realDB.HomeDirectory.Name) || FindPermission(session, "FullDBAccess")) { try { session.operatingBD.realDB.DeleteForm(para[0]); return("L2KDB:Basic:OperationCompleted"); } catch (Exception) { return("L2KDB:Basic:OperationError"); } } return("L2KDB:Basic:AccessForbidden"); }); BasicCommandSet.Functions.Add("GetDatabaseVersion", (List <string> para, string content, Session session) => { return(LiteDatabase.Database.DatabaseVersion.ToString()); }); } }
private static void SaveDescription(DatabaseDescription description, string location, CryptographyCredential credential) { if (credential == null) { File.WriteAllText(location, "PUBLIC:" + description.isPublic); } else if (credential.Key == "") { File.WriteAllText(location, "PUBLIC:" + description.isPublic); } else { CustomedAES aes = new CustomedAES(); aes.Key = credential.Key; aes.IV = credential.IV; File.WriteAllText(location, aes.Encrypt("PUBLIC:" + description.isPublic)); } }
public static Database OpenDatabase(string name, DatabaseMode loadMode = DatabaseMode.OnDemand, CryptographyCredential credential = null) { Database database = new Database(); try { var descption = OpenDescription(Path.Combine(name, "L2KDB-Description"), credential); database.Description = descption; } catch (Exception) { throw new Exception("WRONG-AES"); } database.realDB = new LiteDatabase.Database(name, loadMode, credential); return(database); }
public static Database CreateDatabase(string name, DatabaseMode loadMode = DatabaseMode.OnDemand, CryptographyCredential credential = null, bool isPublic = false) { Database database = new Database(); DatabaseDescription description = new DatabaseDescription(); description.isPublic = isPublic; if (!Directory.Exists(name)) { Directory.CreateDirectory(name); } SaveDescription(description, Path.Combine(name, "L2KDB-Description"), credential); database.realDB = new LiteDatabase.Database(name, loadMode, credential); return(database); }