Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,32 @@ namespace System.Security.Cryptography
/// </summary>
public sealed partial class CngKey : IDisposable
{
//
// Key properties
//

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

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

/// <summary>
/// Algorithm group this key can be used with
/// </summary>
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;

}

}
Expand All @@ -40,14 +49,22 @@ public CngAlgorithm Algorithm
/// 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 @@ -279,10 +296,19 @@ public CngProvider? Provider
{
get
{
string? provider = _providerHandle.GetPropertyAsString(ProviderPropertyName.Name, CngPropertyOptions.None);
if (provider == null)
return null;
return new CngProvider(provider);
if (!_hasCachedProvider || _keyHandle.IsClosed)
{
string? provider = _providerHandle.GetPropertyAsString(ProviderPropertyName.Name, CngPropertyOptions.None);

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

_hasCachedProvider = true;
}

return _cachedProvider;
}
}

Expand Down