1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:38:47 +00:00

Add StreamExtensions

GetHash<T>() computes the hash of the given stream using the specified
hashing algorithm.
This commit is contained in:
Oliver Booth 2019-11-16 02:00:05 +00:00
parent a01ef63ab0
commit ac10a5912e
No known key found for this signature in database
GPG Key ID: 4B0992B2602C3778
2 changed files with 31 additions and 0 deletions

30
X10D/StreamExtensions.cs Normal file
View File

@ -0,0 +1,30 @@
namespace X10D
{
#region Using Directives
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
#endregion
/// <summary>
/// A set of extension methods for <see cref="Stream"/>.
/// </summary>
public static class StreamExtensions
{
/// <summary>
/// Returns the hash of a stream using the specified hashing algorithm.
/// </summary>
/// <typeparam name="T">A <see cref="HashAlgorithm"/> derived type.</typeparam>
/// <param name="stream">The stream whose hash is to be computed.</param>
/// <returns>Returns a <see cref="Byte"/> array representing the hash of the stream.</returns>
public static byte[] GetHash<T>(this Stream stream) where T : HashAlgorithm
{
MethodInfo create = typeof(T).GetMethod("Create", new Type[] { });
using T crypt = (T)create?.Invoke(null, null);
return crypt?.ComputeHash(stream);
}
}
}

View File

@ -58,6 +58,7 @@
<Compile Include="ListExtensions.cs" /> <Compile Include="ListExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RandomExtensions.cs" /> <Compile Include="RandomExtensions.cs" />
<Compile Include="StreamExtensions.cs" />
<Compile Include="StringExtensions.cs" /> <Compile Include="StringExtensions.cs" />
<Compile Include="StructExtensions.cs" /> <Compile Include="StructExtensions.cs" />
<Compile Include="TimeSpanExtensions.cs" /> <Compile Include="TimeSpanExtensions.cs" />