-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify on encrypted data searches in correct lists of keys. Additiona…
…l minor refactorings.
- Loading branch information
1 parent
64c46cf
commit fc73229
Showing
5 changed files
with
84 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,45 @@ | ||
using System.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Org.BouncyCastle.Bcpg.OpenPgp; | ||
|
||
namespace PgpCore | ||
{ | ||
/// <summary> | ||
/// A wrapper class for <see cref="PgpPublicKeyRing"/> that also keeps track of a preferred <see cref="PgpPublicKey"/> to be used for encryption. | ||
/// </summary> | ||
public class PgpPublicKeyRingWithPreferredKey | ||
{ | ||
public PgpPublicKeyRing PgpPublicKeyRing { get; set; } | ||
public PgpPublicKey PreferredKey { get; private set; } = null; | ||
public PgpPublicKey PreferredEncryptionKey { get; private set; } = null; | ||
public PgpPublicKey DefaultEncryptionKey => _defaultEncryptionKey.Value; | ||
|
||
private Lazy<PgpPublicKey> _defaultEncryptionKey; | ||
private Lazy<IEnumerable<PgpPublicKey>> _encryptionKeys; | ||
|
||
public PgpPublicKeyRingWithPreferredKey(PgpPublicKeyRing publicKeyRing) | ||
{ | ||
PgpPublicKeyRing = publicKeyRing; | ||
_defaultEncryptionKey = new Lazy<PgpPublicKey>(() => Utilities.FindBestEncryptionKey(PgpPublicKeyRing)); | ||
_encryptionKeys = new Lazy<IEnumerable<PgpPublicKey>>(() => PgpPublicKeyRing.GetPublicKeys().Where(key => key.IsEncryptionKey)); | ||
} | ||
|
||
public PgpPublicKeyRingWithPreferredKey(PgpPublicKeyRing publicKeyRing, long preferredKeyId) | ||
|
||
/// <summary> | ||
/// Try to find the key with the given keyId and set it as the preferred encryption key. | ||
/// If no key is found, the preferred key is not changed. | ||
/// </summary> | ||
/// <param name="keyId">The keyId to find.</param> | ||
public void UsePreferredEncryptionKey(long? keyId) | ||
{ | ||
PgpPublicKeyRing = publicKeyRing; | ||
UseEncryptionKey(preferredKeyId); | ||
PreferredEncryptionKey = _encryptionKeys.Value.FirstOrDefault(key => key.KeyId == keyId) ?? PreferredEncryptionKey; | ||
} | ||
|
||
/// <summary> | ||
/// This method will try to find the key with the given keyId and set it as the preferred key. | ||
/// If it cannot find the key, it will not change the preferred key. | ||
/// Clear the preferred encryption key. | ||
/// </summary> | ||
/// <param name="keyId">The keyId to find.</param> | ||
public void UseEncryptionKey(long keyId) | ||
public void ClearPreferredEncryptionKey() | ||
{ | ||
PreferredKey = PgpPublicKeyRing.GetPublicKeys().FirstOrDefault(key => key.KeyId == keyId && key.IsEncryptionKey) ?? PreferredKey; | ||
PreferredEncryptionKey = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters