summary refs log tree commit diff
path: root/crypto/src/bcpg
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2017-06-09 12:36:38 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2017-06-09 12:36:38 +0700
commit53a508e381f21061c8c6f49f8c19c703faab2a38 (patch)
tree1bd521559947120efe44077bd6615d8682e2ab5d /crypto/src/bcpg
parentAdd explicit limit for sequence numbers (diff)
downloadBouncyCastle.NET-ed25519-53a508e381f21061c8c6f49f8c19c703faab2a38.tar.xz
Improved header control in ArmoredOutputStream
- in particular, supports removing the default version header
Diffstat (limited to 'crypto/src/bcpg')
-rw-r--r--crypto/src/bcpg/ArmoredOutputStream.cs47
1 files changed, 33 insertions, 14 deletions
diff --git a/crypto/src/bcpg/ArmoredOutputStream.cs b/crypto/src/bcpg/ArmoredOutputStream.cs
index 1f0e412d8..dae6b98e5 100644
--- a/crypto/src/bcpg/ArmoredOutputStream.cs
+++ b/crypto/src/bcpg/ArmoredOutputStream.cs
@@ -20,6 +20,8 @@ namespace Org.BouncyCastle.Bcpg
     public class ArmoredOutputStream
         : BaseOutputStream
     {
+        public static readonly string HeaderVersion = "Version";
+
         private static readonly byte[] encodingTable =
         {
             (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
@@ -101,35 +103,43 @@ namespace Org.BouncyCastle.Bcpg
         private static readonly string    footerStart = "-----END PGP ";
         private static readonly string    footerTail = "-----";
 
-        private static readonly string version = "BCPG C# v" + AssemblyInfo.Version;
+        private static readonly string Version = "BCPG C# v" + AssemblyInfo.Version;
 
         private readonly IDictionary headers;
 
         public ArmoredOutputStream(Stream outStream)
         {
             this.outStream = outStream;
-            this.headers = Platform.CreateHashtable();
-            this.headers["Version"] = version;
+            this.headers = Platform.CreateHashtable(1);
+            this.headers.Add(HeaderVersion, Version);
         }
 
         public ArmoredOutputStream(Stream outStream, IDictionary headers)
         {
             this.outStream = outStream;
             this.headers = Platform.CreateHashtable(headers);
-            this.headers["Version"] = version;
+            if (!this.headers.Contains(HeaderVersion))
+            {
+                this.headers.Add(HeaderVersion, Version);
+            }
         }
 
         /**
-         * Set an additional header entry.
+         * Set an additional header entry. A null value will clear the entry for name.
          *
          * @param name the name of the header entry.
          * @param v the value of the header entry.
          */
-        public void SetHeader(
-            string name,
-            string v)
+        public void SetHeader(string name, string v)
         {
-            headers[name] = v;
+            if (v == null)
+            {
+                headers.Remove(name);
+            }
+            else
+            {
+                headers[name] = v;
+            }
         }
 
         /**
@@ -137,8 +147,14 @@ namespace Org.BouncyCastle.Bcpg
          */
         public void ResetHeaders()
         {
+            string version = (string)headers[HeaderVersion];
+
             headers.Clear();
-            headers["Version"] = version;
+
+            if (version != null)
+            {
+                headers[HeaderVersion] = Version;
+            }
         }
 
         /**
@@ -248,14 +264,17 @@ namespace Org.BouncyCastle.Bcpg
                 }
 
                 DoWrite(headerStart + type + headerTail + nl);
-                WriteHeaderEntry("Version", (string) headers["Version"]);
+                if (headers.Contains(HeaderVersion))
+                {
+                    WriteHeaderEntry(HeaderVersion, (string)headers[HeaderVersion]);
+                }
 
                 foreach (DictionaryEntry de in headers)
                 {
-                    string k = (string) de.Key;
-                    if (k != "Version")
+                    string k = (string)de.Key;
+                    if (k != HeaderVersion)
                     {
-                        string v = (string) de.Value;
+                        string v = (string)de.Value;
                         WriteHeaderEntry(k, v);
                     }
                 }