@@ -15,12 +15,14 @@ namespace System.Security.Cryptography
15
15
/// </summary>
16
16
public sealed partial class CngKey : IDisposable
17
17
{
18
- //
19
- // Key properties
20
- //
21
-
22
18
private const int CachedKeySizeUninitializedSentinel = - 1 ;
23
- private int _cachedKeySize = CachedKeySizeUninitializedSentinel ;
19
+ private volatile int _cachedKeySize = CachedKeySizeUninitializedSentinel ;
20
+
21
+ private volatile CngAlgorithm ? _cachedAlgorithm ;
22
+ private volatile bool _hasCachedAlgorithmGroup ;
23
+ private volatile CngAlgorithmGroup ? _cachedAlgorithmGroup ;
24
+ private volatile bool _hasCachedProvider ;
25
+ private volatile CngProvider ? _cachedProvider ;
24
26
25
27
/// <summary>
26
28
/// Algorithm group this key can be used with
@@ -29,25 +31,38 @@ public CngAlgorithm Algorithm
29
31
{
30
32
get
31
33
{
32
- string algorithm = _keyHandle . GetPropertyAsString ( KeyPropertyName . Algorithm , CngPropertyOptions . None ) ! ;
33
- // .NET Framework compat: Don't check for null. Just let CngAlgorithm handle it.
34
- return new CngAlgorithm ( algorithm ) ;
35
- }
34
+ if ( _cachedAlgorithm is null || _keyHandle . IsClosed )
35
+ {
36
+ string algorithm = _keyHandle . GetPropertyAsString ( KeyPropertyName . Algorithm , CngPropertyOptions . None ) ! ;
36
37
38
+ // .NET Framework compat: Don't check for null. Just let CngAlgorithm handle it.
39
+ _cachedAlgorithm = new CngAlgorithm ( algorithm ) ;
40
+ }
41
+
42
+ return _cachedAlgorithm ;
43
+ }
37
44
}
38
45
39
46
/// <summary>
40
47
/// Name of the algorithm this key can be used with
41
48
/// </summary>
42
49
public CngAlgorithmGroup ? AlgorithmGroup
43
-
44
50
{
45
51
get
46
52
{
47
- string ? algorithmGroup = _keyHandle . GetPropertyAsString ( KeyPropertyName . AlgorithmGroup , CngPropertyOptions . None ) ;
48
- if ( algorithmGroup == null )
49
- return null ;
50
- return new CngAlgorithmGroup ( algorithmGroup ) ;
53
+ if ( ! _hasCachedAlgorithmGroup || _keyHandle . IsClosed )
54
+ {
55
+ string ? algorithmGroup = _keyHandle . GetPropertyAsString ( KeyPropertyName . AlgorithmGroup , CngPropertyOptions . None ) ;
56
+
57
+ if ( algorithmGroup is not null )
58
+ {
59
+ _cachedAlgorithmGroup = new CngAlgorithmGroup ( algorithmGroup ) ;
60
+ }
61
+
62
+ _hasCachedAlgorithmGroup = true ;
63
+ }
64
+
65
+ return _cachedAlgorithmGroup ;
51
66
}
52
67
}
53
68
@@ -242,7 +257,6 @@ int ComputeKeySize()
242
257
/// Usage restrictions on the key
243
258
/// </summary>
244
259
public CngKeyUsages KeyUsage
245
-
246
260
{
247
261
get
248
262
{
@@ -279,10 +293,19 @@ public CngProvider? Provider
279
293
{
280
294
get
281
295
{
282
- string ? provider = _providerHandle . GetPropertyAsString ( ProviderPropertyName . Name , CngPropertyOptions . None ) ;
283
- if ( provider == null )
284
- return null ;
285
- return new CngProvider ( provider ) ;
296
+ if ( ! _hasCachedProvider || _providerHandle . IsClosed )
297
+ {
298
+ string ? provider = _providerHandle . GetPropertyAsString ( ProviderPropertyName . Name , CngPropertyOptions . None ) ;
299
+
300
+ if ( provider is not null )
301
+ {
302
+ _cachedProvider = new CngProvider ( provider ) ;
303
+ }
304
+
305
+ _hasCachedProvider = true ;
306
+ }
307
+
308
+ return _cachedProvider ;
286
309
}
287
310
}
288
311
0 commit comments