/* * Sanity checks public PGP key, to make sure it's valid for encrypting MIME entities. */ static void SanityCheckCryptographyKey(PgpPublicKey key) { if (!key.IsEncryptionKey) { throw new ArgumentException($"Key with fingerprint of '{PgpHelpers.GetFingerprint(key)}' is not an encryption key"); } if (key.IsRevoked()) { throw new ArgumentException($"Key with fingerprint of '{PgpHelpers.GetFingerprint(key)}' is revoked"); } }
/* * Cryptographically signs an entity. */ static MultipartSigned Sign( MimeEntity entity, string armoredPrivateKey, string keyPassword) { var algo = DigestAlgorithm.Sha256; using (var ctx = new CreatePgpMimeContext { Password = keyPassword }) { return(MultipartSigned.Create( ctx, PgpHelpers.GetSecretKeyFromAsciiArmored(armoredPrivateKey), algo, entity)); } }
/* * Cryptographically signs and encrypts an entity. */ static MultipartEncrypted SignAndEncrypt( MimeEntity entity, Node encryptionNode, string armoredPrivateKey, string keyPassword) { var algo = DigestAlgorithm.Sha256; using (var ctx = new CreatePgpMimeContext { Password = keyPassword }) { return(MultipartEncrypted.SignAndEncrypt( ctx, PgpHelpers.GetSecretKeyFromAsciiArmored(armoredPrivateKey), algo, GetEncryptionKeys(encryptionNode), entity)); } }
/* * Returns all public keys referenced in lambda object, somehow. * Values can exist either as value of node, and/or valuesof children of node given. */ static IEnumerable <PgpPublicKey> GetEncryptionKeys(Node encryptionKey) { // Returning any public encryption key found in value of node first. if (encryptionKey.Value != null) { var result = PgpHelpers.GetPublicKeyFromAsciiArmored(encryptionKey.GetEx <string>()); // Sanity checking key, before returning to caller. SanityCheckCryptographyKey(result); yield return(result); } // Looping through children, in case caller provided a collection of encryption keys, that should all be used. foreach (var idx in encryptionKey.Children) { var result = PgpHelpers.GetPublicKeyFromAsciiArmored(idx.GetEx <string>()); // Sanity checking key, before returning to caller. SanityCheckCryptographyKey(result); yield return(result); } }