Hmac-sha256 & MD5 in C #

Source: Internet
Author: User
Tags hmac


two commonly used encryption methods in C #:


Personal mark, for reference only.





Public static class Extends
    {
        /// <summary>
        /// HMAC SHA256
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        Public static string Sha256(this string str)
        {
            Byte[] sha256Data = Encoding.UTF8.GetBytes(str);
            SHA256Managed sha256 = new SHA256Managed();
            Byte[] buffer = sha256.ComputeHash(sha256Data);
            // You can process the encrypted byte array as needed, for example using Base64. Use BitConverter to convert to 64 characters.
            Return BitConverter.ToString(buffer).Replace("-", "").ToLower();
        }

        /// <summary>
        /// MD5
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        Public static string Md5(this string str)
        {
            Var md5 = MD5.Create();// Encrypted is an array of byte types
            Byte[] buffer = md5.ComputeHash(Encoding.UTF8.GetBytes(str));

            // The resulting string is in hexadecimal type format. The characters after the format are lowercase letters. If uppercase (X) is used, the characters after the format are uppercase characters.
            Return buffer.Aggregate(string.Empty, (current, t) => current + t.ToString("x"));
        }
    } 





Hmac-sha256 & MD5 in C #


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.