Skip to content

Commit 49f7004

Browse files
committed
Refactoring
1 parent eb4c86a commit 49f7004

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/QnapBackupDecryptor.Core.Tests/DecryptorTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,28 @@ public class DecryptorTests
1111
{
1212
private const string PASSWORD = "wisLUBIMyBNcnvo3eDMS";
1313

14+
[Test]
15+
public void OpenSSLDecrypt_ValidPassword_OkResult()
16+
{
17+
// Arrange
18+
var encryptedFile = new FileInfo(Path.Combine("TestFiles", "encrypted.txt"));
19+
var outputFile = new FileInfo("decrypted.txt");
20+
21+
// Act
22+
var passwordBytes = Encoding.UTF8.GetBytes(PASSWORD);
23+
var sslDecrypt = OpenSsl.Decrypt(encryptedFile, passwordBytes, outputFile);
24+
25+
// Assert
26+
sslDecrypt.IsSuccess.ShouldBeTrue();
27+
}
28+
1429

1530
[Test]
1631
public void OpenSSLDecrypt_Text()
1732
{
1833
// Arrange
1934
var encryptedFile = new FileInfo(Path.Combine("TestFiles", "encrypted.txt"));
20-
var outputFile = new FileInfo(Path.Combine("TestFiles", "decrypted.txt"));
35+
var outputFile = new FileInfo("decrypted.txt");
2136

2237
// Act
2338
var passwordBytes = Encoding.UTF8.GetBytes(PASSWORD);

src/QnapBackupDecryptor.Core/OpenSsl.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Runtime.InteropServices;
45
using System.Security.Cryptography;
56

67
namespace QnapBackupDecryptor.Core
@@ -108,11 +109,11 @@ private static Result<FileInfo> DecryptFile(FileInfo encryptedFile, byte[] key,
108109
using (var destination = outputFile.OpenWrite())
109110
using (var cryptoStream = new CryptoStream(encryptedFileStream, decryptor, CryptoStreamMode.Read))
110111
{
111-
outputFile.Attributes |= FileAttributes.Hidden;
112+
HideFile(outputFile);
112113
cryptoStream.CopyTo(destination);
113114
}
114115

115-
outputFile.Attributes -= FileAttributes.Hidden;
116+
ShowFile(outputFile);
116117

117118
return Result<FileInfo>.OkResult(outputFile);
118119
}
@@ -129,5 +130,17 @@ private static Result<FileInfo> DecryptFile(FileInfo encryptedFile, byte[] key,
129130
rijndaelManaged.Dispose();
130131
}
131132
}
133+
134+
private static void HideFile(FileSystemInfo file)
135+
{
136+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
137+
file.Attributes |= FileAttributes.Hidden;
138+
}
139+
140+
private static void ShowFile(FileSystemInfo file)
141+
{
142+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
143+
file.Attributes -= FileAttributes.Hidden;
144+
}
132145
}
133146
}

0 commit comments

Comments
 (0)