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])); }
public static OpenPgpSecretKey GetSecretKey([NotNull] this IOpenPgp openPgp, [CanBeNull] 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 { var keyID = OpenPgpUtils.ParseKeyID(keySpecifier); return(secretKeys.First(x => x.KeyID == keyID)); } catch (FormatException) {} catch (InvalidOperationException) {} try { var fingerprint = OpenPgpUtils.ParseFingerpint(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); } }
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.ParseFingerpint("E91FE1CBFCCF315543F6CB13DEED44B49BE24661")) .Should().Be(OpenPgpUtils.ParseKeyID("DEED44B49BE24661")); }
public void TestParseFingerrpint() { OpenPgpUtils.ParseFingerpint(TestFingerprintString) .Should().Equal(TestFingerprint); }