@@ -32,9 +32,6 @@ internal static class HandshakeHelpers
32
32
( byte ) 'B' , ( byte ) '0' , ( byte ) 'D' , ( byte ) 'C' , ( byte ) '8' , ( byte ) '5' , ( byte ) 'B' , ( byte ) '1' , ( byte ) '1'
33
33
} ;
34
34
35
- [ ThreadStatic ]
36
- private static SHA1 _algorithm ;
37
-
38
35
// Verify Method, Upgrade, Connection, version, key, etc..
39
36
public static bool CheckSupportedWebSocketRequest ( string method , IEnumerable < KeyValuePair < string , string > > headers )
40
37
{
@@ -112,25 +109,23 @@ public static string CreateResponseKey(string requestKey)
112
109
// this concatenated value to obtain a 20-byte value and base64-encoding"
113
110
// https://tools.ietf.org/html/rfc6455#section-4.2.2
114
111
115
- if ( _algorithm == null )
112
+ using ( var algorithm = SHA1 . Create ( ) )
116
113
{
117
- _algorithm = SHA1 . Create ( ) ;
118
- }
119
-
120
- // requestKey is already verified to be small (24 bytes) by 'IsRequestKeyValid()' and everything is 1:1 mapping to UTF8 bytes
121
- // so this can be hardcoded to 60 bytes for the requestKey + static websocket string
122
- Span < byte > mergedBytes = stackalloc byte [ 60 ] ;
123
- Encoding . UTF8 . GetBytes ( requestKey , mergedBytes ) ;
124
- _encodedWebSocketKey . CopyTo ( mergedBytes . Slice ( 24 ) ) ;
114
+ // requestKey is already verified to be small (24 bytes) by 'IsRequestKeyValid()' and everything is 1:1 mapping to UTF8 bytes
115
+ // so this can be hardcoded to 60 bytes for the requestKey + static websocket string
116
+ Span < byte > mergedBytes = stackalloc byte [ 60 ] ;
117
+ Encoding . UTF8 . GetBytes ( requestKey , mergedBytes ) ;
118
+ _encodedWebSocketKey . CopyTo ( mergedBytes . Slice ( 24 ) ) ;
119
+
120
+ Span < byte > hashedBytes = stackalloc byte [ 20 ] ;
121
+ var success = algorithm . TryComputeHash ( mergedBytes , hashedBytes , out var written ) ;
122
+ if ( ! success || written != 20 )
123
+ {
124
+ throw new InvalidOperationException ( "Could not compute the hash for the 'Sec-WebSocket-Accept' header." ) ;
125
+ }
125
126
126
- Span < byte > hashedBytes = stackalloc byte [ 20 ] ;
127
- var success = _algorithm . TryComputeHash ( mergedBytes , hashedBytes , out var written ) ;
128
- if ( ! success || written != 20 )
129
- {
130
- throw new InvalidOperationException ( "Could not compute the hash for the 'Sec-WebSocket-Accept' header." ) ;
127
+ return Convert . ToBase64String ( hashedBytes ) ;
131
128
}
132
-
133
- return Convert . ToBase64String ( hashedBytes ) ;
134
129
}
135
130
}
136
131
}
0 commit comments