From ac10a5912e72616c441253525b8cf74dd14877ad Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 16 Nov 2019 02:00:05 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20StreamExtensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GetHash() computes the hash of the given stream using the specified hashing algorithm. --- X10D/StreamExtensions.cs | 30 ++++++++++++++++++++++++++++++ X10D/X10D.csproj | 1 + 2 files changed, 31 insertions(+) create mode 100644 X10D/StreamExtensions.cs diff --git a/X10D/StreamExtensions.cs b/X10D/StreamExtensions.cs new file mode 100644 index 0000000..5576aca --- /dev/null +++ b/X10D/StreamExtensions.cs @@ -0,0 +1,30 @@ +namespace X10D +{ + #region Using Directives + + using System; + using System.IO; + using System.Reflection; + using System.Security.Cryptography; + + #endregion + + /// + /// A set of extension methods for . + /// + public static class StreamExtensions + { + /// + /// Returns the hash of a stream using the specified hashing algorithm. + /// + /// A derived type. + /// The stream whose hash is to be computed. + /// Returns a array representing the hash of the stream. + public static byte[] GetHash(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); + } + } +} diff --git a/X10D/X10D.csproj b/X10D/X10D.csproj index 67c97df..841cc56 100644 --- a/X10D/X10D.csproj +++ b/X10D/X10D.csproj @@ -58,6 +58,7 @@ +