summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/asn1/Asn1Encodable.cs41
-rw-r--r--crypto/src/asn1/Asn1Object.cs26
-rw-r--r--crypto/src/asn1/Asn1Set.cs2
-rw-r--r--crypto/src/asn1/BERGenerator.cs10
-rw-r--r--crypto/src/asn1/DERSequenceGenerator.cs10
-rw-r--r--crypto/src/asn1/DERSetGenerator.cs10
-rw-r--r--crypto/src/asn1/DerOctetString.cs2
-rw-r--r--crypto/src/asn1/DerSequence.cs2
-rw-r--r--crypto/src/cms/CMSSignedDataGenerator.cs2
-rw-r--r--crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs2
-rw-r--r--crypto/test/src/asn1/test/MiscTest.cs2
-rw-r--r--crypto/test/src/asn1/test/X509NameTest.cs7
12 files changed, 59 insertions, 57 deletions
diff --git a/crypto/src/asn1/Asn1Encodable.cs b/crypto/src/asn1/Asn1Encodable.cs
index f3c0f725f..247b0e0bf 100644
--- a/crypto/src/asn1/Asn1Encodable.cs
+++ b/crypto/src/asn1/Asn1Encodable.cs
@@ -8,41 +8,28 @@ namespace Org.BouncyCastle.Asn1
 		public const string Der = "DER";
 		public const string Ber = "BER";
 
-		public byte[] GetEncoded()
+        public virtual void EncodeTo(Stream output)
         {
-            MemoryStream bOut = new MemoryStream();
-            Asn1OutputStream aOut = Asn1OutputStream.Create(bOut);
+            ToAsn1Object().EncodeTo(output);
+        }
 
-            ToAsn1Object().Encode(aOut);
+        public virtual void EncodeTo(Stream output, string encoding)
+        {
+            ToAsn1Object().EncodeTo(output, encoding);
+        }
 
+		public byte[] GetEncoded()
+        {
+            MemoryStream bOut = new MemoryStream();
+            EncodeTo(bOut);
             return bOut.ToArray();
         }
 
         public byte[] GetEncoded(string encoding)
         {
-            if (encoding.Equals(Der))
-            {
-                MemoryStream bOut = new MemoryStream();
-                Asn1OutputStream dOut = Asn1OutputStream.Create(bOut, Der);
-
-                Asn1Object asn1Object = ToAsn1Object();
-
-                Asn1Set asn1Set = asn1Object as Asn1Set;
-                if (null != asn1Set)
-                {
-                    /*
-                     * NOTE: Even a DerSet isn't necessarily already in sorted order (particularly from DerSetParser),
-                     * so all sets have to be converted here.
-                     */
-                    asn1Object = new DerSet(asn1Set.elements);
-                }
-
-                asn1Object.Encode(dOut);
-
-                return bOut.ToArray();
-            }
-
-            return GetEncoded();
+            MemoryStream bOut = new MemoryStream();
+            EncodeTo(bOut, encoding);
+            return bOut.ToArray();
         }
 
         /**
diff --git a/crypto/src/asn1/Asn1Object.cs b/crypto/src/asn1/Asn1Object.cs
index d98f52b06..1abf0e0bd 100644
--- a/crypto/src/asn1/Asn1Object.cs
+++ b/crypto/src/asn1/Asn1Object.cs
@@ -6,6 +6,32 @@ namespace Org.BouncyCastle.Asn1
     public abstract class Asn1Object
 		: Asn1Encodable
     {
+        public override void EncodeTo(Stream output)
+        {
+            Asn1OutputStream.Create(output).WriteObject(this);
+        }
+
+        public override void EncodeTo(Stream output, string encoding)
+        {
+            Asn1OutputStream asn1Out = Asn1OutputStream.Create(output, encoding);
+            Asn1Object asn1Object = this;
+
+            if (Der.Equals(encoding))
+            {
+                Asn1Set asn1Set = asn1Object as Asn1Set;
+                if (null != asn1Set)
+                {
+                    /*
+                     * NOTE: Even a DerSet isn't necessarily already in sorted order (particularly from DerSetParser),
+                     * so all sets have to be converted here.
+                     */
+                    asn1Object = new DerSet(asn1Set.elements);
+                }
+            }
+
+            asn1Out.WriteObject(asn1Object);
+        }
+
         /// <summary>Create a base ASN.1 object from a byte array.</summary>
         /// <param name="data">The byte array to parse.</param>
         /// <returns>The base ASN.1 object represented by the byte array.</returns>
diff --git a/crypto/src/asn1/Asn1Set.cs b/crypto/src/asn1/Asn1Set.cs
index 68ede2275..8c6e2f0e4 100644
--- a/crypto/src/asn1/Asn1Set.cs
+++ b/crypto/src/asn1/Asn1Set.cs
@@ -279,7 +279,7 @@ namespace Org.BouncyCastle.Asn1
             byte[][] keys = new byte[count][];
             for (int i = 0; i < count; ++i)
             {
-                keys[i] = elements[i].GetEncoded(Asn1Encodable.Der);
+                keys[i] = elements[i].GetEncoded(Der);
             }
             Array.Sort(keys, elements, new DerComparer());
 #endif
diff --git a/crypto/src/asn1/BERGenerator.cs b/crypto/src/asn1/BERGenerator.cs
index f5465b756..0b9c43b13 100644
--- a/crypto/src/asn1/BERGenerator.cs
+++ b/crypto/src/asn1/BERGenerator.cs
@@ -28,16 +28,14 @@ namespace Org.BouncyCastle.Asn1
             _tagNo = tagNo;
         }
 
-		public override void AddObject(
-			Asn1Encodable obj)
+		public override void AddObject(Asn1Encodable obj)
 		{
-			new Asn1OutputStream(Out).WriteObject(obj);
+            obj.EncodeTo(Out);
 		}
 
-        public override void AddObject(
-            Asn1Object obj)
+        public override void AddObject(Asn1Object obj)
         {
-            new Asn1OutputStream(Out).WriteObject(obj);
+            obj.EncodeTo(Out);
         }
 
         public override Stream GetRawOutputStream()
diff --git a/crypto/src/asn1/DERSequenceGenerator.cs b/crypto/src/asn1/DERSequenceGenerator.cs
index d5c8a7a64..12c978508 100644
--- a/crypto/src/asn1/DERSequenceGenerator.cs
+++ b/crypto/src/asn1/DERSequenceGenerator.cs
@@ -21,16 +21,14 @@ namespace Org.BouncyCastle.Asn1
 		{
 		}
 
-		public override void AddObject(
-			Asn1Encodable obj)
+		public override void AddObject(Asn1Encodable obj)
 		{
-            Asn1OutputStream.Create(_bOut, Asn1Encodable.Der).WriteObject(obj);
+            obj.EncodeTo(_bOut, Asn1Encodable.Der);
 		}
 
-        public override void AddObject(
-            Asn1Object obj)
+        public override void AddObject(Asn1Object obj)
         {
-            Asn1OutputStream.Create(_bOut, Asn1Encodable.Der).WriteObject(obj);
+            obj.EncodeTo(_bOut, Asn1Encodable.Der);
         }
 
         public override Stream GetRawOutputStream()
diff --git a/crypto/src/asn1/DERSetGenerator.cs b/crypto/src/asn1/DERSetGenerator.cs
index 6b4d03eec..677241798 100644
--- a/crypto/src/asn1/DERSetGenerator.cs
+++ b/crypto/src/asn1/DERSetGenerator.cs
@@ -21,16 +21,14 @@ namespace Org.BouncyCastle.Asn1
 		{
 		}
 
-		public override void AddObject(
-			Asn1Encodable obj)
+		public override void AddObject(Asn1Encodable obj)
 		{
-            Asn1OutputStream.Create(_bOut, Asn1Encodable.Der).WriteObject(obj);
+            obj.EncodeTo(_bOut, Asn1Encodable.Der);
 		}
 
-        public override void AddObject(
-            Asn1Object obj)
+        public override void AddObject(Asn1Object obj)
         {
-            Asn1OutputStream.Create(_bOut, Asn1Encodable.Der).WriteObject(obj);
+            obj.EncodeTo(_bOut, Asn1Encodable.Der);
         }
 
         public override Stream GetRawOutputStream()
diff --git a/crypto/src/asn1/DerOctetString.cs b/crypto/src/asn1/DerOctetString.cs
index b9659bf84..4dc5bff2c 100644
--- a/crypto/src/asn1/DerOctetString.cs
+++ b/crypto/src/asn1/DerOctetString.cs
@@ -18,7 +18,7 @@ namespace Org.BouncyCastle.Asn1
         }
 
         public DerOctetString(Asn1Encodable obj)
-            : base(obj.GetEncoded(Asn1Encodable.Der))
+            : base(obj.GetEncoded(Der))
         {
         }
 
diff --git a/crypto/src/asn1/DerSequence.cs b/crypto/src/asn1/DerSequence.cs
index 651350d91..9b4504cfe 100644
--- a/crypto/src/asn1/DerSequence.cs
+++ b/crypto/src/asn1/DerSequence.cs
@@ -63,7 +63,7 @@ namespace Org.BouncyCastle.Asn1
 
             // TODO Intermediate buffer could be avoided if we could calculate expected length
             MemoryStream bOut = new MemoryStream();
-            Asn1OutputStream dOut = Asn1OutputStream.Create(bOut, Asn1Encodable.Der);
+            Asn1OutputStream dOut = Asn1OutputStream.Create(bOut, Der);
 
             foreach (Asn1Encodable obj in this)
             {
diff --git a/crypto/src/cms/CMSSignedDataGenerator.cs b/crypto/src/cms/CMSSignedDataGenerator.cs
index beabab911..f2d54baa6 100644
--- a/crypto/src/cms/CMSSignedDataGenerator.cs
+++ b/crypto/src/cms/CMSSignedDataGenerator.cs
@@ -166,7 +166,7 @@ namespace Org.BouncyCastle.Cms
 					signedAttr = outer.GetAttributeSet(signed);
 
                     // sig must be composed from the DER encoding.
-                    Asn1OutputStream.Create(sigStr, Asn1Encodable.Der).WriteObject(signedAttr);
+                    signedAttr.EncodeTo(sigStr, Asn1Encodable.Der);
 				}
                 else if (content != null)
                 {
diff --git a/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs b/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs
index 91329fbd5..8109d656a 100644
--- a/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs
+++ b/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs
@@ -51,7 +51,7 @@ namespace Org.BouncyCastle.Asn1.Tests
             };
 
 			MemoryStream bOut = new MemoryStream();
-            Asn1OutputStream aOut = new Asn1OutputStream(bOut);
+            Asn1OutputStream aOut = Asn1OutputStream.Create(bOut);
 
             for (int i = 0; i != values.Length; i++)
             {
diff --git a/crypto/test/src/asn1/test/MiscTest.cs b/crypto/test/src/asn1/test/MiscTest.cs
index 5c48b6f07..2d005a187 100644
--- a/crypto/test/src/asn1/test/MiscTest.cs
+++ b/crypto/test/src/asn1/test/MiscTest.cs
@@ -93,7 +93,7 @@ namespace Org.BouncyCastle.Asn1.Tests
             byte[] data = Base64.Decode("MA4ECAECAwQFBgcIAgIAgAMCBSAWBWhlbGxvMAoECAECAwQFBgcIFgtodHRwOi8vdGVzdA==");
 
             MemoryStream bOut = new MemoryStream();
-            Asn1OutputStream aOut = new Asn1OutputStream(bOut);
+            Asn1OutputStream aOut = Asn1OutputStream.Create(bOut);
 
             for (int i = 0; i != values.Length; i++)
             {
diff --git a/crypto/test/src/asn1/test/X509NameTest.cs b/crypto/test/src/asn1/test/X509NameTest.cs
index 9a564f72f..43214c0e4 100644
--- a/crypto/test/src/asn1/test/X509NameTest.cs
+++ b/crypto/test/src/asn1/test/X509NameTest.cs
@@ -597,12 +597,7 @@ namespace Org.BouncyCastle.Asn1.Tests
 
             n = new X509Name("C=AU, O=The Legion of the Bouncy Castle, L=Melbourne + OU=Ascot Vale");
 
-            MemoryStream bOut = new MemoryStream();
-            Asn1OutputStream aOut = new Asn1OutputStream(bOut);
-
-            aOut.WriteObject(n);
-
-            byte[] enc2 = bOut.ToArray();
+            byte[] enc2 = n.GetEncoded();
 
             if (!Arrays.AreEqual(enc, enc2))
             {