Beispiel #1
0
        /// <inheritdoc/>
        public byte[] Sign(byte[] data, OpenPgpSecretKey secretKey, string?passphrase = null)
        {
            #region Sanity checks
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (secretKey == null)
            {
                throw new ArgumentNullException(nameof(secretKey));
            }
            #endregion

            var pgpSecretKey = SecretBundle.GetSecretKey(secretKey.KeyID);
            if (pgpSecretKey == null)
            {
                throw new KeyNotFoundException("Specified OpenPGP key not found on system");
            }
            var pgpPrivateKey = GetPrivateKey(pgpSecretKey, passphrase);

            var signatureGenerator = new PgpSignatureGenerator(pgpSecretKey.PublicKey.Algorithm, HashAlgorithmTag.Sha1);
            signatureGenerator.InitSign(PgpSignature.BinaryDocument, pgpPrivateKey);
            signatureGenerator.Update(data);
            return(signatureGenerator.Generate().GetEncoded());
        }
    /// <inheritdoc/>
    public string ExportKey(IKeyIDContainer keyIDContainer)
    {
        #region Sanity checks
        if (keyIDContainer == null)
        {
            throw new ArgumentNullException(nameof(keyIDContainer));
        }
        #endregion

        var publicKey = SecretBundle.GetSecretKey(keyIDContainer.KeyID)?.PublicKey ?? PublicBundle.GetPublicKey(keyIDContainer.KeyID);
        if (publicKey == null)
        {
            throw new KeyNotFoundException("Specified OpenPGP key not found on system");
        }

        var output = new MemoryStream();
        using (var armored = new ArmoredOutputStream(output))
            publicKey.Encode(armored);
        return(output.ReadToString(Encoding.ASCII).Replace(Environment.NewLine, "\n"));
    }