summary refs log tree commit diff
path: root/crypto/src/bcpg
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 21:37:59 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 21:37:59 +0700
commit98c8cbf49384796d55cd05a766e2429d8a4262d2 (patch)
treed7e195fc5835e03398ee5a670829470d7633623d /crypto/src/bcpg
parentRefactoring around Stream signers (diff)
downloadBouncyCastle.NET-ed25519-98c8cbf49384796d55cd05a766e2429d8a4262d2.tar.xz
Cleanup Stream disposing in Bcpg
Diffstat (limited to 'crypto/src/bcpg')
-rw-r--r--crypto/src/bcpg/OnePassSignaturePacket.cs20
-rw-r--r--crypto/src/bcpg/PublicKeyEncSessionPacket.cs30
-rw-r--r--crypto/src/bcpg/SignaturePacket.cs71
-rw-r--r--crypto/src/bcpg/SymmetricKeyEncSessionPacket.cs21
4 files changed, 59 insertions, 83 deletions
diff --git a/crypto/src/bcpg/OnePassSignaturePacket.cs b/crypto/src/bcpg/OnePassSignaturePacket.cs
index b67df0a52..70f0a2207 100644
--- a/crypto/src/bcpg/OnePassSignaturePacket.cs
+++ b/crypto/src/bcpg/OnePassSignaturePacket.cs
@@ -71,21 +71,15 @@ namespace Org.BouncyCastle.Bcpg
 			get { return keyId; }
 		}
 
-		public override void Encode(
-			BcpgOutputStream bcpgOut)
+		public override void Encode(BcpgOutputStream bcpgOut)
 		{
 			MemoryStream bOut = new MemoryStream();
-			BcpgOutputStream pOut = new BcpgOutputStream(bOut);
-
-			pOut.Write(
-				(byte) version,
-				(byte) sigType,
-				(byte) hashAlgorithm,
-				(byte) keyAlgorithm);
-
-			pOut.WriteLong(keyId);
-
-			pOut.WriteByte((byte) nested);
+			using (var pOut = new BcpgOutputStream(bOut))
+            {
+				pOut.Write((byte)version, (byte)sigType, (byte)hashAlgorithm, (byte)keyAlgorithm);
+				pOut.WriteLong(keyId);
+				pOut.WriteByte((byte)nested);
+			}
 
 			bcpgOut.WritePacket(PacketTag.OnePassSignature, bOut.ToArray(), true);
 		}
diff --git a/crypto/src/bcpg/PublicKeyEncSessionPacket.cs b/crypto/src/bcpg/PublicKeyEncSessionPacket.cs
index 831b5a189..f0b1577ec 100644
--- a/crypto/src/bcpg/PublicKeyEncSessionPacket.cs
+++ b/crypto/src/bcpg/PublicKeyEncSessionPacket.cs
@@ -90,26 +90,22 @@ namespace Org.BouncyCastle.Bcpg
 			return data;
 		}
 
-        public override void Encode(
-			BcpgOutputStream bcpgOut)
+        public override void Encode(BcpgOutputStream bcpgOut)
 		{
 			MemoryStream bOut = new MemoryStream();
-			BcpgOutputStream pOut = new BcpgOutputStream(bOut);
-
-			pOut.WriteByte((byte) version);
-
-			pOut.WriteLong(keyId);
-
-			pOut.WriteByte((byte)algorithm);
-
-            for (int i = 0; i < data.Length; ++i)
-            {
-                pOut.Write(data[i]);
-            }
-
-            Platform.Dispose(pOut);
+			using (var pOut = new BcpgOutputStream(bOut))
+			{
+				pOut.WriteByte((byte)version);
+				pOut.WriteLong(keyId);
+				pOut.WriteByte((byte)algorithm);
+
+				for (int i = 0; i < data.Length; ++i)
+				{
+					pOut.Write(data[i]);
+				}
+			}
 
-            bcpgOut.WritePacket(PacketTag.PublicKeyEncryptedSession , bOut.ToArray(), true);
+			bcpgOut.WritePacket(PacketTag.PublicKeyEncryptedSession, bOut.ToArray(), true);
 		}
 	}
 }
diff --git a/crypto/src/bcpg/SignaturePacket.cs b/crypto/src/bcpg/SignaturePacket.cs
index ca11ba9f4..3256ee35e 100644
--- a/crypto/src/bcpg/SignaturePacket.cs
+++ b/crypto/src/bcpg/SignaturePacket.cs
@@ -384,55 +384,44 @@ namespace Org.BouncyCastle.Bcpg
             get { return creationTime; }
         }
 
-		public override void Encode(
-            BcpgOutputStream bcpgOut)
+		public override void Encode(BcpgOutputStream bcpgOut)
         {
             MemoryStream bOut = new MemoryStream();
-            BcpgOutputStream pOut = new BcpgOutputStream(bOut);
-
-			pOut.WriteByte((byte) version);
-
-			if (version == 3 || version == 2)
+            using (var pOut = new BcpgOutputStream(bOut))
             {
-				pOut.Write(
-					5, // the length of the next block
-					(byte) signatureType);
-
-				pOut.WriteInt((int)(creationTime / 1000L));
+                pOut.WriteByte((byte)version);
 
-				pOut.WriteLong(keyId);
-
-				pOut.Write(
-					(byte) keyAlgorithm,
-					(byte) hashAlgorithm);
-            }
-            else if (version == 4)
-            {
-                pOut.Write(
-					(byte) signatureType,
-					(byte) keyAlgorithm,
-					(byte) hashAlgorithm);
+                if (version == 3 || version == 2)
+                {
+                    byte nextBlockLength = 5;
+                    pOut.Write(nextBlockLength, (byte)signatureType);
+                    pOut.WriteInt((int)(creationTime / 1000L));
+                    pOut.WriteLong(keyId);
+                    pOut.Write((byte)keyAlgorithm, (byte)hashAlgorithm);
+                }
+                else if (version == 4)
+                {
+                    pOut.Write((byte)signatureType, (byte)keyAlgorithm, (byte)hashAlgorithm);
+                    EncodeLengthAndData(pOut, GetEncodedSubpackets(hashedData));
+                    EncodeLengthAndData(pOut, GetEncodedSubpackets(unhashedData));
+                }
+                else
+                {
+                    throw new IOException("unknown version: " + version);
+                }
 
-				EncodeLengthAndData(pOut, GetEncodedSubpackets(hashedData));
+                pOut.Write(fingerprint);
 
-				EncodeLengthAndData(pOut, GetEncodedSubpackets(unhashedData));
-            }
-            else
-            {
-                throw new IOException("unknown version: " + version);
+                if (signature != null)
+                {
+                    pOut.WriteObjects(signature);
+                }
+                else
+                {
+                    pOut.Write(signatureEncoding);
+                }
             }
 
-			pOut.Write(fingerprint);
-
-			if (signature != null)
-			{
-				pOut.WriteObjects(signature);
-			}
-			else
-			{
-				pOut.Write(signatureEncoding);
-			}
-
 			bcpgOut.WritePacket(PacketTag.Signature, bOut.ToArray(), true);
         }
 
diff --git a/crypto/src/bcpg/SymmetricKeyEncSessionPacket.cs b/crypto/src/bcpg/SymmetricKeyEncSessionPacket.cs
index 0381fa386..85bcc2874 100644
--- a/crypto/src/bcpg/SymmetricKeyEncSessionPacket.cs
+++ b/crypto/src/bcpg/SymmetricKeyEncSessionPacket.cs
@@ -68,21 +68,18 @@ namespace Org.BouncyCastle.Bcpg
 			get { return version; }
         }
 
-        public override void Encode(
-            BcpgOutputStream bcpgOut)
+        public override void Encode(BcpgOutputStream bcpgOut)
         {
             MemoryStream bOut = new MemoryStream();
-            BcpgOutputStream pOut = new BcpgOutputStream(bOut);
+            using (var pOut = new BcpgOutputStream(bOut))
+            {
+                pOut.Write((byte)version, (byte)encAlgorithm);
+                pOut.WriteObject(s2k);
 
-            pOut.Write(
-				(byte) version,
-				(byte) encAlgorithm);
-
-			pOut.WriteObject(s2k);
-
-			if (secKeyData != null && secKeyData.Length > 0)
-			{
-                pOut.Write(secKeyData);
+                if (secKeyData != null && secKeyData.Length > 0)
+                {
+                    pOut.Write(secKeyData);
+                }
             }
 
 			bcpgOut.WritePacket(PacketTag.SymmetricKeyEncryptedSessionKey, bOut.ToArray(), true);