mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 19:58:49 +00:00
✨ Add StreamExtensions
GetHash<T>() computes the hash of the given stream using the specified hashing algorithm.
This commit is contained in:
parent
a01ef63ab0
commit
ac10a5912e
30
X10D/StreamExtensions.cs
Normal file
30
X10D/StreamExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -58,6 +58,7 @@
|
||||
<Compile Include="ListExtensions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="RandomExtensions.cs" />
|
||||
<Compile Include="StreamExtensions.cs" />
|
||||
<Compile Include="StringExtensions.cs" />
|
||||
<Compile Include="StructExtensions.cs" />
|
||||
<Compile Include="TimeSpanExtensions.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user