-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamExtensions.cs
More file actions
34 lines (26 loc) · 953 Bytes
/
StreamExtensions.cs
File metadata and controls
34 lines (26 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Security.Cryptography;
namespace Shared
{
public static class StreamExtensions
{
public static async Task<string> ReadToEndAsync(this Stream value)
{
value.Position = 0;
using var streamReader = new StreamReader(value, leaveOpen: true);
string valueString = await streamReader.ReadToEndAsync();
value.Position = 0;
return valueString;
}
public static string ComputeMd5Checksum(this Stream stream)
{
if (stream is null || stream.Length == 0)
throw new ArgumentException("No content.");
using MD5 md5 = MD5.Create();
stream.Position = 0;
ReadOnlySpan<byte> hash = md5.ComputeHash(stream);
if (hash.IsEmpty)
throw new ArgumentException($"Failed to compute checksum.");
return Convert.ToHexString(hash);
}
}
}