From a8a17fd70fc8df3ca7402323ad5c4f36b25cb806 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 9 Nov 2022 01:13:27 +0700 Subject: Dispose cleanup - IDisposable for PemReader, PemWriter, IStreamGenerator --- crypto/src/crmf/EncryptedValueBuilder.cs | 8 +++++--- crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) (limited to 'crypto/src/crmf') diff --git a/crypto/src/crmf/EncryptedValueBuilder.cs b/crypto/src/crmf/EncryptedValueBuilder.cs index 0154be58c..37f38df1c 100644 --- a/crypto/src/crmf/EncryptedValueBuilder.cs +++ b/crypto/src/crmf/EncryptedValueBuilder.cs @@ -112,12 +112,14 @@ namespace Org.BouncyCastle.Crmf private EncryptedValue EncryptData(byte[] data) { MemoryOutputStream bOut = new MemoryOutputStream(); - Stream eOut = encryptor.BuildCipher(bOut).Stream; + var cipher = encryptor.BuildCipher(bOut); try { - eOut.Write(data, 0, data.Length); - Platform.Dispose(eOut); + using (var eOut = cipher.Stream) + { + eOut.Write(data, 0, data.Length); + } } catch (IOException e) { diff --git a/crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs b/crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs index eed66b083..4cd568e81 100644 --- a/crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs +++ b/crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs @@ -38,11 +38,13 @@ namespace Org.BouncyCastle.Crmf { IMacFactory fact = generator.Build(password); - IStreamCalculator calc = fact.CreateCalculator(); byte[] d = _pubKeyInfo.GetDerEncoded(); - calc.Stream.Write(d, 0, d.Length); - calc.Stream.Flush(); - Platform.Dispose(calc.Stream); + + IStreamCalculator calc = fact.CreateCalculator(); + using (var stream = calc.Stream) + { + stream.Write(d, 0, d.Length); + } this._publicKeyMAC = new PKMacValue( (AlgorithmIdentifier)fact.AlgorithmDetails, -- cgit 1.4.1