7
7
using System . Threading ;
8
8
using System . Threading . Tasks ;
9
9
10
+ #pragma warning disable CS8500 // takes address of managed type
11
+
10
12
namespace System . Security . Cryptography
11
13
{
12
14
/// <summary>
@@ -64,11 +66,16 @@ public void AppendData(byte[] data)
64
66
/// </summary>
65
67
/// <param name="data">The data to process.</param>
66
68
/// <exception cref="ObjectDisposedException">The object has already been disposed.</exception>
67
- public void AppendData ( ReadOnlySpan < byte > data )
69
+ public unsafe void AppendData ( ReadOnlySpan < byte > data )
68
70
{
69
71
CheckDisposed ( ) ;
70
72
71
- _hashProvider . Append ( data ) ;
73
+ ConcurrentGuard < IntPtr , object ? > ( ( IntPtr ) ( & data ) , static ( IntPtr dataPointer , ref LiteXof hashProvider ) =>
74
+ {
75
+ ReadOnlySpan < byte > data = * ( ReadOnlySpan < byte > * ) dataPointer ;
76
+ hashProvider . Append ( data ) ;
77
+ return null ;
78
+ } ) ;
72
79
}
73
80
74
81
/// <summary>
@@ -87,10 +94,13 @@ public byte[] GetHashAndReset(int outputLength)
87
94
ArgumentOutOfRangeException . ThrowIfNegative ( outputLength ) ;
88
95
CheckDisposed ( ) ;
89
96
90
- byte [ ] output = new byte [ outputLength ] ;
91
- _hashProvider . Finalize ( output ) ;
92
- _hashProvider . Reset ( ) ;
93
- return output ;
97
+ return ConcurrentGuard < int , byte [ ] > ( outputLength , static ( int outputLength , ref LiteXof hashProvider ) =>
98
+ {
99
+ byte [ ] output = new byte [ outputLength ] ;
100
+ hashProvider . Finalize ( output ) ;
101
+ hashProvider . Reset ( ) ;
102
+ return output ;
103
+ } ) ;
94
104
}
95
105
96
106
/// <summary>
@@ -100,12 +110,17 @@ public byte[] GetHashAndReset(int outputLength)
100
110
/// <param name="destination">The buffer to fill with the hash.</param>
101
111
/// <exception cref="ObjectDisposedException">The object has already been disposed.</exception>
102
112
/// <seealso cref="GetCurrentHash(Span{byte})" />
103
- public void GetHashAndReset ( Span < byte > destination )
113
+ public unsafe void GetHashAndReset ( Span < byte > destination )
104
114
{
105
115
CheckDisposed ( ) ;
106
116
107
- _hashProvider . Finalize ( destination ) ;
108
- _hashProvider . Reset ( ) ;
117
+ ConcurrentGuard < IntPtr , object ? > ( ( IntPtr ) ( & destination ) , static ( IntPtr destinationPointer , ref LiteXof hashProvider ) =>
118
+ {
119
+ Span < byte > destination = * ( Span < byte > * ) destinationPointer ;
120
+ hashProvider . Finalize ( destination ) ;
121
+ hashProvider . Reset ( ) ;
122
+ return null ;
123
+ } ) ;
109
124
}
110
125
111
126
/// <summary>
@@ -124,9 +139,12 @@ public byte[] GetCurrentHash(int outputLength)
124
139
ArgumentOutOfRangeException . ThrowIfNegative ( outputLength ) ;
125
140
CheckDisposed ( ) ;
126
141
127
- byte [ ] output = new byte [ outputLength ] ;
128
- _hashProvider . Current ( output ) ;
129
- return output ;
142
+ return ConcurrentGuard < int , byte [ ] > ( outputLength , static ( int outputLength , ref LiteXof hashProvider ) =>
143
+ {
144
+ byte [ ] output = new byte [ outputLength ] ;
145
+ hashProvider . Current ( output ) ;
146
+ return output ;
147
+ } ) ;
130
148
}
131
149
132
150
/// <summary>
@@ -136,10 +154,16 @@ public byte[] GetCurrentHash(int outputLength)
136
154
/// <param name="destination">The buffer to fill with the hash.</param>
137
155
/// <exception cref="ObjectDisposedException">The object has already been disposed.</exception>
138
156
/// <seealso cref="GetHashAndReset(Span{byte})" />
139
- public void GetCurrentHash ( Span < byte > destination )
157
+ public unsafe void GetCurrentHash ( Span < byte > destination )
140
158
{
141
159
CheckDisposed ( ) ;
142
- _hashProvider . Current ( destination ) ;
160
+
161
+ ConcurrentGuard < IntPtr , object ? > ( ( IntPtr ) ( & destination ) , static ( IntPtr destinationPointer , ref LiteXof hashProvider ) =>
162
+ {
163
+ Span < byte > destination = * ( Span < byte > * ) destinationPointer ;
164
+ hashProvider . Current ( destination ) ;
165
+ return null ;
166
+ } ) ;
143
167
}
144
168
145
169
/// <summary>
0 commit comments