Skip to content

Commit

Permalink
Merge pull request #37 from mattosaurus/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
mattosaurus authored Sep 18, 2019
2 parents 0ff8aab + 02281cd commit 68485fb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
63 changes: 60 additions & 3 deletions PgpCore.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using Org.BouncyCastle.Bcpg;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Xunit;

Expand All @@ -26,9 +27,7 @@ public void GenerateKey_CreatePublicPrivateKeyFiles()

#region File
[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
[MemberData(nameof(KeyTypeValues))]
public void EncryptFile_CreateEncryptedFile(KeyType keyType)
{
// Arrange
Expand All @@ -45,6 +44,25 @@ public void EncryptFile_CreateEncryptedFile(KeyType keyType)
Teardown();
}

[Theory]
[MemberData(nameof(HashAlgorithmTagValues))]
public void EncryptFile_CreateEncryptedFileWithDifferentHashAlgorithms(HashAlgorithmTag hashAlgorithmTag)
{
// Arrange
Arrange(KeyType.Known);
PGP pgp = new PGP();
pgp.HashAlgorithmTag = hashAlgorithmTag;

// Act
pgp.EncryptFile(contentFilePath, encryptedContentFilePath, publicKeyFilePath1);

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

// Teardown
Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down Expand Up @@ -159,6 +177,29 @@ public void DecryptFile_DecryptEncryptedFile(KeyType keyType)
Teardown();
}

[Theory]
[MemberData(nameof(HashAlgorithmTagValues))]
public void DecryptFile_DecryptEncryptedFileWithDifferentHashAlgorithms(HashAlgorithmTag hashAlgorithmTag)
{
// Arrange
Arrange(KeyType.Known);
PGP pgp = new PGP();
pgp.HashAlgorithmTag = hashAlgorithmTag;

// Act
pgp.EncryptFile(contentFilePath, encryptedContentFilePath, publicKeyFilePath1);
pgp.DecryptFile(encryptedContentFilePath, decryptedContentFilePath1, privateKeyFilePath1, password1);
string decryptedContent = File.ReadAllText(decryptedContentFilePath1);

// Assert
Assert.True(File.Exists(encryptedContentFilePath));
Assert.True(File.Exists(decryptedContentFilePath1));
Assert.Equal(content, decryptedContent.Trim());

// Teardown
Teardown();
}

//[Theory]
//[InlineData(KeyType.Generated, FileType.GeneratedLarge)]
//public void DecryptLargeFile_DecryptEncryptedFile(KeyType keyType, FileType fileType)
Expand Down Expand Up @@ -1006,6 +1047,22 @@ public enum KeyType
KnownGpg
}

public static IEnumerable<object[]> KeyTypeValues()
{
foreach (var keyType in Enum.GetValues(typeof(KeyType)))
{
yield return new object[] { keyType };
}
}

public static IEnumerable<object[]> HashAlgorithmTagValues()
{
foreach (var hashAlgorithmTag in Enum.GetValues(typeof(HashAlgorithmTag)))
{
yield return new object[] { hashAlgorithmTag };
}
}

public enum FileType
{
GeneratedLarge,
Expand Down
9 changes: 8 additions & 1 deletion PgpCore/PGP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public PGPFileType FileType
set;
}

public HashAlgorithmTag HashAlgorithmTag
{
get;
set;
}

#region Constructor

public PGP()
Expand All @@ -65,6 +71,7 @@ public PGP()
PgpSignatureType = PgpSignature.DefaultCertification;
PublicKeyAlgorithm = PublicKeyAlgorithmTag.RsaGeneral;
FileType = PGPFileType.Binary;
HashAlgorithmTag = HashAlgorithmTag.Sha1;
}

#endregion Constructor
Expand Down Expand Up @@ -526,7 +533,7 @@ private Stream ChainLiteralStreamOut(Stream compressedOut, Stream inputStream, s
private PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut, EncryptionKeys encryptionKeys)
{
PublicKeyAlgorithmTag tag = encryptionKeys.SecretKey.PublicKey.Algorithm;
PgpSignatureGenerator pgpSignatureGenerator = new PgpSignatureGenerator(tag, HashAlgorithmTag.Sha1);
PgpSignatureGenerator pgpSignatureGenerator = new PgpSignatureGenerator(tag, HashAlgorithmTag);
pgpSignatureGenerator.InitSign(PgpSignature.BinaryDocument, encryptionKeys.PrivateKey);
foreach (string userId in encryptionKeys.SecretKey.PublicKey.GetUserIds())
{
Expand Down
7 changes: 5 additions & 2 deletions PgpCore/PgpCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
<PackageProjectUrl>https://github.com/mattosaurus/PgpCore</PackageProjectUrl>
<RepositoryUrl>https://github.com/mattosaurus/PgpCore</RepositoryUrl>
<PackageTags>PGP .NET Core</PackageTags>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.0.0</FileVersion>
<PackageReleaseNotes>v2.0.0 - Updated to be async first and to better handle large files</PackageReleaseNotes>
<PackageReleaseNotes>v2.1.0 - Add HashAlgorithmTag option to constructor and strongly sign package.</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>PgpCoreKey.pfx</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 68485fb

Please sign in to comment.