summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-04-20 18:42:12 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-04-20 18:42:12 +0700
commit8ab0160708d540e42b086582bd1992f5269f7166 (patch)
tree528bf8667e4db801fe74f888ad51d08f7533ba27
parentRefactoring: reduced allocations (diff)
downloadBouncyCastle.NET-ed25519-8ab0160708d540e42b086582bd1992f5269f7166.tar.xz
Fix missing Dispose(s) of BcpgOutputStream
-rw-r--r--crypto/src/bcpg/ContainedPacket.cs10
-rw-r--r--crypto/src/bcpg/PublicKeyPacket.cs21
-rw-r--r--crypto/src/bcpg/SecretKeyPacket.cs36
3 files changed, 33 insertions, 34 deletions
diff --git a/crypto/src/bcpg/ContainedPacket.cs b/crypto/src/bcpg/ContainedPacket.cs
index e8f387ca4..ca39fd985 100644
--- a/crypto/src/bcpg/ContainedPacket.cs
+++ b/crypto/src/bcpg/ContainedPacket.cs
@@ -10,11 +10,11 @@ namespace Org.BouncyCastle.Bcpg
         public byte[] GetEncoded()
         {
             MemoryStream bOut = new MemoryStream();
-            BcpgOutputStream pOut = new BcpgOutputStream(bOut);
-
-			pOut.WritePacket(this);
-
-			return bOut.ToArray();
+            using (var pOut = new BcpgOutputStream(bOut))
+            {
+                pOut.WritePacket(this);
+            }
+            return bOut.ToArray();
         }
 
 		public abstract void Encode(BcpgOutputStream bcpgOut);
diff --git a/crypto/src/bcpg/PublicKeyPacket.cs b/crypto/src/bcpg/PublicKeyPacket.cs
index 89177ce84..639d5595c 100644
--- a/crypto/src/bcpg/PublicKeyPacket.cs
+++ b/crypto/src/bcpg/PublicKeyPacket.cs
@@ -98,20 +98,19 @@ namespace Org.BouncyCastle.Bcpg
         public virtual byte[] GetEncodedContents()
         {
             MemoryStream bOut = new MemoryStream();
-            BcpgOutputStream pOut = new BcpgOutputStream(bOut);
-
-            pOut.WriteByte((byte) version);
-            pOut.WriteInt((int) time);
-
-            if (version <= 3)
+            using (var pOut = new BcpgOutputStream(bOut))
             {
-                pOut.WriteShort((short) validDays);
-            }
-
-            pOut.WriteByte((byte) algorithm);
+                pOut.WriteByte((byte)version);
+                pOut.WriteInt((int)time);
 
-            pOut.WriteObject((BcpgObject)key);
+                if (version <= 3)
+                {
+                    pOut.WriteShort((short)validDays);
+                }
 
+                pOut.WriteByte((byte)algorithm);
+                pOut.WriteObject((BcpgObject)key);
+            }
             return bOut.ToArray();
         }
 
diff --git a/crypto/src/bcpg/SecretKeyPacket.cs b/crypto/src/bcpg/SecretKeyPacket.cs
index 1bc684a69..9b9ae93a4 100644
--- a/crypto/src/bcpg/SecretKeyPacket.cs
+++ b/crypto/src/bcpg/SecretKeyPacket.cs
@@ -1,5 +1,6 @@
 using System;
 using System.IO;
+using System.Linq;
 
 using Org.BouncyCastle.Utilities;
 
@@ -136,28 +137,27 @@ namespace Org.BouncyCastle.Bcpg
 		public byte[] GetEncodedContents()
         {
             MemoryStream bOut = new MemoryStream();
-            BcpgOutputStream pOut = new BcpgOutputStream(bOut);
-
-            pOut.Write(pubKeyPacket.GetEncodedContents());
-
-			pOut.WriteByte((byte) s2kUsage);
-
-			if (s2kUsage == UsageChecksum || s2kUsage == UsageSha1)
+            using (var pOut = new BcpgOutputStream(bOut))
             {
-                pOut.WriteByte((byte) encAlgorithm);
-                pOut.WriteObject(s2k);
-            }
+                pOut.Write(pubKeyPacket.GetEncodedContents());
+                pOut.WriteByte((byte)s2kUsage);
 
-			if (iv != null)
-            {
-                pOut.Write(iv);
-            }
+                if (s2kUsage == UsageChecksum || s2kUsage == UsageSha1)
+                {
+                    pOut.WriteByte((byte)encAlgorithm);
+                    pOut.WriteObject(s2k);
+                }
 
-            if (secKeyData != null && secKeyData.Length > 0)
-            {
-                pOut.Write(secKeyData);
-            }
+                if (iv != null)
+                {
+                    pOut.Write(iv);
+                }
 
+                if (secKeyData != null && secKeyData.Length > 0)
+                {
+                    pOut.Write(secKeyData);
+                }
+            }
             return bOut.ToArray();
         }