diff --git a/PgpCore.Tests/UnitTests/UnitTestsAsync.cs b/PgpCore.Tests/UnitTests/UnitTestsAsync.cs index 21ac4cc..c2ce062 100644 --- a/PgpCore.Tests/UnitTests/UnitTestsAsync.cs +++ b/PgpCore.Tests/UnitTests/UnitTestsAsync.cs @@ -884,6 +884,31 @@ public async Task VerifyFileAsync_ThrowIfEncrypted() testFactory.Teardown(); } } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task VerifyAsync_VerifyAndReadSignedFile(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgp = new PGP(encryptionKeys); + + // Act + await pgp.SignAsync(testFactory.ContentFileInfo, testFactory.SignedContentFileInfo); + bool verified = await pgp.VerifyAsync(testFactory.SignedContentFileInfo, testFactory.DecryptedContentFileInfo); + + // Assert + Assert.True(testFactory.SignedContentFileInfo.Exists); + Assert.True(testFactory.DecryptedContentFileInfo.Exists); + Assert.True(verified); + + // Teardown + testFactory.Teardown(); + } #endregion File - FileInfo #region Stream diff --git a/PgpCore/PGP.VerifyAsync.cs b/PgpCore/PGP.VerifyAsync.cs index 9e9f5ba..319ac43 100644 --- a/PgpCore/PGP.VerifyAsync.cs +++ b/PgpCore/PGP.VerifyAsync.cs @@ -63,8 +63,9 @@ public async Task VerifyAsync(Stream inputStream, Stream outputStream = nu if (outputStream == null) outputStream = new MemoryStream(); - using (StreamWriter contentStreamWriter = new StreamWriter(outputStream, outputStream.GetEncoding(), 1024, true)) + using (StreamWriter contentStreamWriter = new StreamWriter(outputStream, inputStream.GetEncoding(), 1024, true)) { + inputStream.Seek(0, SeekOrigin.Begin); Stream encodedFile = PgpUtilities.GetDecoderStream(inputStream); PgpObjectFactory factory = new PgpObjectFactory(encodedFile); PgpObject pgpObject = factory.NextPgpObject(); diff --git a/PgpCore/PGP.VerifySync.cs b/PgpCore/PGP.VerifySync.cs index 7cfd2db..303e85f 100644 --- a/PgpCore/PGP.VerifySync.cs +++ b/PgpCore/PGP.VerifySync.cs @@ -61,8 +61,9 @@ public bool Verify(Stream inputStream, Stream outputStream = null, bool throwIfE if (outputStream == null) outputStream = new MemoryStream(); - using (StreamWriter contentStreamWriter = new StreamWriter(outputStream, outputStream.GetEncoding(), 1024, true)) + using (StreamWriter contentStreamWriter = new StreamWriter(outputStream, inputStream.GetEncoding(), 1024, true)) { + inputStream.Seek(0, SeekOrigin.Begin); Stream encodedFile = PgpUtilities.GetDecoderStream(inputStream); PgpObjectFactory factory = new PgpObjectFactory(encodedFile); PgpObject pgpObject = factory.NextPgpObject(); diff --git a/README.md b/README.md index 0ae4878..3176075 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ If you want a (basic) example of how you can use an Azure Function to encrypt/de ## Methods * [Generate Key](#generate-key) +* [Inspect](#inspect) * [Encrypt](#encrypt) * [Sign](#sign) * [Clear Sign](#clear-sign) @@ -56,6 +57,64 @@ using (PGP pgp = new PGP()) pgp.GenerateKey(@"C:\TEMP\Keys\public.asc", @"C:\TEMP\Keys\private.asc", "email@email.com", "password"); } ``` +#### Inspect +Inspect the provided file, stream or string to determine if it is encrypted or signed. + +[`gpg --list-packets "C:\TEMP\Content\encrypted.pgp"`](https://www.gnupg.org/gph/en/manual/x135.html) +### Inspect File +```C# +// Load keys +FileInfo publicKey = new FileInfo(@"C:\TEMP\Keys\public.asc"); +FileInfo privateKey = new FileInfo(@"C:\TEMP\Keys\private.asc"); +EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey, privateKey, "password"); + +// Reference input file +FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\encrypted.pgp"); + +// Inspect +PGP pgp = new PGP(); +PgpInspectResult result = await pgp.InspectAsync(inputFile); +``` +### Inspect File +```C# +// Load keys +FileInfo publicKey = new FileInfo(@"C:\TEMP\Keys\public.asc"); +FileInfo privateKey = new FileInfo(@"C:\TEMP\Keys\private.asc"); +EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey, privateKey, "password"); + +// Reference input file +FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\encrypted.pgp"); + +// Inspect +PGP pgp = new PGP(); +PgpInspectResult result = await pgp.InspectAsync(inputFile); +``` +### Inspect Stream +```C# +// Load keys +EncryptionKeys encryptionKeys; +using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open)) +using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open)) + encryptionKeys = new EncryptionKeys(publicKeyStream, privateKeyStream, "password"); + +PGP pgp = new PGP(encryptionKeys); + +// Reference input stream +using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encrypted.pgp", FileMode.Open)) + // Inspect + PgpInspectResult result = await pgp.InspectAsync(inputFileStream); +``` +### Inspect String +```C# +// Load keys +string publicKey = File.ReadAllText(@"C:\TEMP\Keys\public.asc"); +string privatyeKey = File.ReadAllText(@"C:\TEMP\Keys\private.asc"); +EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey, privateKey, "password"); + +// Inspect +PGP pgp = new PGP(encryptionKeys); +PgpInspectResult result = await pgp.InspectAsync("String to inspect"); +``` ### Encrypt Encrypt the provided file, stream or string using a public key.