@@ -102,6 +102,7 @@ private sealed class EvpHashProvider : HashProvider
102
102
{
103
103
private readonly LiteHash _liteHash ;
104
104
private bool _running ;
105
+ private ConcurrencyBlock _block ;
105
106
106
107
public EvpHashProvider ( string hashAlgorithmId )
107
108
{
@@ -110,21 +111,30 @@ public EvpHashProvider(string hashAlgorithmId)
110
111
111
112
public override void AppendHashData ( ReadOnlySpan < byte > data )
112
113
{
113
- _liteHash . Append ( data ) ;
114
- _running = true ;
114
+ using ( ConcurrencyBlock . Enter ( ref _block ) )
115
+ {
116
+ _liteHash . Append ( data ) ;
117
+ _running = true ;
118
+ }
115
119
}
116
120
117
121
public override int FinalizeHashAndReset ( Span < byte > destination )
118
122
{
119
- int written = _liteHash . Finalize ( destination ) ;
120
- _liteHash . Reset ( ) ;
121
- _running = false ;
122
- return written ;
123
+ using ( ConcurrencyBlock . Enter ( ref _block ) )
124
+ {
125
+ int written = _liteHash . Finalize ( destination ) ;
126
+ _liteHash . Reset ( ) ;
127
+ _running = false ;
128
+ return written ;
129
+ }
123
130
}
124
131
125
132
public override int GetCurrentHash ( Span < byte > destination )
126
133
{
127
- return _liteHash . Current ( destination ) ;
134
+ using ( ConcurrencyBlock . Enter ( ref _block ) )
135
+ {
136
+ return _liteHash . Current ( destination ) ;
137
+ }
128
138
}
129
139
130
140
public override int HashSizeInBytes => _liteHash . HashSizeInBytes ;
@@ -139,10 +149,13 @@ public override void Dispose(bool disposing)
139
149
140
150
public override void Reset ( )
141
151
{
142
- if ( _running )
152
+ using ( ConcurrencyBlock . Enter ( ref _block ) )
143
153
{
144
- _liteHash . Reset ( ) ;
145
- _running = false ;
154
+ if ( _running )
155
+ {
156
+ _liteHash . Reset ( ) ;
157
+ _running = false ;
158
+ }
146
159
}
147
160
}
148
161
}
@@ -151,6 +164,7 @@ private sealed class HmacHashProvider : HashProvider
151
164
{
152
165
private readonly LiteHmac _liteHmac ;
153
166
private bool _running ;
167
+ private ConcurrencyBlock _block ;
154
168
155
169
public HmacHashProvider ( string hashAlgorithmId , ReadOnlySpan < byte > key )
156
170
{
@@ -159,21 +173,30 @@ public HmacHashProvider(string hashAlgorithmId, ReadOnlySpan<byte> key)
159
173
160
174
public override void AppendHashData ( ReadOnlySpan < byte > data )
161
175
{
162
- _liteHmac . Append ( data ) ;
163
- _running = true ;
176
+ using ( ConcurrencyBlock . Enter ( ref _block ) )
177
+ {
178
+ _liteHmac . Append ( data ) ;
179
+ _running = true ;
180
+ }
164
181
}
165
182
166
183
public override int FinalizeHashAndReset ( Span < byte > destination )
167
184
{
168
- int written = _liteHmac . Finalize ( destination ) ;
169
- _liteHmac . Reset ( ) ;
170
- _running = false ;
171
- return written ;
185
+ using ( ConcurrencyBlock . Enter ( ref _block ) )
186
+ {
187
+ int written = _liteHmac . Finalize ( destination ) ;
188
+ _liteHmac . Reset ( ) ;
189
+ _running = false ;
190
+ return written ;
191
+ }
172
192
}
173
193
174
194
public override int GetCurrentHash ( Span < byte > destination )
175
195
{
176
- return _liteHmac . Current ( destination ) ;
196
+ using ( ConcurrencyBlock . Enter ( ref _block ) )
197
+ {
198
+ return _liteHmac . Current ( destination ) ;
199
+ }
177
200
}
178
201
179
202
public override int HashSizeInBytes => _liteHmac . HashSizeInBytes ;
@@ -188,10 +211,13 @@ public override void Dispose(bool disposing)
188
211
189
212
public override void Reset ( )
190
213
{
191
- if ( _running )
214
+ using ( ConcurrencyBlock . Enter ( ref _block ) )
192
215
{
193
- _liteHmac . Reset ( ) ;
194
- _running = false ;
216
+ if ( _running )
217
+ {
218
+ _liteHmac . Reset ( ) ;
219
+ _running = false ;
220
+ }
195
221
}
196
222
}
197
223
}
0 commit comments