From bf7a488011c09be39bdb4980c2bb020cb1fdb71a Mon Sep 17 00:00:00 2001 From: Carver Harrison Date: Sun, 24 Jul 2016 11:17:36 -0700 Subject: Added Hashing API and Fixed Bugs --- source/WindowsFormsApplication1/API.cs | 720 ++++++++++++++++++--------------- 1 file changed, 385 insertions(+), 335 deletions(-) (limited to 'source/WindowsFormsApplication1/API.cs') diff --git a/source/WindowsFormsApplication1/API.cs b/source/WindowsFormsApplication1/API.cs index e81788d..157f373 100644 --- a/source/WindowsFormsApplication1/API.cs +++ b/source/WindowsFormsApplication1/API.cs @@ -80,341 +80,391 @@ namespace ShiftOS } - public class API - { - public static Dictionary OpenGUIDs = new Dictionary(); - public static Dictionary DEF_PanelGUIDs = new Dictionary(); - - - /// - /// Settings file. - /// - public static Settings LoadedSettings = null; - - /// - /// Whether or not dev commands like '05tray' are available. - /// Typically, this is set to true if the release is classified as - /// an alpha, beta, or release candidate. - /// - /// Turn it off if the release is the final RC, or the stable release! - /// - /// Enabling developer mode will cause the save engine to not encrypt the save file - /// or Shiftorium Registry on save, enabling you to easily modify your save - /// to test new features or to bring in lots of codepoints. This is why - /// Developer mode should ALWAYS be off in non-dev releases, to prevent cheating. - /// - public static bool DeveloperMode = true; - - /// - /// If this is true, only certain applications will open and only - /// certain features will work. - /// - /// This is useful for story plots like the End Game where you don't - /// want the user being distracted by novelty features when they should - /// be focusing on what's happening. - /// - /// Think of it like the opposite of what Developer Mode would do. - /// - public static bool LimitedMode = false; - - public static bool InfoboxesPlaySounds = true; - - public static void SkinWidget(Widget c) - { - if(c is Button) - { - var b = c as Button; - b.FlatStyle = FlatStyle.Standard; - } - if(c is Panel || c is FlowLayoutPanel) - { - foreach(Widget child in c.Widgets) - { - SkinWidget(child); - } - } - } - - public static List RunningModProcesses = new List(); - public static Dictionary CommandAliases = new Dictionary(); - public static Terminal LoggerTerminal = null; - - // Alternate Names for True and False - public static bool yes = true; - public static bool no = false; - - /// - /// Logs an exception to the log. - /// - /// The text to log. - /// Is it a fatal crash? - public static void LogException(string Message, bool fatal) - { - if(!File.Exists(Paths.SystemDir + "_Log.txt")) - { - if (fatal == true) - { - File.WriteAllText(Paths.SystemDir + "_Log.txt", GetLogTime() + " [ExWatch/WARNING] ShiftOS has encountered an UNHANDLED exception with message \"" + Message + "\". Report this to Michael as well as what you were doing when it happened ASAP."); - } - else - { - File.WriteAllText(Paths.SystemDir + "_Log.txt", GetLogTime() + " ShiftOS encountered a handled exception with message \"" + Message + "\" and was able to keep going. Ignore this."); - } - } - else - { - List Entries = new List(); - foreach(string entry in File.ReadAllLines(Paths.SystemDir + "_Log.txt")) { - Entries.Add(entry); - } - if (fatal == true) - { - Entries.Add(GetLogTime() + " [ExWatch/WARNING] ShiftOS has encountered an UNHANDLED exception with message \"" + Message + "\". Report this to Michael as well as what you were doing when it happened ASAP."); - } - else - { - Entries.Add(GetLogTime() + " ShiftOS encountered a handled exception with message \"" + Message + "\" and was able to keep going. Ignore this."); - } - File.WriteAllLines(Paths.SystemDir + "_Log.txt", Entries.ToArray()); - } - } - - /// - /// Logs text to the log file. - /// - /// The text to log. - public static void Log(string Message) - { - if (!File.Exists(Paths.SystemDir + "_Log.txt")) - { - File.WriteAllText(Paths.SystemDir + "_Log.txt", GetLogTime() + " " + Message); - } - else - { - List Entries = new List(); - foreach (string entry in File.ReadAllLines(Paths.SystemDir + "_Log.txt")) - { - Entries.Add(entry); - } - Entries.Add(GetLogTime() + " " + Message); - File.WriteAllLines(Paths.SystemDir + "_Log.txt", Entries.ToArray()); - } - } - - /// - /// Gets a proper-formatted date/time string for the log. - /// - /// - public static string GetLogTime() - { - return "[" + DateTime.Now.ToLongDateString() + "\\" + DateTime.Now.ToLongTimeString() + "]"; - } - - /// - /// Property representing the currently loaded name pack. - /// - public static Skinning.NamePack LoadedNames - { - get - { - if (Skinning.Utilities.LoadedNames != null) - { - return Skinning.Utilities.LoadedNames; - } - else - { - Skinning.Utilities.LoadedNames = new Skinning.NamePack(); - Skinning.Utilities.loadedSkin.EmbeddedNamePackPath = "names.npk"; - Skinning.Utilities.SaveEmbeddedNamePack(); - Log("[Name Changer] Couldn't locate loaded name pack, using default name pack."); - return Skinning.Utilities.LoadedNames; - } - } - } - - /// - /// Adds a command line alias. - /// - /// Alias name - /// Command to run. - /// - public static bool AddAlias(string key, string value) - { - if(!AliasExists(key)) - { - CommandAliases.Add(key, value); - return true; - } - else - { - return false; - } - } - - /// - /// Saves alias list to save game. - /// - public static void SaveAliases() - { - string json = JsonConvert.SerializeObject(CommandAliases); - File.WriteAllText(Paths.SystemDir + "_aliases.json", json); - } - - /// - /// Loads alias list from the save game. - /// - public static void LoadAliases() - { - if (File.Exists(Paths.SystemDir + "_aliases.json")) { - string json = File.ReadAllText(Paths.SystemDir + "_aliases.json"); - CommandAliases = JsonConvert.DeserializeObject>(json); - } - else - { - CommandAliases = new Dictionary(); - } - - } - - /// - /// Removes an alias from the list. - /// - /// Alias to remove. - /// Whether the alias could be removed. - public static bool RemoveAlias(string key) - { - if(AliasExists(key)) - { - CommandAliases.Remove(key); - return true; - } - else - { - return false; - } - } - - /// - /// Checks if the provided alias exists. - /// - /// The alias to check. - /// Whether the alias exists. - public static bool AliasExists(string key) - { - bool no = false; - foreach(KeyValuePair kv in CommandAliases) - { - if(kv.Key == key) - { - no = true; - } - } - return no; - } - - /// - /// I have no idea what this does - Michael - /// - /// - public static void CreateNewLoggerTerminal(string AppName) - { - CreateForm(new Terminal(true), AppName, Properties.Resources.iconTerminal); - } - - public const string HiddenAPMCommand = "0Ifm0DcBBy10VZo/p4r1Jg=="; - public const string HiddenBTNConvertCommand = "js2qrls5kvZnutMbfH46sUKzKVrBtjzPlWn/wIIe/3g="; - public const string HiddenDodgeCommand = "mpL4WPUoDcZrsXnNUJ5RWQ=="; - public const string HiddenLabyrinthCommand = "NbNzpplGKaS5D/RdwrQMXw=="; - public const string HiddenQuickChatCommand = "iQm+/qDqgkHT/zgPiYRlZQ=="; - public const string HiddenShiftnetCommand = "NCM++hbZox7B+m9tXRXGnw=="; - public const string HiddenBWalletCommand = "1nLiZELFcaxkXDufrLuyfw=="; - public const string HiddenBDiggerCommand = "g/efSjsaglt//dr3XHnPOw=="; - public const string HiddenDecryptorCommand = "CYPXaweggfWAuS7ONt/OPQ=="; - - /// - /// Launches an saa file, hooks it up to the Lua API, and runs it. - /// - /// File to run. - public static void LaunchMod(string modSAA) - { - if (!LimitedMode) - { - if (Upgrades["shiftnet"] == true) - { - if (File.Exists(modSAA)) - { - if (File.ReadAllText(modSAA) == HiddenAPMCommand) - { - CreateForm(new Appscape(), "Appscape Package Manager", Properties.Resources.iconAppscape); - } - else if (File.ReadAllText(modSAA) == HiddenDecryptorCommand) - { - CreateForm(new ShiftnetDecryptor(), "Shiftnet Decryptor", Properties.Resources.iconShiftnet); - } - else if (File.ReadAllText(modSAA) == HiddenBDiggerCommand) - { - CreateForm(new BitnoteDigger(), "Bitnote Digger", Properties.Resources.iconBitnoteDigger); - } - else if (File.ReadAllText(modSAA) == HiddenBWalletCommand) - { - CreateForm(new BitnoteWallet(), "Bitnote Wallet", Properties.Resources.iconBitnoteWallet); - } - else if (File.ReadAllText(modSAA) == HiddenShiftnetCommand) - { - CreateForm(new Shiftnet(), "Shiftnet", Properties.Resources.iconShiftnet); - } - else if (File.ReadAllText(modSAA) == HiddenBTNConvertCommand) - { - CreateForm(new BitnoteConverter(), "Bitnote Converter", Properties.Resources.iconBitnoteWallet); - } - else if (File.ReadAllText(modSAA) == HiddenDodgeCommand) - { - CreateForm(new Dodge(), "Dodge", Properties.Resources.iconDodge); - } - else if (File.ReadAllText(modSAA) == HiddenLabyrinthCommand) - { - CreateForm(new Labyrinth(), "Labyrinth", null); - } - else - { - try - { - ExtractFile(modSAA, Paths.Mod_Temp, true); - var l = new LuaInterpreter(Paths.Mod_Temp + "main.lua"); - } - catch (Exception ex) - { - LogException("Error launching mod file (.saa): " + ex.Message, false); - CreateInfoboxSession("Error", "Could not launch the .saa file you specified. It is unsupported by this version of ShiftOS.", infobox.InfoboxMode.Info); - } - } - var story_rnd = new Random(); - int story_chance = story_rnd.Next(0, 3); - switch (story_chance) { - case 2: - if(API.Upgrades["holochat"] == false) - { - var t = new Terminal(); - API.CreateForm(t, API.LoadedNames.TerminalName, API.GetIcon("Terminal")); - t.StartDevXFuriousStory(); - t.BringToFront(); - } - break; - } - - } - else - { - throw new ModNotFoundException(); - } - } - } - else - { - CreateInfoboxSession("Limited mode", "ShiftOS is in limited mode and cannot perform this action. Please complete the current Mission first.", infobox.InfoboxMode.Info); - } - } - + public class API + { + public static Dictionary OpenGUIDs = new Dictionary(); + public static Dictionary DEF_PanelGUIDs = new Dictionary(); + + + /// + /// Settings file. + /// + public static Settings LoadedSettings = null; + + /// + /// Whether or not dev commands like '05tray' are available. + /// Typically, this is set to true if the release is classified as + /// an alpha, beta, or release candidate. + /// + /// Turn it off if the release is the final RC, or the stable release! + /// + /// Enabling developer mode will cause the save engine to not encrypt the save file + /// or Shiftorium Registry on save, enabling you to easily modify your save + /// to test new features or to bring in lots of codepoints. This is why + /// Developer mode should ALWAYS be off in non-dev releases, to prevent cheating. + /// + public static bool DeveloperMode = true; + + /// + /// If this is true, only certain applications will open and only + /// certain features will work. + /// + /// This is useful for story plots like the End Game where you don't + /// want the user being distracted by novelty features when they should + /// be focusing on what's happening. + /// + /// Think of it like the opposite of what Developer Mode would do. + /// + public static bool LimitedMode = false; + + public static bool InfoboxesPlaySounds = true; + + public static void SkinWidget(Widget c) + { + if (c is Button) + { + var b = c as Button; + b.FlatStyle = FlatStyle.Standard; + } + if (c is Panel || c is FlowLayoutPanel) + { + foreach (Widget child in c.Widgets) + { + SkinWidget(child); + } + } + } + + public static List RunningModProcesses = new List(); + public static Dictionary CommandAliases = new Dictionary(); + public static Terminal LoggerTerminal = null; + + // Alternate Names for True and False + public static bool yes = true; + public static bool no = false; + + /// + /// Logs an exception to the log. + /// + /// The text to log. + /// Is it a fatal crash? + public static void LogException(string Message, bool fatal) + { + if (!File.Exists(Paths.SystemDir + "_Log.txt")) + { + if (fatal == true) + { + File.WriteAllText(Paths.SystemDir + "_Log.txt", GetLogTime() + " [ExWatch/WARNING] ShiftOS has encountered an UNHANDLED exception with message \"" + Message + "\". Report this to Michael as well as what you were doing when it happened ASAP."); + } + else + { + File.WriteAllText(Paths.SystemDir + "_Log.txt", GetLogTime() + " ShiftOS encountered a handled exception with message \"" + Message + "\" and was able to keep going. Ignore this."); + } + } + else + { + List Entries = new List(); + foreach (string entry in File.ReadAllLines(Paths.SystemDir + "_Log.txt")) + { + Entries.Add(entry); + } + if (fatal == true) + { + Entries.Add(GetLogTime() + " [ExWatch/WARNING] ShiftOS has encountered an UNHANDLED exception with message \"" + Message + "\". Report this to Michael as well as what you were doing when it happened ASAP."); + } + else + { + Entries.Add(GetLogTime() + " ShiftOS encountered a handled exception with message \"" + Message + "\" and was able to keep going. Ignore this."); + } + File.WriteAllLines(Paths.SystemDir + "_Log.txt", Entries.ToArray()); + } + } + + /// + /// Logs text to the log file. + /// + /// The text to log. + public static void Log(string Message) + { + if (!File.Exists(Paths.SystemDir + "_Log.txt")) + { + File.WriteAllText(Paths.SystemDir + "_Log.txt", GetLogTime() + " " + Message); + } + else + { + List Entries = new List(); + foreach (string entry in File.ReadAllLines(Paths.SystemDir + "_Log.txt")) + { + Entries.Add(entry); + } + Entries.Add(GetLogTime() + " " + Message); + File.WriteAllLines(Paths.SystemDir + "_Log.txt", Entries.ToArray()); + } + } + + /// + /// Gets a proper-formatted date/time string for the log. + /// + /// + public static string GetLogTime() + { + return "[" + DateTime.Now.ToLongDateString() + "\\" + DateTime.Now.ToLongTimeString() + "]"; + } + + /// + /// Property representing the currently loaded name pack. + /// + public static Skinning.NamePack LoadedNames + { + get + { + if (Skinning.Utilities.LoadedNames != null) + { + return Skinning.Utilities.LoadedNames; + } + else + { + Skinning.Utilities.LoadedNames = new Skinning.NamePack(); + Skinning.Utilities.loadedSkin.EmbeddedNamePackPath = "names.npk"; + Skinning.Utilities.SaveEmbeddedNamePack(); + Log("[Name Changer] Couldn't locate loaded name pack, using default name pack."); + return Skinning.Utilities.LoadedNames; + } + } + } + + /// + /// Adds a command line alias. + /// + /// Alias name + /// Command to run. + /// + public static bool AddAlias(string key, string value) + { + if (!AliasExists(key)) + { + CommandAliases.Add(key, value); + return true; + } + else + { + return false; + } + } + + /// + /// Saves alias list to save game. + /// + public static void SaveAliases() + { + string json = JsonConvert.SerializeObject(CommandAliases); + File.WriteAllText(Paths.SystemDir + "_aliases.json", json); + } + + /// + /// Loads alias list from the save game. + /// + public static void LoadAliases() + { + if (File.Exists(Paths.SystemDir + "_aliases.json")) + { + string json = File.ReadAllText(Paths.SystemDir + "_aliases.json"); + CommandAliases = JsonConvert.DeserializeObject>(json); + } + else + { + CommandAliases = new Dictionary(); + } + + } + + /// + /// Removes an alias from the list. + /// + /// Alias to remove. + /// Whether the alias could be removed. + public static bool RemoveAlias(string key) + { + if (AliasExists(key)) + { + CommandAliases.Remove(key); + return true; + } + else + { + return false; + } + } + + /// + /// Checks if the provided alias exists. + /// + /// The alias to check. + /// Whether the alias exists. + public static bool AliasExists(string key) + { + bool nay = false; + foreach (KeyValuePair kv in CommandAliases) + { + if (kv.Key == key) + { + nay = true; + } + } + return nay; + } + + /// + /// I have no idea what this does - Michael + /// + /// + public static void CreateNewLoggerTerminal(string AppName) + { + CreateForm(new Terminal(true), AppName, Properties.Resources.iconTerminal); + } + + public const string HiddenAPMCommand = "0Ifm0DcBBy10VZo/p4r1Jg=="; + public const string HiddenBTNConvertCommand = "js2qrls5kvZnutMbfH46sUKzKVrBtjzPlWn/wIIe/3g="; + public const string HiddenDodgeCommand = "mpL4WPUoDcZrsXnNUJ5RWQ=="; + public const string HiddenLabyrinthCommand = "NbNzpplGKaS5D/RdwrQMXw=="; + public const string HiddenQuickChatCommand = "iQm+/qDqgkHT/zgPiYRlZQ=="; + public const string HiddenShiftnetCommand = "NCM++hbZox7B+m9tXRXGnw=="; + public const string HiddenBWalletCommand = "1nLiZELFcaxkXDufrLuyfw=="; + public const string HiddenBDiggerCommand = "g/efSjsaglt//dr3XHnPOw=="; + public const string HiddenDecryptorCommand = "CYPXaweggfWAuS7ONt/OPQ=="; + + /// + /// Launches an saa file, hooks it up to the Lua API, and runs it. + /// + /// File to run. + public static void LaunchMod(string modSAA) + { + if (!LimitedMode) + { + if (Upgrades["shiftnet"] == true) + { + if (File.Exists(modSAA)) + { + if (File.ReadAllText(modSAA) == HiddenAPMCommand) + { + CreateForm(new Appscape(), "Appscape Package Manager", Properties.Resources.iconAppscape); + } + else if (File.ReadAllText(modSAA) == HiddenDecryptorCommand) + { + CreateForm(new ShiftnetDecryptor(), "Shiftnet Decryptor", Properties.Resources.iconShiftnet); + } + else if (File.ReadAllText(modSAA) == HiddenBDiggerCommand) + { + CreateForm(new BitnoteDigger(), "Bitnote Digger", Properties.Resources.iconBitnoteDigger); + } + else if (File.ReadAllText(modSAA) == HiddenBWalletCommand) + { + CreateForm(new BitnoteWallet(), "Bitnote Wallet", Properties.Resources.iconBitnoteWallet); + } + else if (File.ReadAllText(modSAA) == HiddenShiftnetCommand) + { + CreateForm(new Shiftnet(), "Shiftnet", Properties.Resources.iconShiftnet); + } + else if (File.ReadAllText(modSAA) == HiddenBTNConvertCommand) + { + CreateForm(new BitnoteConverter(), "Bitnote Converter", Properties.Resources.iconBitnoteWallet); + } + else if (File.ReadAllText(modSAA) == HiddenDodgeCommand) + { + CreateForm(new Dodge(), "Dodge", Properties.Resources.iconDodge); + } + else if (File.ReadAllText(modSAA) == HiddenLabyrinthCommand) + { + CreateForm(new Labyrinth(), "Labyrinth", null); + } + else + { + try + { + ExtractFile(modSAA, Paths.Mod_Temp, true); + var l = new LuaInterpreter(Paths.Mod_Temp + "main.lua"); + } + catch (Exception ex) + { + LogException("Error launching mod file (.saa): " + ex.Message, false); + CreateInfoboxSession("Error", "Could not launch the .saa file you specified. It is unsupported by this version of ShiftOS.", infobox.InfoboxMode.Info); + } + } + var story_rnd = new Random(); + int story_chance = story_rnd.Next(0, 3); + switch (story_chance) + { + case 2: + if (API.Upgrades["holochat"] == false) + { + var t = new Terminal(); + API.CreateForm(t, API.LoadedNames.TerminalName, API.GetIcon("Terminal")); + t.StartDevXFuriousStory(); + t.BringToFront(); + } + break; + } + + } + else + { + throw new ModNotFoundException(); + } + } + } + else + { + CreateInfoboxSession("Limited mode", "ShiftOS is in limited mode and cannot perform this action. Please complete the current Mission first.", infobox.InfoboxMode.Info); + } + } + + /// + /// Base64 Function + /// + public static class Base64 + { + public static string Encrypt(string text) + { + return Convert.ToBase64String(Encoding.UTF8.GetBytes(text)); + } + + public static string Decrypt(string text) + { + return Encoding.UTF8.GetString(Convert.FromBase64String(text)); + } + } + + /// + /// Hashing API + /// By Carver Harrison (@carverh) + /// + public static class Hash + { + public static string MD5(string text) + { + return Encoding.UTF8.GetString(System.Security.Cryptography.MD5.Create(text).Hash); + } + public static string SHA1(string text) + { + return Encoding.UTF8.GetString(System.Security.Cryptography.SHA1.Create(text).Hash); + } + public static string SHA256(string text) + { + return Encoding.UTF8.GetString(System.Security.Cryptography.SHA256.Create(text).Hash); + } + public static string SHA384(string text) + { + return Encoding.UTF8.GetString(System.Security.Cryptography.SHA384.Create(text).Hash); + } + public static string SHA512(string text) + { + return Encoding.UTF8.GetString(System.Security.Cryptography.SHA512.Create(text).Hash); + } + public static string DSA(string text) + { + return Encoding.UTF8.GetString(System.Security.Cryptography.DSA.Create().CreateSignature(Encoding.UTF8.GetBytes(text))); + } + } /// /// Source: http://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp -- cgit v1.2.3