@@ -19,11 +19,6 @@ internal static partial class Crypto
1919 private static volatile IntPtr s_evpSha3_512 ;
2020 private static volatile IntPtr s_evpSha3_Shake128 ;
2121 private static volatile IntPtr s_evpSha3_Shake256 ;
22- private static volatile bool s_evpSha3_256Cached ;
23- private static volatile bool s_evpSha3_384Cached ;
24- private static volatile bool s_evpSha3_512Cached ;
25- private static volatile bool s_evpSha3_Shake128Cached ;
26- private static volatile bool s_evpSha3_Shake256Cached ;
2722
2823 [ LibraryImport ( Libraries . CryptoNative ) ]
2924 private static partial IntPtr CryptoNative_EvpMd5 ( ) ;
@@ -58,99 +53,48 @@ private static IntPtr EvpSha512() =>
5853 [ LibraryImport ( Libraries . CryptoNative ) ]
5954 private static partial IntPtr CryptoNative_EvpSha3_256 ( ) ;
6055
61- private static IntPtr EvpSha3_256 ( )
62- {
63- if ( ! s_evpSha3_256Cached )
64- {
65- s_evpSha3_256 = CryptoNative_EvpSha3_256 ( ) ;
66- s_evpSha3_256Cached = true ;
67- }
68-
69- return s_evpSha3_256 ;
70- }
56+ private static IntPtr EvpSha3_256 ( ) =>
57+ s_evpSha3_256 != IntPtr . Zero ? s_evpSha3_256 : ( s_evpSha3_256 = CryptoNative_EvpSha3_256 ( ) ) ;
7158
7259 [ LibraryImport ( Libraries . CryptoNative ) ]
7360 private static partial IntPtr CryptoNative_EvpSha3_384 ( ) ;
7461
75- private static IntPtr EvpSha3_384 ( )
76- {
77- if ( ! s_evpSha3_384Cached )
78- {
79- s_evpSha3_384 = CryptoNative_EvpSha3_384 ( ) ;
80- s_evpSha3_384Cached = true ;
81- }
82-
83- return s_evpSha3_384 ;
84- }
62+ private static IntPtr EvpSha3_384 ( ) =>
63+ s_evpSha3_384 != IntPtr . Zero ? s_evpSha3_384 : ( s_evpSha3_384 = CryptoNative_EvpSha3_384 ( ) ) ;
8564
8665 [ LibraryImport ( Libraries . CryptoNative ) ]
8766 private static partial IntPtr CryptoNative_EvpSha3_512 ( ) ;
8867
89- private static IntPtr EvpSha3_512 ( )
90- {
91- if ( ! s_evpSha3_512Cached )
92- {
93- s_evpSha3_512 = CryptoNative_EvpSha3_512 ( ) ;
94- s_evpSha3_512Cached = true ;
95- }
96-
97- return s_evpSha3_512 ;
98- }
68+ private static IntPtr EvpSha3_512 ( ) =>
69+ s_evpSha3_512 != IntPtr . Zero ? s_evpSha3_512 : ( s_evpSha3_512 = CryptoNative_EvpSha3_512 ( ) ) ;
9970
10071 [ LibraryImport ( Libraries . CryptoNative ) ]
10172 private static partial IntPtr CryptoNative_EvpShake128 ( ) ;
10273
103- private static IntPtr EvpShake128 ( )
104- {
105- if ( ! s_evpSha3_Shake128Cached )
106- {
107- s_evpSha3_Shake128 = CryptoNative_EvpShake128 ( ) ;
108- s_evpSha3_Shake128Cached = true ;
109- }
110-
111- return s_evpSha3_Shake128 ;
112- }
74+ private static IntPtr EvpShake128 ( ) =>
75+ s_evpSha3_Shake128 != IntPtr . Zero ? s_evpSha3_Shake128 : ( s_evpSha3_Shake128 = CryptoNative_EvpShake128 ( ) ) ;
11376
11477 [ LibraryImport ( Libraries . CryptoNative ) ]
11578 private static partial IntPtr CryptoNative_EvpShake256 ( ) ;
11679
117- private static IntPtr EvpShake256 ( )
118- {
119- if ( ! s_evpSha3_Shake256Cached )
120- {
121- s_evpSha3_Shake256 = CryptoNative_EvpShake256 ( ) ;
122- s_evpSha3_Shake256Cached = true ;
123- }
124-
125- return s_evpSha3_Shake256 ;
126- }
80+ private static IntPtr EvpShake256 ( ) =>
81+ s_evpSha3_Shake256 != IntPtr . Zero ? s_evpSha3_Shake256 : ( s_evpSha3_Shake256 = CryptoNative_EvpShake256 ( ) ) ;
12782
12883 internal static IntPtr HashAlgorithmToEvp ( string hashAlgorithmId )
12984 {
130- switch ( hashAlgorithmId )
85+ return hashAlgorithmId switch
13186 {
132- case HashAlgorithmNames . SHA1 : return EvpSha1 ( ) ;
133- case HashAlgorithmNames . SHA256 : return EvpSha256 ( ) ;
134- case HashAlgorithmNames . SHA384 : return EvpSha384 ( ) ;
135- case HashAlgorithmNames . SHA512 : return EvpSha512 ( ) ;
136- case HashAlgorithmNames . SHA3_256 :
137- IntPtr sha3_256 = EvpSha3_256 ( ) ;
138- return sha3_256 != 0 ? sha3_256 : throw new PlatformNotSupportedException ( ) ;
139- case HashAlgorithmNames . SHA3_384 :
140- IntPtr sha3_384 = EvpSha3_384 ( ) ;
141- return sha3_384 != 0 ? sha3_384 : throw new PlatformNotSupportedException ( ) ;
142- case HashAlgorithmNames . SHA3_512 :
143- IntPtr sha3_512 = EvpSha3_512 ( ) ;
144- return sha3_512 != 0 ? sha3_512 : throw new PlatformNotSupportedException ( ) ;
145- case HashAlgorithmNames . SHAKE128 :
146- IntPtr shake128 = EvpShake128 ( ) ;
147- return shake128 != 0 ? shake128 : throw new PlatformNotSupportedException ( ) ;
148- case HashAlgorithmNames . SHAKE256 :
149- IntPtr shake256 = EvpShake256 ( ) ;
150- return shake256 != 0 ? shake256 : throw new PlatformNotSupportedException ( ) ;
151- case nameof ( HashAlgorithmName . MD5 ) : return EvpMd5 ( ) ;
152- default :
153- throw new CryptographicException ( SR . Format ( SR . Cryptography_UnknownHashAlgorithm , hashAlgorithmId ) ) ;
87+ HashAlgorithmNames . MD5 => EvpMd5 ( ) ,
88+ HashAlgorithmNames . SHA1 => EvpSha1 ( ) ,
89+ HashAlgorithmNames . SHA256 => EvpSha256 ( ) ,
90+ HashAlgorithmNames . SHA384 => EvpSha384 ( ) ,
91+ HashAlgorithmNames . SHA512 => EvpSha512 ( ) ,
92+ HashAlgorithmNames . SHA3_256 => EvpSha3_256 ( ) ,
93+ HashAlgorithmNames . SHA3_384 => EvpSha3_384 ( ) ,
94+ HashAlgorithmNames . SHA3_512 => EvpSha3_512 ( ) ,
95+ HashAlgorithmNames . SHAKE128 => EvpShake128 ( ) ,
96+ HashAlgorithmNames . SHAKE256 => EvpShake256 ( ) ,
97+ _ => throw new CryptographicException ( SR . Format ( SR . Cryptography_UnknownHashAlgorithm , hashAlgorithmId ) ) ,
15498 } ;
15599 }
156100
@@ -163,17 +107,12 @@ internal static bool HashAlgorithmSupported(string hashAlgorithmId)
163107 case HashAlgorithmNames . SHA384 :
164108 case HashAlgorithmNames . SHA512 :
165109 case HashAlgorithmNames . MD5 :
166- return true ;
167110 case HashAlgorithmNames . SHA3_256 :
168- return EvpSha3_256 ( ) != 0 ;
169111 case HashAlgorithmNames . SHA3_384 :
170- return EvpSha3_384 ( ) != 0 ;
171112 case HashAlgorithmNames . SHA3_512 :
172- return EvpSha3_512 ( ) != 0 ;
173113 case HashAlgorithmNames . SHAKE128 :
174- return EvpShake128 ( ) != 0 ;
175114 case HashAlgorithmNames . SHAKE256 :
176- return EvpShake256 ( ) != 0 ;
115+ return true ;
177116 default :
178117 throw new CryptographicException ( SR . Format ( SR . Cryptography_UnknownHashAlgorithm , hashAlgorithmId ) ) ;
179118 }
0 commit comments