public static void ImportEncryptedPem <TPass>( ReadOnlySpan <char> input, ReadOnlySpan <TPass> password, ImportEncryptedKeyAction <TPass> importAction) { bool foundEncryptedPem = false; PemFields foundFields = default; ReadOnlySpan <char> foundSlice = default; ReadOnlySpan <char> pem = input; while (PemEncoding.TryFind(pem, out PemFields fields)) { ReadOnlySpan <char> label = pem[fields.Label]; if (label.SequenceEqual(PemLabels.EncryptedPkcs8PrivateKey)) { if (foundEncryptedPem) { throw new ArgumentException(SR.Argument_PemImport_AmbiguousPem, nameof(input)); } foundEncryptedPem = true; foundFields = fields; foundSlice = pem; } Index offset = fields.Location.End; pem = pem[offset..];
protected override PemFields FindPem(ReadOnlySpan <char> input) { bool found = PemEncoding.TryFind(input, out PemFields fields); Assert.True(found, "Did not find PEM."); return(fields); }
private static byte[] DecodePemData(ReadOnlySpan <char> pemData) { PemEncoding.TryFind(pemData, out PemFields fields); byte[] decodedData = new byte[fields.DecodedDataLength]; if (!Convert.TryFromBase64Chars(pemData[fields.Base64Data], decodedData, out _)) { throw new Exception("Error decoding pem data"); } return(decodedData); }
protected override void AssertNoPemFound(ReadOnlySpan <char> input) { bool found = PemEncoding.TryFind(input, out _); Assert.False(found, "Found PEM when not expected"); }