Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/254 verify error #260

Merged
merged 2 commits into from
Dec 8, 2023
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
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@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.

Expand Down