From 45c2cd9979e44e0d813e5b78cee20258499df73e Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Mon, 6 Jan 2020 00:21:25 -0500 Subject: [PATCH] incorporate MBS.Security.Cryptography extensions into MBS.Framework since it doesn't necessitate a separate library --- MBS.Framework/MBS.Framework.csproj | 2 ++ .../Security/Cryptography/ExtensionMethods.cs | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 MBS.Framework/Security/Cryptography/ExtensionMethods.cs diff --git a/MBS.Framework/MBS.Framework.csproj b/MBS.Framework/MBS.Framework.csproj index 4cb50ff..da6beef 100644 --- a/MBS.Framework/MBS.Framework.csproj +++ b/MBS.Framework/MBS.Framework.csproj @@ -11,6 +11,7 @@ 2.0 true ..\..\MichaelBecker.snk + 4.0.2019.12 true @@ -48,6 +49,7 @@ + diff --git a/MBS.Framework/Security/Cryptography/ExtensionMethods.cs b/MBS.Framework/Security/Cryptography/ExtensionMethods.cs new file mode 100644 index 0000000..a1c4593 --- /dev/null +++ b/MBS.Framework/Security/Cryptography/ExtensionMethods.cs @@ -0,0 +1,27 @@ +using System.Security.Cryptography; + +namespace MBS.Framework.Security.Cryptography +{ + public static class ExtensionMethods + { + /// + /// Computes the hash of the specified value in the given encoding. + /// + /// The hash. + /// Value. + public static string ComputeHash(this HashAlgorithm ha, string value, System.Text.Encoding encoding = null) + { + if (encoding == null) encoding = System.Text.Encoding.UTF8; + + byte[] buffer = encoding.GetBytes(value); + + System.Text.StringBuilder sb = new System.Text.StringBuilder(); + byte[] output = ha.ComputeHash(buffer); + for (int i = 0; i < output.Length; i++) + { + sb.Append(output[i].ToString("x").PadLeft(2, '0')); + } + return sb.ToString(); + } + } +}