Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions PgpCore.Tests/UnitTests/Decrypt/DecryptSync.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ public void Decrypt_DecryptEncryptedMessage_ShouldDecryptMessage(KeyType keyType
testFactory.Teardown();
}

[Fact]
public void Decrypt_DecryptLargeEncryptedMessage_ShouldDecryptMessage()
{
// Arrange
TestFactory testFactory = new TestFactory();
testFactory.Arrange(KeyType.Generated, FileType.GeneratedLarge);
EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey);
EncryptionKeys decryptionKeys = new EncryptionKeys(testFactory.PrivateKey, testFactory.Password);
PGP pgpEncrypt = new PGP(encryptionKeys);
PGP pgpDecrypt = new PGP(decryptionKeys);

// Act
string encryptedContent = pgpEncrypt.Encrypt(testFactory.Content);
string decryptedContent = pgpDecrypt.Decrypt(encryptedContent);

// Assert
using (new AssertionScope())
{
encryptedContent.Should().NotBeNullOrEmpty();
decryptedContent.Should().NotBeNullOrEmpty();
decryptedContent.Should().Be(testFactory.Content);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
2 changes: 2 additions & 0 deletions PgpCore/Helpers/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ public static async Task WriteStreamToLiteralDataAsync(
using (Stream pOut = lData.Open(output, fileType, name, input.Length, DateTime.Now))
{
await input.CopyToAsync(pOut);
await pOut.FlushAsync();
}
}

Expand All @@ -396,6 +397,7 @@ public static void WriteStreamToLiteralData(
using (Stream pOut = lData.Open(output, fileType, name, input.Length, DateTime.Now))
{
input.CopyTo(pOut);
pOut.Flush();
}
}

Expand Down
4 changes: 4 additions & 0 deletions PgpCore/PGP.EncryptAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,18 @@ public async Task EncryptAsync(
using (Stream compressedStream = new PgpCompressedDataGenerator(CompressionAlgorithm).Open(@out, new byte[1 << 16]))
{
await Utilities.WriteStreamToLiteralDataAsync(compressedStream, FileTypeToChar(), inputStream, name, oldFormat);
await compressedStream.FlushAsync();
}
await outputStream.FlushAsync();
}
else
{
using (Stream @out = pk.Open(outputStream, new byte[1 << 16]))
{
await Utilities.WriteStreamToLiteralDataAsync(@out, FileTypeToChar(), inputStream, name, oldFormat);
await @out.FlushAsync();
}
await outputStream.FlushAsync();
}

if (armor)
Expand Down
4 changes: 4 additions & 0 deletions PgpCore/PGP.EncryptSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,18 @@ public void Encrypt(
using (Stream compressedStream = new PgpCompressedDataGenerator(CompressionAlgorithm).Open(@out, new byte[1 << 16]))
{
Utilities.WriteStreamToLiteralData(compressedStream, FileTypeToChar(), inputStream, name, oldFormat);
compressedStream.Flush();
}
outputStream.Flush();
}
else
{
using (Stream @out = pk.Open(outputStream, new byte[1 << 16]))
{
Utilities.WriteStreamToLiteralData(@out, FileTypeToChar(), inputStream, name, oldFormat);
@out.Flush();
}
outputStream.Flush();
}

if (armor)
Expand Down
44 changes: 30 additions & 14 deletions PgpCore/PGP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace PgpCore
{
public partial class PGP : IPGP
public partial class PGP : IPGP
{
public static PGP Instance => _instance ?? (_instance = new PGP());
private static PGP _instance;
Expand Down Expand Up @@ -68,7 +68,9 @@ await WriteOutputAndSignAsync(compressedOut, literalOut, inputFileStream,
signatureGenerator);
}
}
await compressedOut.FlushAsync();
}
await encryptedOut.FlushAsync();
}
}

Expand All @@ -84,7 +86,9 @@ private async Task OutputEncryptedAsync(Stream inputStream, Stream outputStream,
{
await WriteOutputAndSignAsync(compressedOut, literalOut, inputStream, signatureGenerator);
}
await compressedOut.FlushAsync();
}
await encryptedOut.FlushAsync();
}
}

Expand All @@ -106,7 +110,9 @@ private void OutputEncrypted(FileInfo inputFile, Stream outputStream, bool withI
WriteOutputAndSign(compressedOut, literalOut, inputFileStream, signatureGenerator);
}
}
compressedOut.Flush();
}
encryptedOut.Flush();
}
}

Expand All @@ -121,7 +127,9 @@ private void OutputEncrypted(Stream inputStream, Stream outputStream, bool withI
{
WriteOutputAndSign(compressedOut, literalOut, inputStream, signatureGenerator);
}
compressedOut.Flush();
}
encryptedOut.Flush();
}
}

Expand All @@ -141,6 +149,7 @@ private async Task OutputSignedAsync(FileInfo inputFile, Stream outputStream, st
await WriteOutputAndSignAsync(compressedOut, literalOut, inputFileStream, signatureGenerator);
}
}
await compressedOut.FlushAsync();
}
}

Expand All @@ -149,12 +158,13 @@ private async Task OutputSignedAsync(Stream inputStream, Stream outputStream,
{
using (Stream compressedOut = ChainCompressedOut(outputStream))
{
PgpSignatureGenerator signatureGenerator = InitSignatureGenerator(compressedOut);
using (Stream literalOut = ChainLiteralOut(compressedOut, inputStream, name, oldFormat))
{
await WriteOutputAndSignAsync(compressedOut, literalOut, inputStream, signatureGenerator);
}
}
PgpSignatureGenerator signatureGenerator = InitSignatureGenerator(compressedOut);
using (Stream literalOut = ChainLiteralOut(compressedOut, inputStream, name, oldFormat))
{
await WriteOutputAndSignAsync(compressedOut, literalOut, inputStream, signatureGenerator);
}
await compressedOut.FlushAsync();
}
}

#endregion OutputSignedAsync
Expand All @@ -173,6 +183,7 @@ private void OutputSigned(FileInfo inputFile, Stream outputStream, string name,
WriteOutputAndSign(compressedOut, literalOut, inputFileStream, signatureGenerator);
}
}
compressedOut.Flush();
}
}

Expand All @@ -185,6 +196,7 @@ private void OutputSigned(Stream inputStream, Stream outputStream, string name,
{
WriteOutputAndSign(compressedOut, literalOut, inputStream, signatureGenerator);
}
compressedOut.Flush();
}
}

Expand Down Expand Up @@ -252,7 +264,7 @@ private void OutputClearSigned(FileInfo inputFile, Stream outputStream, IDiction

private void OutputClearSigned(Stream inputStream, Stream outputStream, IDictionary<string, string> headers)
{
using (StreamReader streamReader = new StreamReader(inputStream))
using (StreamReader streamReader = new StreamReader(inputStream))
using (ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream, headers))
{
PgpSignatureGenerator pgpSignatureGenerator = InitClearSignatureGenerator(armoredOutputStream);
Expand Down Expand Up @@ -304,6 +316,7 @@ private async Task WriteOutputAndSignAsync(Stream compressedOut, Stream literalO
signatureGenerator.Update(buf, 0, length);
}

await literalOut.FlushAsync();
signatureGenerator.Generate().Encode(compressedOut);
}

Expand All @@ -318,6 +331,7 @@ private void WriteOutputAndSign(Stream compressedOut, Stream literalOut, FileStr
signatureGenerator.Update(buf, 0, length);
}

literalOut.Flush();
signatureGenerator.Generate().Encode(compressedOut);
}

Expand All @@ -332,6 +346,7 @@ private async Task WriteOutputAndSignAsync(Stream compressedOut, Stream literalO
signatureGenerator.Update(buf, 0, length);
}

await literalOut.FlushAsync();
signatureGenerator.Generate().Encode(compressedOut);
}

Expand All @@ -346,6 +361,7 @@ private void WriteOutputAndSign(Stream compressedOut, Stream literalOut, Stream
signatureGenerator.Update(buf, 0, length);
}

literalOut.Flush();
signatureGenerator.Generate().Encode(compressedOut);
}

Expand Down Expand Up @@ -391,7 +407,7 @@ private Stream ChainLiteralOut(Stream compressedOut, FileInfo file, string name,
{
PgpLiteralDataGenerator pgpLiteralDataGenerator = new PgpLiteralDataGenerator(oldFormat);

return pgpLiteralDataGenerator.Open(compressedOut, FileTypeToChar(), name, file.Length,
return pgpLiteralDataGenerator.Open(compressedOut, FileTypeToChar(), name, file.Length,
DateTime.UtcNow);
}

Expand All @@ -402,11 +418,11 @@ private Stream ChainLiteralOut(Stream compressedOut, Stream inputStream, string
DateTime.UtcNow);
}

#endregion ChainLiteralOut
#endregion ChainLiteralOut

#region InitSignatureGenerator
#region InitSignatureGenerator

private PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut)
private PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut)
{
PublicKeyAlgorithmTag tag = EncryptionKeys.SigningSecretKey.PublicKey.Algorithm;
PgpSignatureGenerator pgpSignatureGenerator = new PgpSignatureGenerator(tag, HashAlgorithmTag);
Expand Down Expand Up @@ -621,9 +637,9 @@ private static void ProcessLine(PgpSignature sig, byte[] line)
public void Dispose()
{ }

# endregion Misc Utilities
#endregion Misc Utilities

#endregion Private helpers

}
}
6 changes: 3 additions & 3 deletions PgpCore/PgpCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<PackageProjectUrl>https://github.com/mattosaurus/PgpCore</PackageProjectUrl>
<RepositoryUrl>https://github.com/mattosaurus/PgpCore</RepositoryUrl>
<PackageTags>PGP .NET Core</PackageTags>
<Version>6.5.2.0</Version>
<Version>6.5.3.0</Version>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<FileVersion>6.5.2</FileVersion>
<PackageReleaseNotes>v6.5.2 - Fix missing bytes</PackageReleaseNotes>
<FileVersion>6.5.3</FileVersion>
<PackageReleaseNotes>v6.5.3 - Fix missing bytes</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand Down