@@ -23,19 +23,33 @@ public static StreamContent WithDigest(
2323 Stream fileStream ,
2424 FileDigestOptions options )
2525 {
26- if ( ! options . UseFileDigesting || options . DigestHashAlgorithm == null ) {
26+ if (
27+ ! options . UseFileDigesting ||
28+ options . DigestHashAlgorithm == DigestHashAlgorithm . None
29+ ) {
2730 return content ;
2831 }
2932
3033 SetHashValue ( fileStream , options ) ;
3134
3235 string base64Digest = Convert . ToBase64String ( options . DigestHashValue ) ;
3336
34- content . Headers . Add ( "Digest" , $ "{ options . DigestHashAlgorithm } ={ base64Digest } ") ;
37+ content . Headers . Add ( "Digest" , $ "{ GetDigestHashAlgorithmName ( options ) } ={ base64Digest } ") ;
3538
3639 return content ;
3740 }
3841
42+ private static string GetDigestHashAlgorithmName ( FileDigestOptions options )
43+ {
44+ return options . DigestHashAlgorithm switch {
45+ DigestHashAlgorithm . SHA256 => "SHA-256" ,
46+ DigestHashAlgorithm . SHA512 => "SHA-512" ,
47+
48+ _ => throw new InvalidOperationException (
49+ $ "No hash algorithm name for '{ options . DigestHashAlgorithm } '") ,
50+ } ;
51+ }
52+
3953 private static void SetHashValue (
4054 Stream fileStream ,
4155 FileDigestOptions options )
@@ -60,48 +74,17 @@ private static void SetHashValue(
6074 private static HashAlgorithm HashAlgorithmCreate (
6175 FileDigestOptions options )
6276 {
63- string algorithmName = options . DigestHashAlgorithm ;
64- HashAlgorithm algorithm = null ;
65-
77+ return options . DigestHashAlgorithm switch {
6678#if NETSTANDARD2_0 || NET8_0
67- switch ( algorithmName ) {
68- case "SHA1" :
69- case "SHA-1" :
70- algorithm = SHA1 . Create ( ) ;
71- break ;
72- case "SHA256" :
73- case "SHA-256" :
74- algorithm = SHA256 . Create ( ) ;
75- break ;
76- case "SHA384" :
77- case "SHA-384" :
78- algorithm = SHA384 . Create ( ) ;
79- break ;
80- case "SHA512" :
81- case "SHA-512" :
82- algorithm = SHA512 . Create ( ) ;
83- break ;
84- }
79+ DigestHashAlgorithm . SHA256 => SHA256. Create ( ) ,
80+ DigestHashAlgorithm. SHA512 => SHA512. Create ( ) ,
8581#else
86- algorithm = HashAlgorithm . Create ( algorithmName ) ;
82+ DigestHashAlgorithm . SHA256 => HashAlgorithm. Create ( "SHA256" ) ,
83+ DigestHashAlgorithm. SHA512 => HashAlgorithm. Create ( "SHA512" ) ,
8784#endif
88- if ( algorithm == null && options . DigestHashValue == null ) {
89- algorithm = DefaultHashAlgorithm ( ) ;
90- options . DigestHashAlgorithm = algorithm . GetType ( ) . Name ;
91- }
92-
93- if ( algorithm == null ) {
94- throw new InvalidOperationException ( $ "No hash algorithm for '{ algorithmName } '") ;
95- }
96-
97- return algorithm ;
85+ _ => throw new InvalidOperationException(
86+ $ "No hash algorithm for '{ options . DigestHashAlgorithm } '") ,
87+ } ;
9888 }
99-
100- private static HashAlgorithm DefaultHashAlgorithm ( ) =>
101- #if NETSTANDARD2_0 || NET8_0
102- SHA256 . Create ( ) ;
103- #else
104- HashAlgorithm . Create ( ) ;
105- #endif
10689 }
10790}
0 commit comments