SHA1字符串加密

使用SHA1算法,生成某个字符串的hash值作为该字符串所代表对象的唯一标识;

Demo:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SHA1Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            var input_str = "客户@编号";
            var output_str = Program.SHA1_Hash(input_str);
            Console.Write($"SHA1 Base64String: {output_str}");
            Console.WriteLine();
            output_str = Program.SHA1(input_str);
            Console.Write($"SHA1 BitConverter: {output_str}\n");
            output_str = Program.HMACSHA1_Hash(input_str,"test");
            Console.Write($"SHA1 HMACSHA1: {output_str}");
            Console.ReadLine();
        }

        public static string SHA1(string content)
        {
            try
            {
                SHA1 sha1 = new SHA1CryptoServiceProvider();
                byte[] bytes_in = UTF8Encoding.UTF8.GetBytes(content);
                byte[] bytes_out = sha1.ComputeHash(bytes_in);
                sha1.Dispose();
                string result = BitConverter.ToString(bytes_out);
                result = result.Replace("-", "").ToLower();
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("SHA1加密出错:" + ex.Message);
            }
        }

        /// <summary>
        /// 哈希Sha1加密输出Base64字符
        /// </summary>
        /// <param name="str_sha1_in"></param>
        /// <returns></returns>
        public static string SHA1_Hash(string str_sha1_in)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes_sha1_in = UTF8Encoding.UTF8.GetBytes(str_sha1_in);
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            string str_sha1_out = Convert.ToBase64String(bytes_sha1_out);
            str_sha1_out = str_sha1_out.Replace("-", "");

            return str_sha1_out;
        }

        /// <summary>
        /// 哈希sha1加密输出字节数组
        /// </summary>
        /// <param name="str_sha1_in"></param>
        /// <returns></returns>
        public static byte[] SHA1_HashBytes(string str_sha1_in)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes_sha1_in = UTF8Encoding.UTF8.GetBytes(str_sha1_in);
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            return bytes_sha1_out;
        }

        public static string HMACSHA1_Hash(string strText,string strKey)
        {
            HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(strKey));
            byte[] byteText = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(strText));
            return System.Convert.ToBase64String(byteText);

        }
    }
}

输出:

SHA1 Base64String: 06YnDCZ3gXWBReR5dHgtY56A438=
SHA1 BitConverter: d3a6270c267781758145e47974782d639e80e37f
SHA1 HMACSHA1: HrVyvnnPgMulvCBaaIP2gQUgJtM=

相关学习参考链接:

https://www.cnblogs.com/fullsail/archive/2013/02/22/2921505.html

https://blog.csdn.net/weixin_38756990/article/details/72177367

 

posted on 2022-04-29 09:13  积跬步---行千里  阅读(247)  评论(0编辑  收藏  举报