Skip to content

Commit

Permalink
Merge pull request #34 from mattosaurus/development
Browse files Browse the repository at this point in the history
Handle large files
  • Loading branch information
mattosaurus authored Aug 28, 2019
2 parents c21ef8a + a90f0b6 commit dc7f769
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 75 deletions.
58 changes: 55 additions & 3 deletions PgpCore.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ public void DecryptFile_DecryptEncryptedFile(KeyType keyType)
Teardown();
}

//[Theory]
//[InlineData(KeyType.Generated, FileType.GeneratedLarge)]
//public void DecryptLargeFile_DecryptEncryptedFile(KeyType keyType, FileType fileType)
//{
// // Arrange
// Arrange(keyType, fileType);
// PGP pgp = new PGP();

// // Act
// pgp.EncryptFile(contentFilePath, encryptedContentFilePath, publicKeyFilePath1);
// pgp.DecryptFile(encryptedContentFilePath, decryptedContentFilePath1, privateKeyFilePath1, password1);

// // Assert
// Assert.True(File.Exists(encryptedContentFilePath));
// Assert.True(File.Exists(decryptedContentFilePath1));

// // Teardown
// Teardown();
//}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down Expand Up @@ -826,7 +846,7 @@ public void Verify_DoNotVerifySignedStream(KeyType keyType)
}
#endregion Stream

private void Arrange(KeyType keyType)
private void Arrange(KeyType keyType, FileType fileType = FileType.Known)
{
Directory.CreateDirectory(keyDirectory);
Directory.CreateDirectory(contentDirectory);
Expand Down Expand Up @@ -884,9 +904,35 @@ private void Arrange(KeyType keyType)
}

// Create content file
using (StreamWriter streamWriter = File.CreateText(contentFilePath))
if (fileType == FileType.Known)
{
using (StreamWriter streamWriter = File.CreateText(contentFilePath))
{
streamWriter.WriteLine(content);
}
}
else if (fileType == FileType.GeneratedLarge)
{
CreateRandomFile(contentFilePath, 7000);
}
}

private void CreateRandomFile(string filePath, int sizeInMb)
{
// Note: block size must be a factor of 1MB to avoid rounding errors
const int blockSize = 1024 * 8;
const int blocksPerMb = (1024 * 1024) / blockSize;

byte[] data = new byte[blockSize];
Random rng = new Random();
using (FileStream stream = File.OpenWrite(filePath))
{
streamWriter.WriteLine(content);
// There
for (int i = 0; i < sizeInMb * blocksPerMb; i++)
{
rng.NextBytes(data);
stream.Write(data, 0, data.Length);
}
}
}

Expand Down Expand Up @@ -957,6 +1003,12 @@ public enum KeyType
KnownGpg
}

public enum FileType
{
GeneratedLarge,
Known
}

// Content
const string contentDirectory = "./Content/";
const string content = "The quick brown fox jumps over the lazy dog";
Expand Down
107 changes: 35 additions & 72 deletions PgpCore/PGP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,50 +146,22 @@ public void EncryptFile(
throw new FileNotFoundException(String.Format("Input file [{0}] does not exist.", publicKeyFilePath));
}

using (MemoryStream @out = new MemoryStream())
{
if (CompressionAlgorithm != CompressionAlgorithmTag.Uncompressed)
{
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(CompressionAlgorithm);
PgpUtilities.WriteFileToLiteralData(comData.Open(@out), FileTypeToChar(), new FileInfo(inputFilePath));
comData.Close();
}
else
PgpUtilities.WriteFileToLiteralData(@out, FileTypeToChar(), new FileInfo(inputFilePath));
List<Stream> publicKeyStreams = new List<Stream>();

PgpEncryptedDataGenerator pk = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithm, withIntegrityCheck, new SecureRandom());

foreach (string publicKeyFilePath in publicKeys)
foreach (string publicKeyFilePath in publicKeyFilePaths)
{
MemoryStream memoryStream = new MemoryStream();
using (Stream publicKeyStream = new FileStream(publicKeyFilePath, FileMode.Open))
{
using (Stream pkStream = File.OpenRead(publicKeyFilePath))
{
pk.AddMethod(Utilities.ReadPublicKey(pkStream));
}
}

byte[] bytes = @out.ToArray();

using (Stream outStream = File.Create(outputFilePath))
{
if (armor)
{
using (ArmoredOutputStream armoredStream = new ArmoredOutputStream(outStream))
{
using (Stream armoredOutStream = pk.Open(armoredStream, bytes.Length))
{
armoredOutStream.Write(bytes, 0, bytes.Length);
}
}
}
else
{
using (Stream plainStream = pk.Open(outStream, bytes.Length))
{
plainStream.Write(bytes, 0, bytes.Length);
}
}
publicKeyStream.CopyTo(memoryStream);
memoryStream.Position = 0;
publicKeyStreams.Add(memoryStream);
}
}

using (FileStream inputStream = new FileStream(inputFilePath, FileMode.Open))
using (Stream outputStream = File.Create(outputFilePath))
EncryptStream(inputStream, outputStream, publicKeyStreams, armor, withIntegrityCheck);
}

/// <summary>
Expand Down Expand Up @@ -233,43 +205,34 @@ public void EncryptStream(Stream inputStream, Stream outputStream, IEnumerable<S
throw new ArgumentException("PublicKeyStream");
}

using (MemoryStream @out = new MemoryStream())
if (armor)
{
if (CompressionAlgorithm != CompressionAlgorithmTag.Uncompressed)
{
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(CompressionAlgorithm);
Utilities.WriteStreamToLiteralData(comData.Open(@out), FileTypeToChar(), inputStream, "name");
comData.Close();
}
else
Utilities.WriteStreamToLiteralData(@out, FileTypeToChar(), inputStream, "name");
outputStream = new ArmoredOutputStream(outputStream);
}

PgpEncryptedDataGenerator pk = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithm, withIntegrityCheck, new SecureRandom());
PgpEncryptedDataGenerator pk = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithm, withIntegrityCheck, new SecureRandom());

foreach (Stream publicKey in publicKeys)
{
pk.AddMethod(Utilities.ReadPublicKey(publicKey));
}
foreach (Stream publicKey in publicKeys)
{
pk.AddMethod(Utilities.ReadPublicKey(publicKey));
}

byte[] bytes = @out.ToArray();
Stream @out = pk.Open(outputStream, new byte[1 << 16]);

if (armor)
{
using (ArmoredOutputStream armoredStream = new ArmoredOutputStream(outputStream))
{
using (Stream armoredOutStream = pk.Open(armoredStream, bytes.Length))
{
armoredOutStream.Write(bytes, 0, bytes.Length);
}
}
}
else
{
using (Stream plainStream = pk.Open(outputStream, bytes.Length))
{
plainStream.Write(bytes, 0, bytes.Length);
}
}
if (CompressionAlgorithm != CompressionAlgorithmTag.Uncompressed)
{
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(CompressionAlgorithm);
Utilities.WriteStreamToLiteralData(comData.Open(@out), FileTypeToChar(), inputStream, "name");
comData.Close();
}
else
Utilities.WriteStreamToLiteralData(@out, FileTypeToChar(), inputStream, "name");

@out.Close();

if (armor)
{
outputStream.Close();
}
}

Expand Down

0 comments on commit dc7f769

Please sign in to comment.