Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-s committed Feb 22, 2021
1 parent eb4c86a commit 49f7004
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/QnapBackupDecryptor.Core.Tests/DecryptorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions src/QnapBackupDecryptor.Core/OpenSsl.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace QnapBackupDecryptor.Core
Expand Down Expand Up @@ -108,11 +109,11 @@ private static Result<FileInfo> 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<FileInfo>.OkResult(outputFile);
}
Expand All @@ -129,5 +130,17 @@ private static Result<FileInfo> 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;
}
}
}

0 comments on commit 49f7004

Please sign in to comment.