Skip to content

Commit

Permalink
Merge pull request #11 from Cysharp/remove-store-hashalgorithm
Browse files Browse the repository at this point in the history
Removing store HashAlgorithm to avoid memory leak
  • Loading branch information
mayuki authored Jan 28, 2022
2 parents efb08f3 + 4f65a94 commit 8e00be6
Showing 1 changed file with 0 additions and 21 deletions.
21 changes: 0 additions & 21 deletions src/LitJWT/JwtAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public abstract class SymmetricJwtAlgorithmBase : IJwtAlgorithm, IDisposable
int isDisposed;

public ReadOnlySpan<byte> HeaderBase64Url => header;
ConcurrentBag<HashAlgorithm> generatedAlgorithms = new ConcurrentBag<HashAlgorithm>();

protected SymmetricJwtAlgorithmBase(byte[] key)
{
Expand All @@ -69,11 +68,9 @@ protected SymmetricJwtAlgorithmBase(byte[] key)
Base64.TryToBase64UrlUtf8(alg, buffer, out _);
header = buffer.ToArray();

// Create a local thread version of the hash algorithm instance for thread safety.
hash = new ThreadLocal<HashAlgorithm>(() =>
{
var newHash = CreateHashAlgorithm(key);
generatedAlgorithms.Add(newHash);
return newHash;
}, false);
}
Expand Down Expand Up @@ -105,10 +102,6 @@ protected virtual void Dispose(bool disposing)
{
if (Interlocked.Increment(ref isDisposed) == 1)
{
foreach (var item in generatedAlgorithms)
{
item.Dispose();
}
hash.Dispose();
}
}
Expand Down Expand Up @@ -200,7 +193,6 @@ public abstract class RSAJwtAlgorithmBase : IJwtAlgorithm, IDisposable

readonly ThreadLocal<RSA> publicKey;
readonly ThreadLocal<RSA> privateKey;
readonly ConcurrentBag<RSA> generatedAlgorithms = new ConcurrentBag<RSA>();

int isDisposed;
byte[] header;
Expand All @@ -220,15 +212,13 @@ public RSAJwtAlgorithmBase(X509Certificate2 cert)
publicKey = new ThreadLocal<RSA>(() =>
{
var key = cert?.GetRSAPublicKey() ?? publicKeyFactory();
generatedAlgorithms.Add(key);
return key;
}, false);

// Create a local version of the private key instance for thread safety.
privateKey = new ThreadLocal<RSA>(() =>
{
var key = cert?.GetRSAPrivateKey() ?? privateKeyFactory();
generatedAlgorithms.Add(key);
return key;
}, false);
}
Expand Down Expand Up @@ -275,10 +265,6 @@ protected virtual void Dispose(bool disposing)
{
if (Interlocked.Increment(ref isDisposed) == 1)
{
foreach (var algorithmsValue in generatedAlgorithms)
{
algorithmsValue.Dispose();
}
publicKey.Dispose();
privateKey.Dispose();
}
Expand Down Expand Up @@ -398,7 +384,6 @@ public abstract class ESJwtAlgorithmBase : IJwtAlgorithm, IDisposable

readonly ThreadLocal<ECDsa> publicKey;
readonly ThreadLocal<ECDsa> privateKey;
readonly ConcurrentBag<ECDsa> generatedAlgorithms = new ConcurrentBag<ECDsa>();
int isDisposed;
byte[] header;
public ReadOnlySpan<byte> HeaderBase64Url => header;
Expand All @@ -417,15 +402,13 @@ public ESJwtAlgorithmBase(X509Certificate2 cert)
publicKey = new ThreadLocal<ECDsa>(() =>
{
var key = cert.GetECDsaPublicKey();
generatedAlgorithms.Add(key);
return key;
}, false);

// Create a local version of the private key instance for thread safety.
privateKey = new ThreadLocal<ECDsa>(() =>
{
var key = cert.GetECDsaPrivateKey();
generatedAlgorithms.Add(key);
return key;
}, false);
}
Expand Down Expand Up @@ -464,10 +447,6 @@ protected virtual void Dispose(bool disposing)
{
if (Interlocked.Increment(ref isDisposed) == 1)
{
foreach (var algorithmsValue in generatedAlgorithms)
{
algorithmsValue.Dispose();
}
publicKey.Dispose();
privateKey.Dispose();
}
Expand Down

0 comments on commit 8e00be6

Please sign in to comment.