Hash class using System.Security.Cryptography

Adding for archival purposes…

<span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>System.Web;
<span style="color: blue">using </span>System.Security.Cryptography;

<span style="color: blue">namespace </span>Classes
{
    <span style="color: blue">public class </span><span style="color: #2b91af">Hash
    </span>{
        <span style="color: blue">public </span>Hash()
        { }

        <span style="color: blue">#region </span>Hash Choices
        <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">The wanted hash function.</span><span style="color: gray">&lt;/summary&gt;
        </span><span style="color: blue">public enum </span><span style="color: #2b91af">HashType </span>: <span style="color: blue">int
        </span>{
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">MD5 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>MD5,
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">SHA1 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>SHA1,
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">SHA256 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>SHA256,
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">SHA384 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>SHA384,
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">SHA512 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>SHA512
        } <span style="color: green">/* HashType */
        </span><span style="color: blue">#endregion

        #region </span>Public Methods
        <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">Generates the hash of a text.</span><span style="color: gray">&lt;/summary&gt;
        /// &lt;param name=&quot;strPlain&quot;&gt;</span><span style="color: green">The text of which to generate a hash of.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;param name=&quot;hshType&quot;&gt;</span><span style="color: green">The hash function to use.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;returns&gt;</span><span style="color: green">The hash as a hexadecimal string.</span><span style="color: gray">&lt;/returns&gt;
        </span><span style="color: blue">public static string </span>GetHash(<span style="color: blue">string </span>strPlain, <span style="color: #2b91af">HashType </span>hshType)
        {
            <span style="color: blue">string </span>strRet;
            <span style="color: blue">switch </span>(hshType)
            {
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.MD5: 
                    strRet = GetMD5(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.SHA1: 
                    strRet = GetSHA1(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.SHA256: 
                    strRet = GetSHA256(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.SHA384: 
                    strRet = GetSHA384(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.SHA512: 
                    strRet = GetSHA512(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">default</span>: 
                    strRet = <span style="color: #a31515">&quot;Invalid HashType&quot;</span>; 
                    <span style="color: blue">break</span>;
            }
            <span style="color: blue">return </span>strRet;
        } <span style="color: green">/* GetHash */

        </span><span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">Checks a text with a hash.</span><span style="color: gray">&lt;/summary&gt;
        /// &lt;param name=&quot;strOriginal&quot;&gt;</span><span style="color: green">The text to compare the hash against.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;param name=&quot;strHash&quot;&gt;</span><span style="color: green">The hash to compare against.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;param name=&quot;hshType&quot;&gt;</span><span style="color: green">The type of hash.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;returns&gt;</span><span style="color: green">True if the hash validates, false otherwise.</span><span style="color: gray">&lt;/returns&gt;
        </span><span style="color: blue">public static bool </span>CheckHash(<span style="color: blue">string </span>strOriginal, <span style="color: blue">string </span>strHash, <span style="color: #2b91af">HashType </span>hshType)
        {
            <span style="color: blue">string </span>strOrigHash = GetHash(strOriginal, hshType);
            <span style="color: blue">return </span>(strOrigHash == strHash);
        } <span style="color: green">/* CheckHash */
        </span><span style="color: blue">#endregion

        #region </span>Hashers
        <span style="color: blue">private static string </span>GetMD5(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">MD5 </span>md5 = <span style="color: blue">new </span><span style="color: #2b91af">MD5CryptoServiceProvider</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = md5.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetMD5 */

        </span><span style="color: blue">private static string </span>GetSHA1(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">SHA1Managed </span>SHhash = <span style="color: blue">new </span><span style="color: #2b91af">SHA1Managed</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = SHhash.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetSHA1 */

        </span><span style="color: blue">private static string </span>GetSHA256(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">SHA256Managed </span>SHhash = <span style="color: blue">new </span><span style="color: #2b91af">SHA256Managed</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = SHhash.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetSHA256 */

        </span><span style="color: blue">private static string </span>GetSHA384(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">SHA384Managed </span>SHhash = <span style="color: blue">new </span><span style="color: #2b91af">SHA384Managed</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = SHhash.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetSHA384 */

        </span><span style="color: blue">private static string </span>GetSHA512(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">SHA512Managed </span>SHhash = <span style="color: blue">new </span><span style="color: #2b91af">SHA512Managed</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = SHhash.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetSHA512 */
        </span><span style="color: blue">#endregion
    </span>}
}

Leave a Reply

Your email address will not be published. Required fields are marked *