Skip to content

Commit b968aa9

Browse files
authored
Add HashData polyfills (#443)
1 parent ee362f5 commit b968aa9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+6772
-0
lines changed

api_list.include.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,18 @@
441441
* `ushort Clamp(ushort, ushort, ushort)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.math.clamp?view=net-10.0)
442442

443443

444+
#### MD5
445+
446+
* `byte[] HashData(byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-byte()))
447+
* `int HashData(ReadOnlySpan<byte>, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-readonlyspan((system-byte))-system-span((system-byte))))
448+
* `byte[] HashData(ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-readonlyspan((system-byte))))
449+
* `int HashData(Stream, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-io-stream-system-span((system-byte))))
450+
* `byte[] HashData(Stream)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-io-stream))
451+
* `ValueTask<byte[]> HashDataAsync(Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdataasync?view=net-10.0#system-security-cryptography-md5-hashdataasync(system-io-stream-system-threading-cancellationtoken))
452+
* `ValueTask<int> HashDataAsync(Stream, Memory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdataasync?view=net-10.0#system-security-cryptography-md5-hashdataasync(system-io-stream-system-memory((system-byte))-system-threading-cancellationtoken))
453+
* `bool TryHashData(ReadOnlySpan<byte>, Span<byte>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.tryhashdata?view=net-10.0)
454+
455+
444456
#### MemberInfo
445457

446458
* `NullabilityState GetNullability()`
@@ -615,6 +627,18 @@
615627
* `bool TryParse(string?, IFormatProvider?, sbyte)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.sbyte.tryparse?view=net-10.0#system-sbyte-tryparse(system-string-system-iformatprovider-system-sbyte@))
616628

617629

630+
#### SHA1
631+
632+
* `byte[] HashData(byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha1.hashdata?view=net-10.0#system-security-cryptography-sha1-hashdata(system-byte()))
633+
* `int HashData(ReadOnlySpan<byte>, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha1.hashdata?view=net-10.0#system-security-cryptography-sha1-hashdata(system-readonlyspan((system-byte))-system-span((system-byte))))
634+
* `byte[] HashData(ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha1.hashdata?view=net-10.0#system-security-cryptography-sha1-hashdata(system-readonlyspan((system-byte))))
635+
* `int HashData(Stream, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha1.hashdata?view=net-10.0#system-security-cryptography-sha1-hashdata(system-io-stream-system-span((system-byte))))
636+
* `byte[] HashData(Stream)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha1.hashdata?view=net-10.0#system-security-cryptography-sha1-hashdata(system-io-stream))
637+
* `ValueTask<byte[]> HashDataAsync(Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha1.hashdataasync?view=net-10.0#system-security-cryptography-sha1-hashdataasync(system-io-stream-system-threading-cancellationtoken))
638+
* `ValueTask<int> HashDataAsync(Stream, Memory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha1.hashdataasync?view=net-10.0#system-security-cryptography-sha1-hashdataasync(system-io-stream-system-memory((system-byte))-system-threading-cancellationtoken))
639+
* `bool TryHashData(ReadOnlySpan<byte>, Span<byte>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha1.tryhashdata?view=net-10.0)
640+
641+
618642
#### SHA256
619643

620644
* `byte[] HashData(byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha256.hashdata?view=net-10.0#system-security-cryptography-sha256-hashdata(system-byte()))
@@ -627,6 +651,18 @@
627651
* `bool TryHashData(ReadOnlySpan<byte>, Span<byte>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha256.tryhashdata?view=net-10.0)
628652

629653

654+
#### SHA384
655+
656+
* `byte[] HashData(byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha384.hashdata?view=net-10.0#system-security-cryptography-sha384-hashdata(system-byte()))
657+
* `int HashData(ReadOnlySpan<byte>, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha384.hashdata?view=net-10.0#system-security-cryptography-sha384-hashdata(system-readonlyspan((system-byte))-system-span((system-byte))))
658+
* `byte[] HashData(ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha384.hashdata?view=net-10.0#system-security-cryptography-sha384-hashdata(system-readonlyspan((system-byte))))
659+
* `int HashData(Stream, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha384.hashdata?view=net-10.0#system-security-cryptography-sha384-hashdata(system-io-stream-system-span((system-byte))))
660+
* `byte[] HashData(Stream)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha384.hashdata?view=net-10.0#system-security-cryptography-sha384-hashdata(system-io-stream))
661+
* `ValueTask<byte[]> HashDataAsync(Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha384.hashdataasync?view=net-10.0#system-security-cryptography-sha384-hashdataasync(system-io-stream-system-threading-cancellationtoken))
662+
* `ValueTask<int> HashDataAsync(Stream, Memory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha384.hashdataasync?view=net-10.0#system-security-cryptography-sha384-hashdataasync(system-io-stream-system-memory((system-byte))-system-threading-cancellationtoken))
663+
* `bool TryHashData(ReadOnlySpan<byte>, Span<byte>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha384.tryhashdata?view=net-10.0)
664+
665+
630666
#### SHA512
631667

632668
* `byte[] HashData(byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha512.hashdata?view=net-10.0#system-security-cryptography-sha512-hashdata(system-byte()))

src/Consume/Consume.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,75 @@ void SHA512Usage()
228228
SHA512.HashDataAsync(source: null!, destination: memory);
229229
SHA512.HashDataAsync(source: null!, destination: memory, cancellationToken: CancellationToken.None);
230230
#endif
231+
#endif
232+
}
233+
234+
void SHA1Usage()
235+
{
236+
SHA1.HashData(source: (byte[]) null!);
237+
SHA1.HashData(source: (Stream) null!);
238+
#if FeatureValueTask
239+
SHA1.HashDataAsync(source: null!, cancellationToken: CancellationToken.None);
240+
#endif
241+
#if FeatureMemory
242+
Span<byte> span = default;
243+
ReadOnlySpan<byte> readOnlySpan = default;
244+
Memory<byte> memory = default;
245+
246+
SHA1.HashData(source: (Stream) null!, destination: span);
247+
SHA1.HashData(source: readOnlySpan);
248+
SHA1.HashData(source: readOnlySpan, destination: span);
249+
SHA1.TryHashData(source: readOnlySpan, destination: span, bytesWritten: out _);
250+
#if FeatureValueTask
251+
SHA1.HashDataAsync(source: null!, destination: memory);
252+
SHA1.HashDataAsync(source: null!, destination: memory, cancellationToken: CancellationToken.None);
253+
#endif
254+
#endif
255+
}
256+
257+
void SHA384Usage()
258+
{
259+
SHA384.HashData(source: (byte[]) null!);
260+
SHA384.HashData(source: (Stream) null!);
261+
#if FeatureValueTask
262+
SHA384.HashDataAsync(source: null!, cancellationToken: CancellationToken.None);
263+
#endif
264+
#if FeatureMemory
265+
Span<byte> span = default;
266+
ReadOnlySpan<byte> readOnlySpan = default;
267+
Memory<byte> memory = default;
268+
269+
SHA384.HashData(source: (Stream) null!, destination: span);
270+
SHA384.HashData(source: readOnlySpan);
271+
SHA384.HashData(source: readOnlySpan, destination: span);
272+
SHA384.TryHashData(source: readOnlySpan, destination: span, bytesWritten: out _);
273+
#if FeatureValueTask
274+
SHA384.HashDataAsync(source: null!, destination: memory);
275+
SHA384.HashDataAsync(source: null!, destination: memory, cancellationToken: CancellationToken.None);
276+
#endif
277+
#endif
278+
}
279+
280+
void MD5Usage()
281+
{
282+
MD5.HashData(source: (byte[]) null!);
283+
MD5.HashData(source: (Stream) null!);
284+
#if FeatureValueTask
285+
MD5.HashDataAsync(source: null!, cancellationToken: CancellationToken.None);
286+
#endif
287+
#if FeatureMemory
288+
Span<byte> span = default;
289+
ReadOnlySpan<byte> readOnlySpan = default;
290+
Memory<byte> memory = default;
291+
292+
MD5.HashData(source: (Stream) null!, destination: span);
293+
MD5.HashData(source: readOnlySpan);
294+
MD5.HashData(source: readOnlySpan, destination: span);
295+
MD5.TryHashData(source: readOnlySpan, destination: span, bytesWritten: out _);
296+
#if FeatureValueTask
297+
MD5.HashDataAsync(source: null!, destination: memory);
298+
MD5.HashDataAsync(source: null!, destination: memory, cancellationToken: CancellationToken.None);
299+
#endif
231300
#endif
232301
}
233302

src/Polyfill/MD5Polyfill.cs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#if !NET7_0_OR_GREATER
2+
3+
namespace Polyfills;
4+
5+
using System.Threading.Tasks;
6+
using System.Threading;
7+
using System;
8+
using System.IO;
9+
using System.Security.Cryptography;
10+
using System.Diagnostics;
11+
using System.Diagnostics.CodeAnalysis;
12+
13+
[ExcludeFromCodeCoverage]
14+
[DebuggerNonUserCode]
15+
#if PolyUseEmbeddedAttribute
16+
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
17+
#endif
18+
#if PolyPublic
19+
public
20+
#endif
21+
static class MD5Polyfill
22+
{
23+
extension(MD5)
24+
{
25+
#if !NET
26+
27+
/// <summary>
28+
/// Computes the hash of data using the MD5 algorithm.
29+
/// </summary>
30+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-byte())
31+
public static byte[] HashData(byte[] source)
32+
{
33+
using var hasher = MD5.Create();
34+
return hasher.ComputeHash(source);
35+
}
36+
37+
#endif
38+
39+
/// <summary>
40+
/// Computes the hash of a stream using the MD5 algorithm.
41+
/// </summary>
42+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-io-stream)
43+
public static byte[] HashData(Stream source)
44+
{
45+
using var hasher = MD5.Create();
46+
return hasher.ComputeHash(source);
47+
}
48+
49+
50+
#if FeatureValueTask
51+
/// <summary>
52+
/// Asynchronously computes the hash of a stream using the MD5 algorithm.
53+
/// </summary>
54+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdataasync?view=net-10.0#system-security-cryptography-md5-hashdataasync(system-io-stream-system-threading-cancellationtoken)
55+
public static ValueTask<byte[]> HashDataAsync(Stream source, CancellationToken cancellationToken = default)
56+
{
57+
cancellationToken.ThrowIfCancellationRequested();
58+
using var hasher = MD5.Create();
59+
return new(hasher.ComputeHash(source));
60+
}
61+
#endif
62+
63+
#if FeatureMemory
64+
65+
/// <summary>
66+
/// Computes the hash of a stream using the MD5 algorithm.
67+
/// </summary>
68+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-io-stream-system-span((system-byte)))
69+
public static int HashData(Stream source, Span<byte> destination)
70+
{
71+
var hash = HashData(source);
72+
hash.CopyTo(destination);
73+
return hash.Length;
74+
}
75+
76+
#if !NET
77+
78+
/// <summary>
79+
/// Computes the hash of data using the MD5 algorithm.
80+
/// </summary>
81+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-readonlyspan((system-byte)))
82+
public static byte[] HashData(ReadOnlySpan<byte> source)
83+
{
84+
using var hasher = MD5.Create();
85+
return hasher.ComputeHash(source.ToArray());
86+
}
87+
88+
#endif
89+
90+
#if FeatureValueTask
91+
92+
/// <summary>
93+
/// Asynchronously computes the hash of a stream using the MD5 algorithm.
94+
/// </summary>
95+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdataasync?view=net-10.0#system-security-cryptography-md5-hashdataasync(system-io-stream-system-memory((system-byte))-system-threading-cancellationtoken)
96+
public static ValueTask<int> HashDataAsync(Stream source, Memory<byte> destination, CancellationToken cancellationToken = default)
97+
{
98+
cancellationToken.ThrowIfCancellationRequested();
99+
var hash = HashData(source);
100+
hash.CopyTo(destination);
101+
return new(hash.Length);
102+
}
103+
104+
#endif
105+
106+
#if !NET
107+
108+
/// <summary>
109+
/// Computes the hash of data using the MD5 algorithm.
110+
/// </summary>
111+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.hashdata?view=net-10.0#system-security-cryptography-md5-hashdata(system-readonlyspan((system-byte))-system-span((system-byte)))
112+
public static int HashData(ReadOnlySpan<byte> source, Span<byte> destination)
113+
{
114+
var hash = HashData(source);
115+
hash.CopyTo(destination);
116+
return hash.Length;
117+
}
118+
119+
/// <summary>
120+
/// Attempts to compute the hash of data using the MD5 algorithm.
121+
/// </summary>
122+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5.tryhashdata?view=net-10.0
123+
public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
124+
{
125+
using var hasher = MD5.Create();
126+
var hash = hasher.ComputeHash(source.ToArray());
127+
128+
if (destination.Length < hash.Length)
129+
{
130+
bytesWritten = 0;
131+
return false;
132+
}
133+
134+
hash.CopyTo(destination);
135+
bytesWritten = hash.Length;
136+
return true;
137+
}
138+
139+
#endif
140+
#endif
141+
}
142+
}
143+
#endif

0 commit comments

Comments
 (0)