summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-05-20 19:14:46 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-05-20 19:14:46 +0700
commit4b9f92f40910434d9227d6cfae3bc003614c0ff0 (patch)
tree0d864b55cdbb7334dec4970063d71fad86ca3fe1
parentPicnic followup changes (diff)
downloadBouncyCastle.NET-ed25519-4b9f92f40910434d9227d6cfae3bc003614c0ff0.tar.xz
Add constructors allowing optional version header
- see https://github.com/bcgit/bc-csharp/issues/533
-rw-r--r--crypto/src/bcpg/ArmoredOutputStream.cs34
1 files changed, 22 insertions, 12 deletions
diff --git a/crypto/src/bcpg/ArmoredOutputStream.cs b/crypto/src/bcpg/ArmoredOutputStream.cs
index 03d4a1a91..0f4d410b3 100644
--- a/crypto/src/bcpg/ArmoredOutputStream.cs
+++ b/crypto/src/bcpg/ArmoredOutputStream.cs
@@ -6,7 +6,6 @@ using System.Reflection;
 
 using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Utilities;
-using Org.BouncyCastle.Utilities.Collections;
 using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Bcpg
@@ -145,24 +144,35 @@ namespace Org.BouncyCastle.Bcpg
 
         private static readonly string Version = CreateVersion();
 
-        private readonly IDictionary<string, IList<string>> m_headers;
+        private readonly Dictionary<string, List<string>> m_headers;
 
         public ArmoredOutputStream(Stream outStream)
+            : this(outStream, true)
+        {
+        }
+
+        public ArmoredOutputStream(Stream outStream, bool addVersionHeader)
         {
             this.outStream = outStream;
-            this.m_headers = new Dictionary<string, IList<string>>(1);
-            SetHeader(HeaderVersion, Version);
+            this.m_headers = new Dictionary<string, List<string>>();
+
+            if (addVersionHeader)
+            {
+                SetHeader(HeaderVersion, Version);
+            }
         }
 
         public ArmoredOutputStream(Stream outStream, IDictionary<string, string> headers)
-            : this(outStream)
+            : this(outStream, headers, true)
+        {
+        }
+
+        public ArmoredOutputStream(Stream outStream, IDictionary<string, string> headers, bool addVersionHeader)
+            : this(outStream, addVersionHeader && !headers.ContainsKey(HeaderVersion))
         {
             foreach (var header in headers)
             {
-                var headerList = new List<string>(1);
-                headerList.Add(header.Value);
-
-                m_headers[header.Key] = headerList;
+                m_headers.Add(header.Key, new List<string> { header.Value });
             }
         }
 
@@ -219,13 +229,13 @@ namespace Org.BouncyCastle.Bcpg
          */
         public void ResetHeaders()
         {
-            var versions = CollectionUtilities.GetValueOrNull(m_headers, HeaderVersion);
+            bool hadVersion = m_headers.TryGetValue(HeaderVersion, out var version);
 
             m_headers.Clear();
 
-            if (versions != null)
+            if (hadVersion)
             {
-                m_headers[HeaderVersion] = versions;
+                m_headers.Add(HeaderVersion, version);
             }
         }