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/pkcs/Pkcs10CertificationRequest.cs | 8 ++++---- crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs | 8 +++++--- crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs | 10 ++++++---- 3 files changed, 15 insertions(+), 11 deletions(-) (limited to 'crypto/src/pkcs') diff --git a/crypto/src/pkcs/Pkcs10CertificationRequest.cs b/crypto/src/pkcs/Pkcs10CertificationRequest.cs index 59b5b51ed..12151a001 100644 --- a/crypto/src/pkcs/Pkcs10CertificationRequest.cs +++ b/crypto/src/pkcs/Pkcs10CertificationRequest.cs @@ -345,10 +345,10 @@ namespace Org.BouncyCastle.Pkcs byte[] b = reqInfo.GetDerEncoded(); IStreamCalculator streamCalculator = verifier.CreateCalculator(); - - streamCalculator.Stream.Write(b, 0, b.Length); - - Platform.Dispose(streamCalculator.Stream); + using (var stream = streamCalculator.Stream) + { + stream.Write(b, 0, b.Length); + } return streamCalculator.GetResult().IsVerified(sigBits.GetOctets()); } diff --git a/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs b/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs index 5882dee38..e18619c18 100644 --- a/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs +++ b/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs @@ -91,9 +91,11 @@ namespace Org.BouncyCastle.Pkcs ICipher encIn = decryptorBuilder.BuildCipher(new MemoryInputStream(encryptedPrivateKeyInfo.GetEncryptedData())); - Stream strm = encIn.Stream; - byte[] data = Streams.ReadAll(encIn.Stream); - Platform.Dispose(strm); + byte[] data; + using (var strm = encIn.Stream) + { + data = Streams.ReadAll(encIn.Stream); + } return PrivateKeyInfo.GetInstance(data); } diff --git a/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs b/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs index 8f751492f..2556a947f 100644 --- a/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs +++ b/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs @@ -36,11 +36,13 @@ namespace Org.BouncyCastle.Pkcs ICipher cOut = encryptor.BuildCipher(bOut); byte[] keyData = privateKeyInfo.GetEncoded(); - Stream str = cOut.Stream; - str.Write(keyData, 0, keyData.Length); - Platform.Dispose(str); + using (var str = cOut.Stream) + { + str.Write(keyData, 0, keyData.Length); + } - return new Pkcs8EncryptedPrivateKeyInfo(new EncryptedPrivateKeyInfo((AlgorithmIdentifier)encryptor.AlgorithmDetails, bOut.ToArray())); + return new Pkcs8EncryptedPrivateKeyInfo( + new EncryptedPrivateKeyInfo((AlgorithmIdentifier)encryptor.AlgorithmDetails, bOut.ToArray())); } catch (IOException) { -- cgit 1.4.1