From c653fece93316a1d2b1c3ecd799d1ae303498aec Mon Sep 17 00:00:00 2001 From: DarkFeather Date: Thu, 15 Dec 2016 16:29:07 -0600 Subject: [PATCH] Getting rid of static privates on CryptoWorkbench; camel-casing names; introducing SharedLibraries dependency --- affine.csharp => Affine.csharp | 0 analysis.csharp => Analysis.csharp | 0 caesarian.csharp => Caesarian.csharp | 0 chargrid.csharp => CharGrid.csharp | 0 cipher.csharp => Cipher.csharp | 2 +- ...ition.csharp => ColumnTransposition.csharp | 0 ...workbench.csharp => CryptoWorkbench.csharp | 30 ++++------ Makefile | 6 +- README.bzr => README | 0 simple.csharp => Simple.csharp | 0 substitution.csharp => Substitution.csharp | 0 ubchi.csharp => Ubchi.csharp | 0 vigenere.csharp => Vigenere.csharp | 0 ravenexecute.csharp | 58 ------------------- 14 files changed, 15 insertions(+), 81 deletions(-) rename affine.csharp => Affine.csharp (100%) rename analysis.csharp => Analysis.csharp (100%) rename caesarian.csharp => Caesarian.csharp (100%) rename chargrid.csharp => CharGrid.csharp (100%) rename cipher.csharp => Cipher.csharp (98%) rename columntransposition.csharp => ColumnTransposition.csharp (100%) rename cryptoworkbench.csharp => CryptoWorkbench.csharp (92%) rename README.bzr => README (100%) rename simple.csharp => Simple.csharp (100%) rename substitution.csharp => Substitution.csharp (100%) rename ubchi.csharp => Ubchi.csharp (100%) rename vigenere.csharp => Vigenere.csharp (100%) delete mode 100644 ravenexecute.csharp diff --git a/affine.csharp b/Affine.csharp similarity index 100% rename from affine.csharp rename to Affine.csharp diff --git a/analysis.csharp b/Analysis.csharp similarity index 100% rename from analysis.csharp rename to Analysis.csharp diff --git a/caesarian.csharp b/Caesarian.csharp similarity index 100% rename from caesarian.csharp rename to Caesarian.csharp diff --git a/chargrid.csharp b/CharGrid.csharp similarity index 100% rename from chargrid.csharp rename to CharGrid.csharp diff --git a/cipher.csharp b/Cipher.csharp similarity index 98% rename from cipher.csharp rename to Cipher.csharp index 594f6de..4501a64 100644 --- a/cipher.csharp +++ b/Cipher.csharp @@ -29,7 +29,7 @@ namespace AniNIX.Crypto { } /// - /// We should be able to act on a workspace and command line. Most ciphers will sue the same syntax. Those that don't can override. + /// We should be able to act on a workspace and command line. Most ciphers will use the same syntax. Those that don't can override. /// /// The current version of the text being worked on. /// The command sequence. diff --git a/columntransposition.csharp b/ColumnTransposition.csharp similarity index 100% rename from columntransposition.csharp rename to ColumnTransposition.csharp diff --git a/cryptoworkbench.csharp b/CryptoWorkbench.csharp similarity index 92% rename from cryptoworkbench.csharp rename to CryptoWorkbench.csharp index f03ff87..3e6f93e 100644 --- a/cryptoworkbench.csharp +++ b/CryptoWorkbench.csharp @@ -3,7 +3,8 @@ using System.IO; using System.Linq; using System.Text; using System.Collections.Generic; -using AniNIX.TheRaven; +using System.Reflection; +using AniNIX.Shared; namespace AniNIX.Crypto { public class Workbench { @@ -12,14 +13,7 @@ namespace AniNIX.Crypto { public StringBuilder HelpText = new StringBuilder(); public Dictionary SwitchCases = new Dictionary(); // The workbench needs to maintain an instance of each ciphersuite for operation. - private Substitution _sub; - private Analysis _analysis; - private Simple _simple; - private Caesarian _caesar; - private Affine _affine; - private Vigenere _vig; - private ColumnTransposition _col; - private Ubchi _ubchi; + private List _ciphers = new List(); // If this is true, we will prevent prompt and filesystem access. private bool _isBlind = false; @@ -107,15 +101,11 @@ namespace AniNIX.Crypto { HelpText.Append("exit -- exit and show the result.\n"); HelpText.Append("quit -- alias of exit.\n"); // Initialize the ciphersuites. - _sub = new Substitution(this); - _analysis = new Analysis(this); - _simple = new Simple(this); - _caesar = new Caesarian(this); - _affine = new Affine(this); - _vig = new Vigenere(this); - _col = new ColumnTransposition(this); - _ubchi = new Ubchi(this); - + Object[] cipherArgs = { (Object)this }; + foreach (Type cipherType in Assembly.GetAssembly(typeof(Cipher)).GetTypes() + .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(Cipher)))) { + _ciphers.Add((Cipher)Activator.CreateInstance(cipherType, cipherArgs)); + } } /// @@ -250,9 +240,9 @@ namespace AniNIX.Crypto { case "regex": try { if (line.Length == 3) { - Console.Write(RavenExecute.Command(String.Format("bash /usr/local/src/CryptoWorkbench/regex-lookup.bash \"{0}\" \"{1}\"",line[1].Replace("\\","\\\\").Replace("$","\\$"),line[2].Replace("\\","\\\\").Replace("$","\\$")))); + Console.Write(ExecuteCommand.Run(String.Format("bash /usr/local/src/CryptoWorkbench/regex-lookup.bash \"{0}\" \"{1}\"",line[1].Replace("\\","\\\\").Replace("$","\\$"),line[2].Replace("\\","\\\\").Replace("$","\\$")))); } else if (line.Length == 2) { - Console.Write(RavenExecute.Command(String.Format("bash /usr/local/src/CryptoWorkbench/regex-lookup.bash \"{0}\"",line[1].Replace("\\","\\\\").Replace("$","\\$")))); + Console.Write(ExecuteCommand.Run(String.Format("bash /usr/local/src/CryptoWorkbench/regex-lookup.bash \"{0}\"",line[1].Replace("\\","\\\\").Replace("$","\\$")))); } else { Console.Error.WriteLine("Need at least one search term."); } diff --git a/Makefile b/Makefile index 0cd1b4c..89da37e 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,9 @@ TMUXSetting != grep -c "cryptoworkbench" /etc/tmux.conf -compile: clean /usr/bin/mcs analysis.csharp substitution.csharp caesarian.csharp cryptoworkbench.csharp - /usr/bin/mcs -out:cryptoworkbench.exe *.csharp 2>&1 | grep -v 'is assigned but its value is never used' +compile: clean /usr/bin/mcs CryptoWorkbench.csharp + if [ ! -d ../SharedLibraries ]; then git -C /usr/local/src clone https://aninix.net/foundation/SharedLibraries; fi + git -c /usr/local/src/SharedLibraries pull + /usr/bin/mcs -out:cryptoworkbench.exe ../SharedLibraries/CSharp/*.csharp *.csharp 2>&1 test: /usr/bin/mono compile /usr/bin/mono cryptoworkbench.exe ./sample.txt diff --git a/README.bzr b/README similarity index 100% rename from README.bzr rename to README diff --git a/simple.csharp b/Simple.csharp similarity index 100% rename from simple.csharp rename to Simple.csharp diff --git a/substitution.csharp b/Substitution.csharp similarity index 100% rename from substitution.csharp rename to Substitution.csharp diff --git a/ubchi.csharp b/Ubchi.csharp similarity index 100% rename from ubchi.csharp rename to Ubchi.csharp diff --git a/vigenere.csharp b/Vigenere.csharp similarity index 100% rename from vigenere.csharp rename to Vigenere.csharp diff --git a/ravenexecute.csharp b/ravenexecute.csharp deleted file mode 100644 index ad24027..0000000 --- a/ravenexecute.csharp +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.IO; -using System.Text; -using System.Diagnostics; -using System.Collections.Generic; - -namespace AniNIX.TheRaven { - - public static class RavenExecute { - - /// - /// This method allows TheRaven to execute a command on the OS. - /// - /// The command string to run as the string argument to "bash -c 'command'" - /// The effective replacement for the command's stdinThe stdout of the command - /// - public static String Command(String command, String input) { - //Sanitize inputs. - if (command.Contains("\'")) { - throw new Exception("Command strings cannot include \'."); - } - - //Create process. - Process proc = new Process(); - proc.StartInfo.CreateNoWindow = true; - proc.StartInfo.FileName = "/bin/bash"; - proc.StartInfo.Arguments = String.Format("-c \'{0}\'",command); - proc.StartInfo.UseShellExecute=false; - - //Redirect input - proc.StartInfo.RedirectStandardOutput=true; - proc.StartInfo.RedirectStandardInput=true; - - //Start process - proc.Start(); - - //Add input and read output. - proc.StandardInput.Write(input); - proc.StandardInput.Close(); - proc.WaitForExit(); - if (proc.ExitCode != 0) { - throw new Exception(String.Format("Failed to exit command with return code {0}",proc.ExitCode)); - } - String stdoutString = proc.StandardOutput.ReadToEnd(); - - //Close up and return - proc.Close(); - return stdoutString; - } - - //Add polymorphism to allow no stdin - public static String Command(String command) { - return Command(command,null); - } - - } -}