Beispiel #1
0
        public static PemFormat Parse(string typeString)
        {
            var format      = new PemFormat(typeString);
            var knownFormat = GetKnownFormats().FirstOrDefault(x => x.Equals(format));

            if (knownFormat == null)
            {
                throw new InvalidOperationException($"Unknown format: {typeString}");
            }
            return(knownFormat);
        }
Beispiel #2
0
        private static PemFormat ExtractFormat(string headerOrFooter, bool isFooter)
        {
            var beginOrEnd = isFooter ? "END" : "BEGIN";
            var match      = Regex.Match(headerOrFooter, $@"({beginOrEnd})\s+(?<format>[^-]+)", RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                throw new InvalidOperationException($"Unrecognized {beginOrEnd}: {headerOrFooter}");
            }
            return(PemFormat.Parse(match.Groups["format"].Value.Trim()));
        }
Beispiel #3
0
        private void Write(DerAsnType der, PemFormat format)
        {
            var derBytes = DerConvert.Encode(der);

            var derBase64 = Convert.ToBase64String(derBytes);

            var pem = new StringBuilder();

            pem.Append(format.Header + "\n");
            for (var i = 0; i < derBase64.Length; i += _maximumLineLength)
            {
                pem.Append(derBase64.Substring(i, Math.Min(_maximumLineLength, derBase64.Length - i)));
                pem.Append("\n");
            }
            pem.Append(format.Footer + "\n");

            using (var writer = new StreamWriter(_stream, _encoding, 4096, true))
                writer.Write(pem.ToString());
        }