public MainViewModel() { BuildErrors = new ObservableCollection <CompilerError>(); Document = new TextDocument(); ExecuteCommand = new RelayCommand <string>(Execute); CancelExecutionCommand = new RelayCommand(CancelExecution); Console.SetOut(ConsoleOutput = new ConsoleOutStream()); }
public void ConsoleRaw_To_ConsoleBase64_To_ConsoleRaw() { Directory.SetCurrentDirectory(AppContext.BaseDirectory); var program = new Program(); string clearText = "bla bla useless and a bit longer than usual message lorem ipsum quick brown fox bla bla"; string[] encryptArgs = new string[] { "-e", "--input-type", "console", "--input-format", "raw", "--output-type", "console", "--output-format", "b64", "--password", "abcd", "--key-salt-length", "64", "--key-iterations", "2000", "--key-size", "256", "--skip-bytes", "0", clearText }; var sb = new StringBuilder(); Console.SetOut(new StringWriter(sb)); program.Run(encryptArgs); string outputData = ConsoleOutStream.TrimConsoleDataMarkers(sb.ToString()); Assert.IsTrue(Base64Regex.IsMatch(outputData)); string[] decryptArgs = new string[] { "-d", "--input-type", "console", "--input-format", "b64", "--output-type", "console", "--output-format", "raw", "--password", "abcd", "--key-salt-length", "64", "--key-iterations", "2000", "--key-size", "256", "--skip-bytes", "0", outputData }; sb = new StringBuilder(); Console.SetOut(new StringWriter(sb)); program.Run(decryptArgs); Assert.AreEqual(clearText, ConsoleOutStream.TrimConsoleDataMarkers(sb.ToString())); }
public void Dispose(bool disposeManaged) { if (alreadyDisposed) { return; } if (disposeManaged) { ConsoleOutStream.Dispose(); _consoleInputWriter?.Dispose(); _process?.Dispose(); _pseudoConsole?.Dispose(); _outputPipe?.Dispose(); _inputPipe?.Dispose(); } alreadyDisposed = true; }
public void Dispose(bool disposeManaged) { if (alreadyDisposed) { return; } if (disposeManaged) { ConsoleOutStream.Dispose(); // Dispose pseudo console before _consoleInputWriter to avoid // hanging on call of ClosePseudoConsole _pseudoConsole?.Dispose(); _consoleInputWriter?.Dispose(); _process?.Dispose(); _outputPipe?.Dispose(); _inputPipe?.Dispose(); } alreadyDisposed = true; }
private static bool EncryptDecrypt( DataType startType, DataType interType, DataType endType, DataFormat startFormat, DataFormat interFormat, DataFormat endFormat, string password, int saltLength, int keyIterations, int keySize, int messageLength ) { if ((interType == DataType.Console && interFormat == DataFormat.Raw) || (endType == DataType.Console && endFormat == DataFormat.Raw)) { return(false); } string startInputArg; string startOutputArg = string.Empty; byte[] originalClearMessage; if (startType == DataType.Console) { do { originalClearMessage = GenerateRandom(messageLength, true); if (startFormat == DataFormat.Base64) { startInputArg = Convert.ToBase64String(originalClearMessage); } else if (startFormat == DataFormat.Hexa) { startInputArg = ToHexString(originalClearMessage); } else { startInputArg = Encoding.ASCII.GetString(originalClearMessage); } } while (startInputArg.StartsWith("-")); } else { originalClearMessage = GenerateRandom(messageLength, false); string startFilename = Utils.EnsureFilename("tests/clear.bin"); #if IN_MEMORY if (InMemoryFile.Exists(startFilename)) { InMemoryFile.Delete(startFilename); } if (startFormat == DataFormat.Base64) { InMemoryFile.WriteAllText(startFilename, Convert.ToBase64String(originalClearMessage)); } else if (startFormat == DataFormat.Hexa) { InMemoryFile.WriteAllText(startFilename, ToHexString(originalClearMessage)); } else { InMemoryFile.WriteAllBytes(startFilename, originalClearMessage); } #else if (File.Exists(startFilename)) { File.Delete(startFilename); } if (startFormat == DataFormat.Base64) { File.WriteAllText(startFilename, Convert.ToBase64String(originalClearMessage)); } else if (startFormat == DataFormat.Hexa) { File.WriteAllText(startFilename, ToHexString(originalClearMessage)); } else { File.WriteAllBytes(startFilename, originalClearMessage); } #endif startInputArg = startFilename; } StringBuilder interStringBuilder = null; if (interType == DataType.Console) { interStringBuilder = new StringBuilder(); Console.SetOut(new StringWriter(interStringBuilder)); } else { string interFilename = Utils.EnsureFilename("tests/encrypted.aes"); #if IN_MEMORY if (InMemoryFile.Exists(interFilename)) { InMemoryFile.Delete(interFilename); } #else if (File.Exists(interFilename)) { File.Delete(interFilename); } #endif startOutputArg = interFilename; } var startArgs = new string[] { "-e", "--input-type", startType.ToString().ToLowerInvariant(), "--input-format", startFormat.ToString().ToUpperInvariant(), "--output-type", interType.ToString().ToLowerInvariant(), "--output-format", interFormat.ToString().ToLowerInvariant(), "--password", password, "--key-salt-length", saltLength.ToString(), "--key-iterations", keyIterations.ToString(), "--key-size", keySize.ToString(), "--skip-bytes", "0", startInputArg, startOutputArg }; new Aes.Program().Run(startArgs); // ================================================== string endInputArg; string endOutputArg = string.Empty; if (interType == DataType.Console) { endInputArg = ConsoleOutStream.TrimConsoleDataMarkers(interStringBuilder.ToString()); } else { endInputArg = Utils.EnsureFilename("tests/encrypted.aes"); } StringBuilder endStringBuilder = null; if (endType == DataType.Console) { endStringBuilder = new StringBuilder(); Console.SetOut(new StringWriter(endStringBuilder)); } else { string endFilename = Utils.EnsureFilename("tests/decrypted.bin"); #if IN_MEMORY if (InMemoryFile.Exists(endFilename)) { InMemoryFile.Delete(endFilename); } #else if (File.Exists(endFilename)) { File.Delete(endFilename); } #endif endOutputArg = endFilename; } var endArgs = new string[] { "-d", "--input-type", interType.ToString().ToLowerInvariant(), "--input-format", interFormat.ToString().ToUpperInvariant(), "--output-type", endType.ToString().ToLowerInvariant(), "--output-format", endFormat.ToString().ToLowerInvariant(), "--password", password, "--key-salt-length", saltLength.ToString(), "--key-iterations", keyIterations.ToString(), "--key-size", keySize.ToString(), "--skip-bytes", "0", endInputArg, endOutputArg }; new Aes.Program().Run(endArgs); byte[] resultMessage = null; if (endType == DataType.Console) { resultMessage = Encoding.ASCII.GetBytes(ConsoleOutStream.TrimConsoleDataMarkers(endStringBuilder.ToString())); } else { #if IN_MEMORY resultMessage = InMemoryFile.ReadAllBytes(Utils.EnsureFilename("tests/decrypted.bin")); #else resultMessage = File.ReadAllBytes(Utils.EnsureFilename("tests/decrypted.bin")); #endif } if (endFormat == DataFormat.Base64) { resultMessage = Convert.FromBase64String(Encoding.ASCII.GetString(resultMessage)); } else if (endFormat == DataFormat.Hexa) { resultMessage = FromHexStringAsBytes(resultMessage); } string info = $"{startType} {interType} {endType}, {startFormat} {interFormat} {endFormat}, {password}, {saltLength}, {keyIterations}, {keySize}, {messageLength}"; if (originalClearMessage.Length != resultMessage.Length) { throw new Exception($"Length mismatch. [{info}]"); } for (int i = 0; i < originalClearMessage.Length; i++) { if (originalClearMessage[i] != resultMessage[i]) { throw new Exception($"Data mismatch at position '{i}'. [{info}]"); } } return(true); }