Helper methods for IKeyIDContainer and IFingerprintContainer.
示例#1
0
 private static OpenPgpSecretKey ParseSecretKey(string[] sec, string[] fpr, string[] uid)
 {
     return(new OpenPgpSecretKey(
                keyID: OpenPgpUtils.ParseKeyID(sec[4]),
                fingerprint: OpenPgpUtils.ParseFingerpint(fpr[9]),
                userID: uid[9]));
 }
        /// <summary>
        /// Returns a specific secret key in the keyring.
        /// </summary>
        /// <param name="openPgp">The <see cref="IOpenPgp"/> implementation.</param>
        /// <param name="keySpecifier">The key ID, fingerprint or any part of a user ID that identifies the keypair; <c>null</c> to use the default key.</param>
        /// <exception cref="KeyNotFoundException">The specified key could not be found on the system.</exception>
        /// <seealso cref="IOpenPgp.Sign"/>
        /// <seealso cref="IOpenPgp.ExportKey"/>
        public static OpenPgpSecretKey GetSecretKey(this IOpenPgp openPgp, string?keySpecifier = null)
        {
            #region Sanity checks
            if (openPgp == null)
            {
                throw new ArgumentNullException(nameof(openPgp));
            }
            #endregion

            var secretKeys = openPgp.ListSecretKeys().ToList();
            if (secretKeys.Count == 0)
            {
                throw new KeyNotFoundException(Resources.UnableToFindSecretKey);
            }

            if (string.IsNullOrEmpty(keySpecifier))
            {
                return(secretKeys[0]);
            }

            try
            {
                long keyID = OpenPgpUtils.ParseKeyID(keySpecifier);
                return(secretKeys.First(x => x.KeyID == keyID));
            }
            catch (FormatException)
            {}
            catch (InvalidOperationException)
            {}

            try
            {
                var fingerprint = OpenPgpUtils.ParseFingerprint(keySpecifier);
                return(secretKeys.First(x => x.GetFingerprint().SequenceEqual(fingerprint)));
            }
            catch (FormatException)
            {}
            catch (InvalidOperationException)
            {}

            try
            {
                return(secretKeys.First(x => x.UserID.ContainsIgnoreCase(keySpecifier)));
            }
            catch
            {
                throw new KeyNotFoundException(Resources.UnableToFindSecretKey);
            }
        }
示例#3
0
        private static OpenPgpSignature ParseSignatureLine([NotNull] string line)
        {
            const int signatureTypeIndex = 1, fingerprintIndex = 2, timestampIndex = 4, keyIDIndex = 2, errorCodeIndex = 7;

            string[] signatureParts = line.Split(' ');
            if (signatureParts.Length < signatureTypeIndex + 1)
            {
                return(null);
            }
            switch (signatureParts[signatureTypeIndex])
            {
            case "VALIDSIG":
                if (signatureParts.Length != 12)
                {
                    throw new FormatException("Incorrect number of columns in VALIDSIG line.");
                }
                var fingerprint = OpenPgpUtils.ParseFingerpint(signatureParts[fingerprintIndex]);
                return(new ValidSignature(
                           keyID: OpenPgpUtils.FingerprintToKeyID(fingerprint),
                           fingerprint: fingerprint,
                           timestamp: FileUtils.FromUnixTime(Int64.Parse(signatureParts[timestampIndex]))));

            case "BADSIG":
                if (signatureParts.Length < 3)
                {
                    throw new FormatException("Incorrect number of columns in BADSIG line.");
                }
                return(new BadSignature(OpenPgpUtils.ParseKeyID(signatureParts[keyIDIndex])));

            case "ERRSIG":
                if (signatureParts.Length != 8)
                {
                    throw new FormatException("Incorrect number of columns in ERRSIG line.");
                }
                int errorCode = Int32.Parse(signatureParts[errorCodeIndex]);
                switch (errorCode)
                {
                case 9:
                    return(new MissingKeySignature(OpenPgpUtils.ParseKeyID(signatureParts[keyIDIndex])));

                default:
                    return(new ErrorSignature(OpenPgpUtils.ParseKeyID(signatureParts[keyIDIndex])));
                }

            default:
                return(null);
            }
        }
 public void TestFingerprintToKeyID()
 => OpenPgpUtils.FingerprintToKeyID(OpenPgpUtils.ParseFingerprint("E91FE1CBFCCF315543F6CB13DEED44B49BE24661"))
 .Should().Be(OpenPgpUtils.ParseKeyID("DEED44B49BE24661"));
 public void TestParseFingerpint()
 => OpenPgpUtils.ParseFingerprint(TestFingerprintString)
 .Should().Equal(TestFingerprint);
 public void TestParseKeyID()
 => OpenPgpUtils.ParseKeyID(TestKeyIDString)
 .Should().Be(TestKeyID);