Skip to content

Commit

Permalink
Merge pull request #260 from mattosaurus/bug/254-verify-error
Browse files Browse the repository at this point in the history
Bug/254 verify error
  • Loading branch information
mattosaurus authored Dec 8, 2023
2 parents 735f311 + 2291b73 commit fd3bab1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
25 changes: 25 additions & 0 deletions PgpCore.Tests/UnitTests/UnitTestsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion PgpCore/PGP.VerifyAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public async Task<bool> 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();
Expand Down
3 changes: 2 additions & 1 deletion PgpCore/PGP.VerifySync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -56,6 +57,64 @@ using (PGP pgp = new PGP())
pgp.GenerateKey(@"C:\TEMP\Keys\public.asc", @"C:\TEMP\Keys\private.asc", "[email protected]", "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.

Expand Down

0 comments on commit fd3bab1

Please sign in to comment.