MD5, SHA256, SHA512 hash algorithm __ algorithm

Source: Internet
Author: User
Tags md5 hash sha256 algorithm
StringHasher.cs
    <summary>///class///</summary> public class Stringhasher {///to implement various string hash hashing algorithms <summary>///Delegate Hash method to achieve a clear hashing algorithm///</summary>///<param name= "Stringtohash" ; string </param>///<returns>hash hashed string </returns> Private delegate String Hashalgorith

        Method (string Stringtohash); <summary>///using MD5 algorithm hash string///</summary>///<param name= "Stringtohash" > Pending hash String .</param>///<returns>hash after the hash .</returns> public string Md5computehash (string St
            Ringtohash) {string results; using (var md5algorithm = MD5.
                Create ()) {results = ComputeHash (Md5algorithm, Stringtohash);
            Disposealgorithm (Md5algorithm);
        return results; ///<summary>///Adds a randomly generated salt to a string, and then uses the MD5 algorithm toThe string is hashed.
        The length of the salt is the same length as the algorithm generates///</summary>///<param name= "Stringtohash" > Strings to be hashed .</param>
        <param name= "Salt" >salt string for password .</param>///hash <returns>hash public string Md5saltcomputehash (string stringtohash, out string salt) {salt = Generaterandomsalt (16
            );
        return Md5computehash (salt + stringtohash);
        }///<summary>///verify string with another MD5 hash string. </summary>///<param name= "Stringtoverify" > string to be validated .</param>///<param name= "has H "&GT;MD5 generates a hash to verify that the .</param>///<returns> string matches a hash of a Boolean value. </returns> public bool Md5verifyhash (string stringtoverify, String hash) {var hashedstr
            ing = Md5computehash (stringtoverify);
        Return aretwostringsequal (hash, hashedstring); }///<summary>///validation string with another MD5 SAlt Hash String. </summary>///<param name= "Stringtoverify" > string to be validated .</param>///<param name= "has H ">md5 salt-generated hash to validate </param>///<param name=" Salt ">salt for password </param>///<returns& gt; Boolean value that matches the salt hash </returns> public bool Md5saltverifyhash (string stringtoverify, String hash, string sal
        T) {return Verifysalthash (stringtoverify, hash, salt, md5computehash); ///<summary>///using SHA256 algorithm hash string///</summary>///<param name= "Stri Ngtohash "> to hash string .</param>///<returns>hash Hash string .</returns> public string Sha256co
            Mputehash (String stringtohash) {string results; using (var sha256algorithm = new SHA256Managed ()) {results = ComputeHash (Sha256algorithm, str
                Ingtohash);
      Disposealgorithm (Sha256algorithm);      return results;  ///<summary>///adds randomly generated salt to a string, and then hashes the string using the SHA256 algorithm///the length of the salt is the same length as the algorithm generates/// </summary>///<param name= "Stringtohash" > to hash string .</param>///<param name= "Salt" ; Salt is used for password .</param>///<returns>hash after hashing the string .</returns> public string Sha256saltcomputeha
            SH (string stringtohash, out string salt) {salt = Generaterandomsalt (32);
        return Sha256computehash (salt + stringtohash);
        }///<summary>///verify string with another SHA256 hash string. </summary>///<param name= "Stringtoverify" > string to be validated .</param>///<param name= "has H ">sha256 generates a hash to verify that the .</param>///<returns> string matches a hash of a Boolean value .</returns> public bool Sha256ver Ifyhash (string stringtoverify, String hash) {var hashedstring = Sha256computehash (stringToverify);
        Return aretwostringsequal (hash, hashedstring); }///<summary>///validation string with another SHA256 salt hash string///</summary>///<param Nam
        E= "Stringtoverify" > the string to be validated .</param>///<param name= "hash" >sha256 salt generates a hash to validate </param>
        <param name= "Salt" >salt for passwords .</param>///<returns> strings are Boolean values that match the salt hash </returns> public bool Sha256saltverifyhash (string stringtoverify, string hash, string salt) {return Verifysa
        Lthash (stringtoverify, hash, salt, sha256computehash);
        ///<summary>///uses the SHA512 algorithm hash string. </summary>///<param name= "Stringtohash" > String to hash .</param>///<returns>hash The hash string .</returns> public string Sha512computehash (string stringtohash) {string results
            ; using (var sha512algorithm = new Sha512manageD ()) {results = ComputeHash (Sha512algorithm, Stringtohash);
            Disposealgorithm (Sha512algorithm);
        return results;
        ///<summary>///Adds a randomly generated salt to a string, and then hashes the string using the SHA512 algorithm///salt is the same length as the algorithm generates. </summary>///<param name= "Stringtohash" > String to Hash .</param>///<param name= "Salt" >salt for password .</param>///<returns>hash hashed string .</returns> public string Sha512saltcomput
            Ehash (String stringtohash, out string salt) {salt = Generaterandomsalt (64);
        return Sha512computehash (salt + stringtohash); ///<returns> Boolean value that indicates whether the string matches the hash.
        </returns>///<summary>///verifies the string pair with another SHA512 hash string. </summary>///<param name= "Stringtoverify" > string to be validated .</param>///<param name= "has H ">sha512 the generated hash to validate. &LT;/param>///<returns> String is a Boolean value that matches the hash .</returns> public bool Sha512verifyhash (string stringt
            Overify, String hash) {var hashedstring = Sha512computehash (stringtoverify);
        Return aretwostringsequal (hash, hashedstring);
        ///<summary>///validates the string with another SHA512 salt hash string. </summary>///<param name= "Stringtoverify" > string to be validated .</param>///<param name= "has H ">sha512 salt-generated hash to verify .</param>///<param name=" Salt ">salt as password .</param>/// Rns> string is a Boolean value that matches the salt hash .</returns> public bool Sha512saltverifyhash (string stringtoverify, string hash, St
        Ring salt) {return Verifysalthash (stringtoverify, hash, salt, sha512computehash); ///<summary>///computes the hash value of a string using a passed hash algorithm///</summary>///<param name= "has Halgorithm "> Hash algorithm for calculation. </param>///<param name= "Stringtohash" > String to be hashed .</param> after///. <returns>hash ns> private static string ComputeHash (HashAlgorithm hashalgorithm, string stringtohash) {V
            AR results = Hashalgorithm.computehash (Encoding.UTF8.GetBytes (Stringtohash));
        return bytestohexadecimalstring (results);
        ///<summary>///validates the string with another salt hash string using the passed hash algorithm. </summary>///<param name = "Stringtoverify" > String to validate. </param>///<param name = "Hash" > generated salt hash to validate. </param>///<param name = "Salt" > salt is used for passwords. </param>///<param name = "Algorithmtoinvoke" > Hash algorithm for validation. </param>///<returns> string is a Boolean value that matches the salt hash </returns> private static bool Verifysalthash ( String stringtoverify, string hash, string salt, Hashalgorithmethod algorithmtoinvoke) {var hashedstr ing = ALGOrithmtoinvoke (salt + stringtoverify);
        Return aretwostringsequal (hash, hashedstring); ///<summary>///processing hash algorithm///</summary>///<param name= "HashAlgorithm" &
            gt; released hash algorithm .</param> private static void Disposealgorithm (HashAlgorithm hashalgorithm) {
        Hashalgorithm.clear (); ///<summary>///generate random salt///</summary>///<param name= for a specified length Saltleng Th "> produces salt length </param>///<returns> randomly generated salt.</returns> private static string Generat
            Erandomsalt (int saltlength) {var random = new RNGCryptoServiceProvider ();
            var salt = new Byte[saltlength]; Random.
            GetBytes (salt);
        return bytestohexadecimalstring (salt);
        ///<summary>///Converts the given byte array to the appropriate hexadecimal string. </summary>///<param name = "Bytestoconvert"> The bytes array to convert. </param>///<returns> corresponding string representation. </returns> private static string bytestohexadecimalstring (byte[] bytestoconvert) {var

            hexadecimalstring = new StringBuilder (); for (var i = 0; i < bytestoconvert.length i++) {hexadecimalstring.append (bytestoconvert[i ].
            ToString ("X2"));
        return hexadecimalstring.tostring ();
        ///<summary>///Compares two hash values, ignoring their case. </summary>///<param name = "Hash" > the first hash to compare. </param>///<param name = "Hashedstring" > the second hash to compare. </param>///<returns> A Boolean value that indicates whether the hash is equal. </returns> private static bool Aretwostringsequal (string hash, string hashedstring) {R Eturn Hash.
        Equals (hashedstring, stringcomparison.ordinalignorecase); }
    }
Program.cs Code:
Class Program {static void Main (string[] args) {const string stringtohash = ' HelloWorld '
            ;
            string Salt;

            var hasher = new Stringhasher (); var Md5hash = Hasher.
            Md5computehash (Stringtohash);
            Console.WriteLine ("Md5hash string:" +md5hash);
            Console.WriteLine ("Md5hash Length:" + md5hash.length); Console.WriteLine ("Md5verifyhash check:" + hasher.

            Md5verifyhash (Stringtohash, Md5hash));
            Console.WriteLine ("---------------------------------------------------"); Md5hash = Hasher.
            Md5saltcomputehash (Stringtohash, out salt);
            Console.WriteLine ("Md5hash string:" + Md5hash);
            Console.WriteLine ("Md5hash Length:" + md5hash.length); Console.WriteLine ("Md5saltverifyhash check:" + hasher.
            Md5saltverifyhash (Stringtohash, Md5hash, salt));
            Console.WriteLine ("---------------------------------------------------"); var Sha256hash = Hasher. Sha256computehash (Stringtohash);
            Console.WriteLine ("Sha256hash string:" + Sha256hash);
            Console.WriteLine ("Sha256hash Length:" + sha256hash.length); Console.WriteLine ("Sha256verifyhash check:" + hasher.

            Sha256verifyhash (Stringtohash, Sha256hash));
            Console.WriteLine ("---------------------------------------------------"); Sha256hash = Hasher.  Sha256saltcomputehash (Stringtohash,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.