From 49f70049caffe9ead47b32b227a0408b66f49177 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 22 Feb 2021 11:44:06 +0900 Subject: [PATCH] Refactoring --- .../DecryptorTests.cs | 17 ++++++++++++++++- src/QnapBackupDecryptor.Core/OpenSsl.cs | 17 +++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/QnapBackupDecryptor.Core.Tests/DecryptorTests.cs b/src/QnapBackupDecryptor.Core.Tests/DecryptorTests.cs index d014a81..a662845 100644 --- a/src/QnapBackupDecryptor.Core.Tests/DecryptorTests.cs +++ b/src/QnapBackupDecryptor.Core.Tests/DecryptorTests.cs @@ -11,13 +11,28 @@ public class DecryptorTests { private const string PASSWORD = "wisLUBIMyBNcnvo3eDMS"; + [Test] + public void OpenSSLDecrypt_ValidPassword_OkResult() + { + // Arrange + var encryptedFile = new FileInfo(Path.Combine("TestFiles", "encrypted.txt")); + var outputFile = new FileInfo("decrypted.txt"); + + // Act + var passwordBytes = Encoding.UTF8.GetBytes(PASSWORD); + var sslDecrypt = OpenSsl.Decrypt(encryptedFile, passwordBytes, outputFile); + + // Assert + sslDecrypt.IsSuccess.ShouldBeTrue(); + } + [Test] public void OpenSSLDecrypt_Text() { // Arrange var encryptedFile = new FileInfo(Path.Combine("TestFiles", "encrypted.txt")); - var outputFile = new FileInfo(Path.Combine("TestFiles", "decrypted.txt")); + var outputFile = new FileInfo("decrypted.txt"); // Act var passwordBytes = Encoding.UTF8.GetBytes(PASSWORD); diff --git a/src/QnapBackupDecryptor.Core/OpenSsl.cs b/src/QnapBackupDecryptor.Core/OpenSsl.cs index 5e3728c..8c42659 100644 --- a/src/QnapBackupDecryptor.Core/OpenSsl.cs +++ b/src/QnapBackupDecryptor.Core/OpenSsl.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Runtime.InteropServices; using System.Security.Cryptography; namespace QnapBackupDecryptor.Core @@ -108,11 +109,11 @@ private static Result DecryptFile(FileInfo encryptedFile, byte[] key, using (var destination = outputFile.OpenWrite()) using (var cryptoStream = new CryptoStream(encryptedFileStream, decryptor, CryptoStreamMode.Read)) { - outputFile.Attributes |= FileAttributes.Hidden; + HideFile(outputFile); cryptoStream.CopyTo(destination); } - outputFile.Attributes -= FileAttributes.Hidden; + ShowFile(outputFile); return Result.OkResult(outputFile); } @@ -129,5 +130,17 @@ private static Result DecryptFile(FileInfo encryptedFile, byte[] key, rijndaelManaged.Dispose(); } } + + private static void HideFile(FileSystemInfo file) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + file.Attributes |= FileAttributes.Hidden; + } + + private static void ShowFile(FileSystemInfo file) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + file.Attributes -= FileAttributes.Hidden; + } } }