From c7ec1eb8607b77803578de81931837834753c833 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 9 Nov 2022 12:52:58 +0700 Subject: IStreamGenerator changes - go back to Close (but Obsolete it) - callers should just Dispose any opened Stream --- crypto/src/openpgp/IStreamGenerator.cs | 3 +- crypto/src/openpgp/PgpCompressedDataGenerator.cs | 34 ++--- crypto/src/openpgp/PgpEncryptedDataGenerator.cs | 70 +++++----- crypto/src/openpgp/PgpLiteralDataGenerator.cs | 26 +--- crypto/src/openpgp/WrappedGeneratorStream.cs | 4 +- crypto/test/src/openpgp/test/DSA2Test.cs | 32 ++--- crypto/test/src/openpgp/test/PGPCompressionTest.cs | 17 ++- crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs | 87 +++++++------ crypto/test/src/openpgp/test/PGPDSATest.cs | 115 ++++++++-------- crypto/test/src/openpgp/test/PGPPBETest.cs | 145 +++++++++++---------- crypto/test/src/openpgp/test/PGPPacketTest.cs | 19 ++- crypto/test/src/openpgp/test/PGPRSATest.cs | 11 +- crypto/test/src/openpgp/test/PGPSignatureTest.cs | 92 +++++++------ 13 files changed, 321 insertions(+), 334 deletions(-) diff --git a/crypto/src/openpgp/IStreamGenerator.cs b/crypto/src/openpgp/IStreamGenerator.cs index df8ff75da..03df37158 100644 --- a/crypto/src/openpgp/IStreamGenerator.cs +++ b/crypto/src/openpgp/IStreamGenerator.cs @@ -3,7 +3,8 @@ using System; namespace Org.BouncyCastle.Bcpg.OpenPgp { public interface IStreamGenerator - : IDisposable { + [Obsolete("Dispose any opened Stream directly")] + void Close(); } } diff --git a/crypto/src/openpgp/PgpCompressedDataGenerator.cs b/crypto/src/openpgp/PgpCompressedDataGenerator.cs index 271dfac7a..ecd09fd4f 100644 --- a/crypto/src/openpgp/PgpCompressedDataGenerator.cs +++ b/crypto/src/openpgp/PgpCompressedDataGenerator.cs @@ -148,33 +148,21 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } } - #region IDisposable - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - protected virtual void Dispose(bool disposing) - { - if (disposing) + [Obsolete("Dispose any opened Stream directly")] + public void Close() + { + if (dOut != null) { - if (dOut != null) + if (dOut != pkOut) { - if (dOut != pkOut) - { - dOut.Dispose(); - } - dOut = null; - - pkOut.Finish(); - pkOut.Flush(); - pkOut = null; + dOut.Dispose(); } + dOut = null; + + pkOut.Finish(); + pkOut.Flush(); + pkOut = null; } } - - #endregion } } diff --git a/crypto/src/openpgp/PgpEncryptedDataGenerator.cs b/crypto/src/openpgp/PgpEncryptedDataGenerator.cs index b9473a32c..29d90c6fa 100644 --- a/crypto/src/openpgp/PgpEncryptedDataGenerator.cs +++ b/crypto/src/openpgp/PgpEncryptedDataGenerator.cs @@ -600,55 +600,43 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp return Open(outStr, 0, buffer); } - #region IDisposable - - public void Dispose() + [Obsolete("Dispose any opened Stream directly")] + public void Close() { - Dispose(true); - GC.SuppressFinalize(this); - } - - protected virtual void Dispose(bool disposing) - { - if (disposing) + if (cOut != null) { - if (cOut != null) + // TODO Should this all be under the try/catch block? + if (digestOut != null) { - // TODO Should this all be under the try/catch block? - if (digestOut != null) - { - // - // hand code a mod detection packet - // - BcpgOutputStream bOut = new BcpgOutputStream( - digestOut, PacketTag.ModificationDetectionCode, 20); - - bOut.Flush(); - digestOut.Flush(); - - // TODO - byte[] dig = DigestUtilities.DoFinal(digestOut.WriteDigest); - cOut.Write(dig, 0, dig.Length); - } + // + // hand code a mod detection packet + // + BcpgOutputStream bOut = new BcpgOutputStream( + digestOut, PacketTag.ModificationDetectionCode, 20); - cOut.Flush(); + bOut.Flush(); + digestOut.Flush(); - try - { - pOut.Write(c.DoFinal()); - pOut.Finish(); - } - catch (Exception e) - { - throw new IOException(e.Message, e); - } + // TODO + byte[] dig = DigestUtilities.DoFinal(digestOut.WriteDigest); + cOut.Write(dig, 0, dig.Length); + } + + cOut.Flush(); - cOut = null; - pOut = null; + try + { + pOut.Write(c.DoFinal()); + pOut.Finish(); + } + catch (Exception e) + { + throw new IOException(e.Message, e); } + + cOut = null; + pOut = null; } } - - #endregion } } diff --git a/crypto/src/openpgp/PgpLiteralDataGenerator.cs b/crypto/src/openpgp/PgpLiteralDataGenerator.cs index fd59c1ebe..8fbed23d5 100644 --- a/crypto/src/openpgp/PgpLiteralDataGenerator.cs +++ b/crypto/src/openpgp/PgpLiteralDataGenerator.cs @@ -162,27 +162,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp return Open(outStr, format, file.Name, file.Length, file.LastWriteTime); } - #region IDisposable - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - protected virtual void Dispose(bool disposing) - { - if (disposing) + [Obsolete("Dispose any opened Stream directly")] + public void Close() + { + if (pkOut != null) { - if (pkOut != null) - { - pkOut.Finish(); - pkOut.Flush(); - pkOut = null; - } + pkOut.Finish(); + pkOut.Flush(); + pkOut = null; } } - - #endregion } } diff --git a/crypto/src/openpgp/WrappedGeneratorStream.cs b/crypto/src/openpgp/WrappedGeneratorStream.cs index c985a7a05..c973d0b02 100644 --- a/crypto/src/openpgp/WrappedGeneratorStream.cs +++ b/crypto/src/openpgp/WrappedGeneratorStream.cs @@ -20,7 +20,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp { if (disposing) { - m_generator.Dispose(); +#pragma warning disable CS0618 // Type or member is obsolete + m_generator.Close(); +#pragma warning restore CS0618 // Type or member is obsolete } Detach(disposing); diff --git a/crypto/test/src/openpgp/test/DSA2Test.cs b/crypto/test/src/openpgp/test/DSA2Test.cs index dc7dffe81..d4827b232 100644 --- a/crypto/test/src/openpgp/test/DSA2Test.cs +++ b/crypto/test/src/openpgp/test/DSA2Test.cs @@ -130,21 +130,20 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests DateTime modificationTime = DateTimeUtilities.UnixMsToDateTime( DateTimeUtilities.CurrentUnixMs() / 1000 * 1000); - Stream lOut = lGen.Open( + using (var lOut = lGen.Open( new UncloseableStream(bcOut), PgpLiteralData.Binary, "_CONSOLE", dataBytes.Length, - modificationTime); - - int ch; - while ((ch = testIn.ReadByte()) >= 0) + modificationTime)) { - lOut.WriteByte((byte)ch); - sGen.Update((byte)ch); - } - - lGen.Dispose(); + int ch; + while ((ch = testIn.ReadByte()) >= 0) + { + lOut.WriteByte((byte)ch); + sGen.Update((byte)ch); + } + } sGen.Generate().Encode(bcOut); @@ -165,12 +164,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests ops.InitVerify(pubRing.GetPublicKey()); - while ((ch = dIn.ReadByte()) >= 0) { - ops.Update((byte)ch); - } - - PgpSignatureList p3 = (PgpSignatureList)pgpFact.NextPgpObject(); + int ch; + while ((ch = dIn.ReadByte()) >= 0) + { + ops.Update((byte)ch); + } + } + + PgpSignatureList p3 = (PgpSignatureList)pgpFact.NextPgpObject(); PgpSignature sig = p3[0]; Assert.AreEqual(digest, sig.HashAlgorithm); diff --git a/crypto/test/src/openpgp/test/PGPCompressionTest.cs b/crypto/test/src/openpgp/test/PGPCompressionTest.cs index 21f1616af..4654975f9 100644 --- a/crypto/test/src/openpgp/test/PGPCompressionTest.cs +++ b/crypto/test/src/openpgp/test/PGPCompressionTest.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Reflection.Emit; using System.Text; using NUnit.Framework; @@ -78,17 +79,21 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests } else { - cPacket.Dispose(); - } +#pragma warning disable CS0618 // Type or member is obsolete + cPacket.Close(); +#pragma warning restore CS0618 // Type or member is obsolete + } - ValidateData(data, bOut.ToArray()); + ValidateData(data, bOut.ToArray()); try { os.Dispose(); - cPacket.Dispose(); - } - catch (Exception) +#pragma warning disable CS0618 // Type or member is obsolete + cPacket.Close(); +#pragma warning restore CS0618 // Type or member is obsolete + } + catch (Exception) { Fail("Redundant Close() should be ignored"); } diff --git a/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs b/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs index a5de29622..b7eee22b2 100644 --- a/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs +++ b/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs @@ -131,42 +131,41 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.InitSign(PgpSignature.BinaryDocument, pgpPrivKey); - PgpCompressedDataGenerator cGen = new PgpCompressedDataGenerator( - CompressionAlgorithmTag.Zip); - - BcpgOutputStream bcOut = new BcpgOutputStream( - cGen.Open(new UncloseableStream(bOut))); - - sGen.GenerateOnePassVersion(false).Encode(bcOut); - - PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); - Stream lOut = lGen.Open( - new UncloseableStream(bcOut), - PgpLiteralData.Binary, - "_CONSOLE", - dataBytes.Length, - modificationTime); + PgpCompressedDataGenerator cGen = new PgpCompressedDataGenerator( + CompressionAlgorithmTag.Zip); - int ch; - while ((ch = testIn.ReadByte()) >= 0) + using (var cGenOut = cGen.Open(new UncloseableStream(bOut))) { - lOut.WriteByte((byte) ch); - sGen.Update((byte) ch); - } - - lGen.Dispose(); + BcpgOutputStream bcOut = new BcpgOutputStream(cGenOut); + + sGen.GenerateOnePassVersion(false).Encode(bcOut); + + PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); + + using (var lOut = lGen.Open( + new UncloseableStream(bcOut), + PgpLiteralData.Binary, + "_CONSOLE", + dataBytes.Length, + modificationTime)) + { + int ch; + while ((ch = testIn.ReadByte()) >= 0) + { + lOut.WriteByte((byte)ch); + sGen.Update((byte)ch); + } + } - sGen.Generate().Encode(bcOut); + sGen.Generate().Encode(bcOut); + } - cGen.Dispose(); - - // - // verify Generated signature - // - pgpFact = new PgpObjectFactory(bOut.ToArray()); + // + // verify Generated signature + // + pgpFact = new PgpObjectFactory(bOut.ToArray()); PgpCompressedData c1 = (PgpCompressedData)pgpFact.NextPgpObject(); @@ -182,16 +181,19 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests Fail("Modification time not preserved"); } - Stream dIn = p2.GetInputStream(); + Stream dIn = p2.GetInputStream(); ops.InitVerify(pubKey); - while ((ch = dIn.ReadByte()) >= 0) - { - ops.Update((byte)ch); - } + { + int ch; + while ((ch = dIn.ReadByte()) >= 0) + { + ops.Update((byte)ch); + } + } - PgpSignatureList p3 = (PgpSignatureList)pgpFact.NextPgpObject(); + PgpSignatureList p3 = (PgpSignatureList)pgpFact.NextPgpObject(); if (!ops.Verify(p3[0])) { @@ -317,13 +319,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests // ops.InitVerify(pgpPub.GetPublicKey()); - while ((ch = inLd.ReadByte()) >= 0) { - ops.Update((byte) ch); - bOut.WriteByte((byte) ch); - } + int ch; + while ((ch = inLd.ReadByte()) >= 0) + { + ops.Update((byte)ch); + bOut.WriteByte((byte)ch); + } + } - p3 = (PgpSignatureList)pgpFact.NextPgpObject(); + p3 = (PgpSignatureList)pgpFact.NextPgpObject(); if (!ops.Verify(p3[0])) { diff --git a/crypto/test/src/openpgp/test/PGPDSATest.cs b/crypto/test/src/openpgp/test/PGPDSATest.cs index 357753a3c..e11b8bb18 100644 --- a/crypto/test/src/openpgp/test/PGPDSATest.cs +++ b/crypto/test/src/openpgp/test/PGPDSATest.cs @@ -296,37 +296,37 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.SetHashedSubpackets(spGen.Generate()); + DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); + PgpCompressedDataGenerator cGen = new PgpCompressedDataGenerator( CompressionAlgorithmTag.Zip); - BcpgOutputStream bcOut = new BcpgOutputStream(cGen.Open(new UncloseableStream(bOut))); - - sGen.GenerateOnePassVersion(false).Encode(bcOut); + using (var cGenOut = cGen.Open(new UncloseableStream(bOut))) + { + BcpgOutputStream bcOut = new BcpgOutputStream(cGenOut); - PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); + sGen.GenerateOnePassVersion(false).Encode(bcOut); - DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); + PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - Stream lOut = lGen.Open( - new UncloseableStream(bcOut), - PgpLiteralData.Binary, - "_CONSOLE", - dataBytes.Length, - modificationTime); + using (var lOut = lGen.Open( + new UncloseableStream(bcOut), + PgpLiteralData.Binary, + "_CONSOLE", + dataBytes.Length, + modificationTime)) + { + int ch; + while ((ch = testIn.ReadByte()) >= 0) + { + lOut.WriteByte((byte)ch); + sGen.Update((byte)ch); + } + } - int ch; - while ((ch = testIn.ReadByte()) >= 0) - { - lOut.WriteByte((byte) ch); - sGen.Update((byte)ch); + sGen.Generate().Encode(bcOut); } - lGen.Dispose(); - - sGen.Generate().Encode(bcOut); - - cGen.Dispose(); - PgpObjectFactory pgpFact = new PgpObjectFactory(bOut.ToArray()); PgpCompressedData c1 = (PgpCompressedData)pgpFact.NextPgpObject(); @@ -345,12 +345,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests ops.InitVerify(pgpPubKey); - while ((ch = dIn.ReadByte()) >= 0) { - ops.Update((byte) ch); + int ch; + while ((ch = dIn.ReadByte()) >= 0) + { + ops.Update((byte)ch); + } } - PgpSignatureList p3 = (PgpSignatureList) pgpFact.NextPgpObject(); + PgpSignatureList p3 = (PgpSignatureList) pgpFact.NextPgpObject(); if (!ops.Verify(p3[0])) { @@ -392,10 +395,12 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests ops.InitVerify(pubKey); - int ch; - while ((ch = dIn.ReadByte()) >= 0) { - ops.Update((byte) ch); + int ch; + while ((ch = dIn.ReadByte()) >= 0) + { + ops.Update((byte) ch); + } } PgpSignatureList p3 = (PgpSignatureList) pgpFact.NextPgpObject(); @@ -422,35 +427,36 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.InitSign(PgpSignature.CanonicalTextDocument, pgpPrivKey); - PgpCompressedDataGenerator cGen = new PgpCompressedDataGenerator( - CompressionAlgorithmTag.Zip); - - BcpgOutputStream bcOut = new BcpgOutputStream(cGen.Open(new UncloseableStream(bOut))); - - sGen.GenerateOnePassVersion(false).Encode(bcOut); + DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); - PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); + PgpCompressedDataGenerator cGen = new PgpCompressedDataGenerator( + CompressionAlgorithmTag.Zip); - DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); - - Stream lOut = lGen.Open( - new UncloseableStream(bcOut), - PgpLiteralData.Text, - "_CONSOLE", - dataBytes.Length, - modificationTime); - - while ((ch = testIn.ReadByte()) >= 0) + using (var cGenOut = cGen.Open(new UncloseableStream(bOut))) { - lOut.WriteByte((byte) ch); - sGen.Update((byte)ch); - } + BcpgOutputStream bcOut = new BcpgOutputStream(cGenOut); - lGen.Dispose(); + sGen.GenerateOnePassVersion(false).Encode(bcOut); - sGen.Generate().Encode(bcOut); + PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - cGen.Dispose(); + using (var lOut = lGen.Open( + new UncloseableStream(bcOut), + PgpLiteralData.Text, + "_CONSOLE", + dataBytes.Length, + modificationTime)) + { + int ch; + while ((ch = testIn.ReadByte()) >= 0) + { + lOut.WriteByte((byte)ch); + sGen.Update((byte)ch); + } + } + + sGen.Generate().Encode(bcOut); + } // // verify Generated signature - canconical text @@ -475,9 +481,12 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests ops.InitVerify(pubKey); - while ((ch = dIn.ReadByte()) >= 0) { - ops.Update((byte)ch); + int ch; + while ((ch = dIn.ReadByte()) >= 0) + { + ops.Update((byte)ch); + } } p3 = (PgpSignatureList) pgpFact.NextPgpObject(); diff --git a/crypto/test/src/openpgp/test/PGPPBETest.cs b/crypto/test/src/openpgp/test/PGPPBETest.cs index db7782144..85d6b7bff 100644 --- a/crypto/test/src/openpgp/test/PGPPBETest.cs +++ b/crypto/test/src/openpgp/test/PGPPBETest.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Reflection.Emit; using System.Text; using NUnit.Framework; @@ -12,7 +13,7 @@ using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests { - [TestFixture] + [TestFixture] public class PgpPbeTest : SimpleTest { @@ -147,21 +148,22 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator( CompressionAlgorithmTag.Zip); - PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator(); - Stream comOut = comData.Open(new UncloseableStream(bOut)); - Stream ldOut = lData.Open( - new UncloseableStream(comOut), - PgpLiteralData.Binary, - PgpLiteralData.Console, - text.Length, - ModificationTime); - - ldOut.Write(text, 0, text.Length); - ldOut.Close(); - - comOut.Close(); + using (var comOut = comData.Open(new UncloseableStream(bOut))) + { + var lData = new PgpLiteralDataGenerator(); + + using (var ldOut = lData.Open( + new UncloseableStream(comOut), + PgpLiteralData.Binary, + PgpLiteralData.Console, + text.Length, + ModificationTime)) + { + ldOut.Write(text, 0, text.Length); + } + } - // + // // encrypt - with stream close // MemoryStream cbOut = new UncloseableMemoryStream(); @@ -171,9 +173,10 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests cPk.AddMethod(pass, HashAlgorithmTag.Sha1); byte[] bOutData = bOut.ToArray(); - Stream cOut = cPk.Open(new UncloseableStream(cbOut), bOutData.Length); - cOut.Write(bOutData, 0, bOutData.Length); - cOut.Close(); + using (var cOut = cPk.Open(new UncloseableStream(cbOut), bOutData.Length)) + { + cOut.Write(bOutData, 0, bOutData.Length); + } data = DecryptMessage(cbOut.ToArray()); if (!Arrays.AreEqual(data, text)) @@ -191,12 +194,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests cPk.AddMethod(pass, HashAlgorithmTag.Sha1); bOutData = bOut.ToArray(); - cOut = cPk.Open(new UncloseableStream(cbOut), bOutData.Length); - cOut.Write(bOutData, 0, bOutData.Length); + { + var cOut = cPk.Open(new UncloseableStream(cbOut), bOutData.Length); + cOut.Write(bOutData, 0, bOutData.Length); - cPk.Dispose(); +#pragma warning disable CS0618 // Type or member is obsolete + cPk.Close(); +#pragma warning restore CS0618 // Type or member is obsolete + } - data = DecryptMessage(cbOut.ToArray()); + data = DecryptMessage(cbOut.ToArray()); if (!AreEqual(data, text)) { @@ -213,35 +220,32 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests bOut = new UncloseableMemoryStream(); - comData = new PgpCompressedDataGenerator( - CompressionAlgorithmTag.Zip); - comOut = comData.Open(new UncloseableStream(bOut)); - - lData = new PgpLiteralDataGenerator(); - ldOut = lData.Open( - new UncloseableStream(comOut), - PgpLiteralData.Binary, - PgpLiteralData.Console, - ModificationTime, - new byte[16]); - - ldOut.Write(test, 0, test.Length); - lData.Dispose(); + comData = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip); + using (var comOut = comData.Open(new UncloseableStream(bOut))) + { + var lData = new PgpLiteralDataGenerator(); + using (var ldOut = lData.Open( + new UncloseableStream(comOut), + PgpLiteralData.Binary, + PgpLiteralData.Console, + ModificationTime, + new byte[16])) + { + ldOut.Write(test, 0, test.Length); + } + } - comData.Dispose(); cbOut = new UncloseableMemoryStream(); cPk = new PgpEncryptedDataGenerator( SymmetricKeyAlgorithmTag.Cast5, rand); cPk.AddMethod(pass, HashAlgorithmTag.Sha1); - cOut = cPk.Open(new UncloseableStream(cbOut), new byte[16]); - { - byte[] tmp = bOut.ToArray(); - cOut.Write(tmp, 0, tmp.Length); - } - - cPk.Dispose(); + using (var cOut = cPk.Open(new UncloseableStream(cbOut), new byte[16])) + { + byte[] tmp = bOut.ToArray(); + cOut.Write(tmp, 0, tmp.Length); + } data = DecryptMessage(cbOut.ToArray()); if (!Arrays.AreEqual(data, test)) @@ -258,12 +262,14 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests cPk.AddMethod(pass, HashAlgorithmTag.Sha1); - cOut = cPk.Open(new UncloseableStream(cbOut), new byte[16]); bOutData = bOut.ToArray(); - cOut.Write(bOutData, 0, bOutData.Length); - cPk.Dispose(); - data = DecryptMessage(cbOut.ToArray()); + using (var cOut = cPk.Open(new UncloseableStream(cbOut), new byte[16])) + { + cOut.Write(bOutData, 0, bOutData.Length); + } + + data = DecryptMessage(cbOut.ToArray()); if (!Arrays.AreEqual(data, test)) { Fail("wrong plain text in generated packet"); @@ -307,35 +313,34 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests byte[] msg = new byte[1]; bOut = new MemoryStream(); - comData = new PgpCompressedDataGenerator( - CompressionAlgorithmTag.Zip); + comData = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip); - lData = new PgpLiteralDataGenerator(); - comOut = comData.Open(new UncloseableStream(bOut)); - ldOut = lData.Open( - new UncloseableStream(comOut), - PgpLiteralData.Binary, - PgpLiteralData.Console, - msg.Length, - ModificationTime); - - ldOut.Write(msg, 0, msg.Length); - - ldOut.Close(); + using (var comOut = comData.Open(new UncloseableStream(bOut))) + { + var lData = new PgpLiteralDataGenerator(); + + using (var ldOut = lData.Open( + new UncloseableStream(comOut), + PgpLiteralData.Binary, + PgpLiteralData.Console, + msg.Length, + ModificationTime)) + { + ldOut.Write(msg, 0, msg.Length); + } + } - comOut.Close(); - - cbOut = new MemoryStream(); + cbOut = new MemoryStream(); cPk = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, true, rand); cPk.AddMethod(pass, HashAlgorithmTag.Sha1); - cOut = cPk.Open(new UncloseableStream(cbOut), new byte[16]); + data = bOut.ToArray(); - data = bOut.ToArray(); - cOut.Write(data, 0, data.Length); - - cOut.Close(); + using (var cOut = cPk.Open(new UncloseableStream(cbOut), new byte[16])) + { + cOut.Write(data, 0, data.Length); + } data = DecryptMessage(cbOut.ToArray()); if (!AreEqual(data, msg)) diff --git a/crypto/test/src/openpgp/test/PGPPacketTest.cs b/crypto/test/src/openpgp/test/PGPPacketTest.cs index 737ba6578..0aaf51235 100644 --- a/crypto/test/src/openpgp/test/PGPPacketTest.cs +++ b/crypto/test/src/openpgp/test/PGPPacketTest.cs @@ -25,16 +25,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests { MemoryStream bOut = new MemoryStream(); - Stream outputStream = generator.Open( - new UncloseableStream(bOut), - PgpLiteralData.Binary, - PgpLiteralData.Console, - i, - DateTime.UtcNow); - - outputStream.Write(buf, 0, i); - - generator.Dispose(); + using (var outputStream = generator.Open( + new UncloseableStream(bOut), + PgpLiteralData.Binary, + PgpLiteralData.Console, + i, + DateTime.UtcNow)) + { + outputStream.Write(buf, 0, i); + } PgpObjectFactory fact = new PgpObjectFactory(bOut.ToArray()); diff --git a/crypto/test/src/openpgp/test/PGPRSATest.cs b/crypto/test/src/openpgp/test/PGPRSATest.cs index e3ed2f021..d8b350b2d 100644 --- a/crypto/test/src/openpgp/test/PGPRSATest.cs +++ b/crypto/test/src/openpgp/test/PGPRSATest.cs @@ -354,16 +354,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests // MemoryStream bOut = new MemoryStream(); PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - Stream lOut = lGen.Open( + using (var lOut = lGen.Open( bOut, PgpLiteralData.Binary, PgpLiteralData.Console, text.Length, - DateTime.UtcNow); - - lOut.Write(text, 0, text.Length); - - lGen.Dispose(); + DateTime.UtcNow)) + { + lOut.Write(text, 0, text.Length); + } byte[] bytes = bOut.ToArray(); diff --git a/crypto/test/src/openpgp/test/PGPSignatureTest.cs b/crypto/test/src/openpgp/test/PGPSignatureTest.cs index 2ca0aef0a..a67c07762 100644 --- a/crypto/test/src/openpgp/test/PGPSignatureTest.cs +++ b/crypto/test/src/openpgp/test/PGPSignatureTest.cs @@ -840,24 +840,23 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.GenerateOnePassVersion(false).Encode(bOut); PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - Stream lOut = lGen.Open( + using (var lOut = lGen.Open( new UncloseableStream(bOut), PgpLiteralData.Binary, "_CONSOLE", TEST_DATA.Length * 2, - DateTime.UtcNow); - - int ch; - while ((ch = testIn.ReadByte()) >= 0) + DateTime.UtcNow)) { - lOut.WriteByte((byte)ch); - sGen.Update((byte)ch); - } - - lOut.Write(TEST_DATA, 0, TEST_DATA.Length); - sGen.Update(TEST_DATA); + int ch; + while ((ch = testIn.ReadByte()) >= 0) + { + lOut.WriteByte((byte)ch); + sGen.Update((byte)ch); + } - lGen.Dispose(); + lOut.Write(TEST_DATA, 0, TEST_DATA.Length); + sGen.Update(TEST_DATA); + } sGen.Generate().Encode(bOut); @@ -881,24 +880,23 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.GenerateOnePassVersion(false).Encode(bOut); PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - Stream lOut = lGen.Open( + using (var lOut = lGen.Open( new UncloseableStream(bOut), PgpLiteralData.Text, "_CONSOLE", data.Length * 2, - creationTime); - - int ch; - while ((ch = testIn.ReadByte()) >= 0) + creationTime)) { - lOut.WriteByte((byte)ch); - sGen.Update((byte)ch); - } - - lOut.Write(data, 0, data.Length); - sGen.Update(data); + int ch; + while ((ch = testIn.ReadByte()) >= 0) + { + lOut.WriteByte((byte)ch); + sGen.Update((byte)ch); + } - lGen.Dispose(); + lOut.Write(data, 0, data.Length); + sGen.Update(data); + } PgpSignature sig = sGen.Generate(); @@ -936,24 +934,23 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.GenerateOnePassVersion(false).Encode(bOut); PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - Stream lOut = lGen.Open( + using (var lOut = lGen.Open( new UncloseableStream(bOut), PgpLiteralData.Binary, "_CONSOLE", TEST_DATA.Length * 2, - DateTime.UtcNow); - - int ch; - while ((ch = testIn.ReadByte()) >= 0) + DateTime.UtcNow)) { - lOut.WriteByte((byte)ch); - sGen.Update((byte)ch); - } - - lOut.Write(TEST_DATA, 0, TEST_DATA.Length); - sGen.Update(TEST_DATA); + int ch; + while ((ch = testIn.ReadByte()) >= 0) + { + lOut.WriteByte((byte)ch); + sGen.Update((byte)ch); + } - lGen.Dispose(); + lOut.Write(TEST_DATA, 0, TEST_DATA.Length); + sGen.Update(TEST_DATA); + } sGen.Generate().Encode(bOut); @@ -976,24 +973,23 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.GenerateOnePassVersion(false).Encode(bOut); PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - Stream lOut = lGen.Open( + using (var lOut = lGen.Open( new UncloseableStream(bOut), PgpLiteralData.Text, "_CONSOLE", data.Length * 2, - DateTime.UtcNow); - - int ch; - while ((ch = testIn.ReadByte()) >= 0) + DateTime.UtcNow)) { - lOut.WriteByte((byte)ch); - sGen.Update((byte)ch); - } - - lOut.Write(data, 0, data.Length); - sGen.Update(data); + int ch; + while ((ch = testIn.ReadByte()) >= 0) + { + lOut.WriteByte((byte)ch); + sGen.Update((byte)ch); + } - lGen.Dispose(); + lOut.Write(data, 0, data.Length); + sGen.Update(data); + } PgpSignature sig = sGen.Generate(); -- cgit 1.4.1