Updating
This commit is contained in:
+132
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using Org.BouncyCastle.Bcpg;
|
||||
using Org.BouncyCastle.Bcpg.OpenPgp;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Generators;
|
||||
using Org.BouncyCastle.Crypto.Prng;
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace LCU.API.Internal.FileOperations.IntegrationTests
|
||||
{
|
||||
class FileDataEncryptionTests
|
||||
{
|
||||
private const string TestPassword = "password";
|
||||
private const string PrivateKeyFile = "TestPrivateKey";
|
||||
private const string PublicKeyFile = "TestPublicKey";
|
||||
private const string TestFile = "TestTextFile.txt";
|
||||
private const string TestFileContents = "This text will be saved to a temporary test file for encryption/comparison to resulting files from this integration test";
|
||||
private const string TestEncryptedFile = "TestTextFile.pgp";
|
||||
private const string TestDecryptedFile = "TestTextFile-Decrypted";
|
||||
private readonly string _basePath;
|
||||
private readonly EncryptFileDataService _encryptFileDataService;
|
||||
private readonly DecryptFileDataService _decryptFileDataService;
|
||||
private readonly ServicesFactoryConfiguration _servicesFactoryConfiguration;
|
||||
|
||||
FileDataEncryptionTests()
|
||||
{
|
||||
_encryptFileDataService = new EncryptFileDataService(_servicesFactoryConfiguration);
|
||||
_decryptFileDataService = new DecryptFileDataService(_servicesFactoryConfiguration);
|
||||
_basePath = Path.Combine(Directory.GetCurrentDirectory(), "TempFileFolder");
|
||||
Directory.CreateDirectory(_basePath);
|
||||
GenerateTestKeys("[email protected]", TestPassword, Path.Combine(_basePath, PrivateKeyFile), Path.Combine(_basePath, PublicKeyFile));
|
||||
File.WriteAllText(Path.Combine(_basePath, TestFile), TestFileContents);
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
var fileDataEncryptionTests = new FileDataEncryptionTests();
|
||||
|
||||
var encryptFileDataRequest = new EncryptFileDataRequest
|
||||
{
|
||||
SourcePath = fileDataEncryptionTests._basePath,
|
||||
SourceFile = TestFile,
|
||||
KeySourcePath = fileDataEncryptionTests._basePath,
|
||||
KeySourceFile = PublicKeyFile,
|
||||
DestinationPath = fileDataEncryptionTests._basePath
|
||||
};
|
||||
|
||||
var decryptFileDataRequest = new DecryptFileDataRequest
|
||||
{
|
||||
SourcePath = fileDataEncryptionTests._basePath,
|
||||
SourceFile = TestEncryptedFile,
|
||||
KeySourcePath = fileDataEncryptionTests._basePath,
|
||||
KeySourceFile = PrivateKeyFile,
|
||||
DestinationPath = fileDataEncryptionTests._basePath,
|
||||
DestinationFile = TestDecryptedFile,
|
||||
PassPhrase = TestPassword
|
||||
};
|
||||
|
||||
var encryptResult = fileDataEncryptionTests._encryptFileDataService.PerformRequest(encryptFileDataRequest);
|
||||
var decryptResult = fileDataEncryptionTests._decryptFileDataService.PerformRequest(decryptFileDataRequest);
|
||||
|
||||
Console.WriteLine($"Encrypted Request Is Valid: {encryptResult.IsSuccessful}");
|
||||
Console.WriteLine($"Decrypted Request Is Valid: {decryptResult.IsSuccessful}");
|
||||
|
||||
var encryptedFileContents = File.ReadAllText(Path.Combine(fileDataEncryptionTests._basePath, TestEncryptedFile));
|
||||
var decryptedFileContents = File.ReadAllText(Path.Combine(fileDataEncryptionTests._basePath, TestDecryptedFile));
|
||||
|
||||
Console.WriteLine($"Test File Contents: {TestFileContents}");
|
||||
Console.WriteLine($"Encrypted File Contents: {encryptedFileContents}");
|
||||
Console.WriteLine($"Decrypted File Contents: {decryptedFileContents}");
|
||||
|
||||
Console.WriteLine("Complete");
|
||||
}
|
||||
|
||||
private static void GenerateTestKeys(string identity, string passPhrase, string secretKeyOutputPath, string publicKeyOutputPath)
|
||||
{
|
||||
var keyPairGenerator = new RsaKeyPairGenerator();
|
||||
keyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(new CryptoApiRandomGenerator()), 1024));
|
||||
var keyPair = keyPairGenerator.GenerateKeyPair();
|
||||
|
||||
using var secretOut = new FileInfo(secretKeyOutputPath).OpenWrite();
|
||||
using var publicOut = new FileInfo(publicKeyOutputPath).OpenWrite();
|
||||
ExportGeneratedKeyPair(secretOut, publicOut, keyPair.Public, keyPair.Private, identity, passPhrase.ToCharArray(), true);
|
||||
}
|
||||
|
||||
private static void ExportGeneratedKeyPair(
|
||||
Stream secretOut,
|
||||
Stream publicOut,
|
||||
AsymmetricKeyParameter publicKey,
|
||||
AsymmetricKeyParameter privateKey,
|
||||
string identity,
|
||||
char[] passPhrase,
|
||||
bool armor)
|
||||
{
|
||||
if (armor)
|
||||
{
|
||||
secretOut = new ArmoredOutputStream(secretOut);
|
||||
}
|
||||
|
||||
var pgpSecretKey = new PgpSecretKey
|
||||
(
|
||||
PgpSignature.DefaultCertification,
|
||||
PublicKeyAlgorithmTag.RsaGeneral,
|
||||
publicKey,
|
||||
privateKey,
|
||||
DateTime.Now,
|
||||
identity,
|
||||
SymmetricKeyAlgorithmTag.Cast5,
|
||||
passPhrase,
|
||||
null,
|
||||
null,
|
||||
new SecureRandom()
|
||||
);
|
||||
|
||||
pgpSecretKey.Encode(secretOut);
|
||||
secretOut.Close();
|
||||
|
||||
if (armor)
|
||||
{
|
||||
publicOut = new ArmoredOutputStream(publicOut);
|
||||
}
|
||||
|
||||
var pgpPublicKey = pgpSecretKey.PublicKey;
|
||||
pgpPublicKey.Encode(publicOut);
|
||||
publicOut.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LCU.Library.Internal.FileOperationsModels\LCU.Library.Internal.FileOperationsModels.csproj" />
|
||||
<ProjectReference Include="..\LCU.Api.Internal.FileOperations\LCU.Api.Internal.FileOperations.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user