Skip to content

Commit

Permalink
Memoize some CngKey standard properties
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones authored Mar 6, 2024
1 parent d47c6f1 commit 5f52977
Showing 1 changed file with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ namespace System.Security.Cryptography
/// </summary>
public sealed partial class CngKey : IDisposable
{
//
// Key properties
//

private const int CachedKeySizeUninitializedSentinel = -1;
private int _cachedKeySize = CachedKeySizeUninitializedSentinel;
private volatile int _cachedKeySize = CachedKeySizeUninitializedSentinel;

private volatile CngAlgorithm? _cachedAlgorithm;
private volatile bool _hasCachedAlgorithmGroup;
private volatile CngAlgorithmGroup? _cachedAlgorithmGroup;
private volatile bool _hasCachedProvider;
private volatile CngProvider? _cachedProvider;

/// <summary>
/// Algorithm group this key can be used with
Expand All @@ -29,25 +31,38 @@ public CngAlgorithm Algorithm
{
get
{
string algorithm = _keyHandle.GetPropertyAsString(KeyPropertyName.Algorithm, CngPropertyOptions.None)!;
// .NET Framework compat: Don't check for null. Just let CngAlgorithm handle it.
return new CngAlgorithm(algorithm);
}
if (_cachedAlgorithm is null || _keyHandle.IsClosed)
{
string algorithm = _keyHandle.GetPropertyAsString(KeyPropertyName.Algorithm, CngPropertyOptions.None)!;

// .NET Framework compat: Don't check for null. Just let CngAlgorithm handle it.
_cachedAlgorithm = new CngAlgorithm(algorithm);
}

return _cachedAlgorithm;
}
}

/// <summary>
/// Name of the algorithm this key can be used with
/// </summary>
public CngAlgorithmGroup? AlgorithmGroup

{
get
{
string? algorithmGroup = _keyHandle.GetPropertyAsString(KeyPropertyName.AlgorithmGroup, CngPropertyOptions.None);
if (algorithmGroup == null)
return null;
return new CngAlgorithmGroup(algorithmGroup);
if (!_hasCachedAlgorithmGroup || _keyHandle.IsClosed)
{
string? algorithmGroup = _keyHandle.GetPropertyAsString(KeyPropertyName.AlgorithmGroup, CngPropertyOptions.None);

if (algorithmGroup is not null)
{
_cachedAlgorithmGroup = new CngAlgorithmGroup(algorithmGroup);
}

_hasCachedAlgorithmGroup = true;
}

return _cachedAlgorithmGroup;
}
}

Expand Down Expand Up @@ -242,7 +257,6 @@ int ComputeKeySize()
/// Usage restrictions on the key
/// </summary>
public CngKeyUsages KeyUsage

{
get
{
Expand Down Expand Up @@ -279,10 +293,19 @@ public CngProvider? Provider
{
get
{
string? provider = _providerHandle.GetPropertyAsString(ProviderPropertyName.Name, CngPropertyOptions.None);
if (provider == null)
return null;
return new CngProvider(provider);
if (!_hasCachedProvider || _providerHandle.IsClosed)
{
string? provider = _providerHandle.GetPropertyAsString(ProviderPropertyName.Name, CngPropertyOptions.None);

if (provider is not null)
{
_cachedProvider = new CngProvider(provider);
}

_hasCachedProvider = true;
}

return _cachedProvider;
}
}

Expand Down

0 comments on commit 5f52977

Please sign in to comment.