fixed not disposing RSA keys

master
Avril 5 years ago
parent e885b3c5d3
commit d4fc31da8d
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -264,7 +264,7 @@ namespace EncryptedNetwork
~EncryptedWriteBlock() { Dispose(false); aes = null; }
}
private readonly RSACryptoServiceProvider you;
private RSACryptoServiceProvider you;
private RSACryptoServiceProvider them;
/// <summary>
@ -305,7 +305,7 @@ namespace EncryptedNetwork
/// Initialise a new <see cref="EncryptedNetworkStream"/> from a <seealso cref="NetworkStream"/>
/// </summary>
/// <param name="stream">The Stream to set backing for.</param>
public EncryptedNetworkStream(NetworkStream stream) : this(stream, new RSACryptoServiceProvider()) { }
public EncryptedNetworkStream(NetworkStream stream) : this(stream, new RSACryptoServiceProvider()) { KeepPrivateCSPAlive = false; }
/// <summary>
/// Initialise a new <see cref="EncryptedNetworkStream"/> from a <seealso cref="Socket"/>
/// </summary>
@ -318,7 +318,7 @@ namespace EncryptedNetwork
/// </summary>
/// <param name="stream">The Socket to set backing for. (NOTE: Closes the socket on dispose)</param>
public EncryptedNetworkStream(Socket sock)
: this(sock, new RSACryptoServiceProvider()) { }
: this(sock, new RSACryptoServiceProvider()) { KeepPrivateCSPAlive = false; }
/// <summary>
/// Exchange the RSA public keys asynchronously.
@ -360,10 +360,25 @@ namespace EncryptedNetwork
public void Exchange()
=> ExchangeAsync().Sync();
/// <summary>
/// Keep <see cref="PrivateCSP"/> alive after disposing?
/// </summary>
public bool KeepPrivateCSPAlive { get; set; } = true;
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if(disposing)
{
if (!KeepPrivateCSPAlive)
you.Dispose();
them?.Dispose();
}
you = null;
them = null;
}
~EncryptedNetworkStream() => Dispose(false);
/// <summary>
/// Read unencrypted data from the backing stream.

@ -30,12 +30,24 @@ namespace EncryptedNetwork
backing = s;
}
protected ObjectDisposedException ThrowIfDisposed()
{
if (Disposed)
throw new ObjectDisposedException(GetType().Name + " object has been disposed.");
else return null;
}
protected bool Disposed { get; private set; } = false;
protected override void Dispose(bool disposing)
{
ThrowIfDisposed();
Disposed = true;
if (disposing)
{
if (!KeepBackingStreamAlive) backing.Dispose();
}
backing = null;
}
#region Stream Overrides

Loading…
Cancel
Save