• NET Core 通过扩展方法实现密码字符串加密(Sha256和Sha512)


    using System;
    using System.Security.Cryptography;
    using System.Text;
    namespace SPACore.Extensions
    {
        /// <summary>
        /// Extension methods for hashing strings
        /// </summary>
        public static class HashExtensions
        {
          static void Main(string[] args)
          {
            var secretString = Guid.NewGuid().ToString("N");
             Console.WriteLine($"Guid:{secretString}");
            var wetet = Sha256(secretString);
            Console.WriteLine($"Sha256:{wetet}");
            var sha512 = Sha512(secretString);
             Console.WriteLine($"Sha512:{wetet}");
            Console.ReadKey();
          }
            /// <summary>
            /// Creates a SHA256 hash of the specified input.
            /// </summary>
            /// <param name="input">The input.</param>
            /// <returns>A hash</returns>
            public static string Sha256(this string input)
            {
                if (String.IsNullOrEmpty(input)) return string.Empty;
                using (var sha = SHA256.Create())
                {
                    var bytes = Encoding.UTF8.GetBytes(input);
                    var hash = sha.ComputeHash(bytes);
                    return Convert.ToBase64String(hash);
                }
            }
            /// <summary>
            /// Creates a SHA256 hash of the specified input.
            /// </summary>
            /// <param name="input">The input.</param>
            /// <returns>A hash.</returns>
            public static byte[] Sha256(this byte[] input)
            {
                if (input == null)
                {
                    return null;
                }
                using (var sha = SHA256.Create())
                {
                    return sha.ComputeHash(input);
                }
            }
            /// <summary>
            /// Creates a SHA512 hash of the specified input.
            /// </summary>
            /// <param name="input">The input.</param>
            /// <returns>A hash</returns>
            public static string Sha512(this string input)
            {
                if (string.IsNullOrEmpty(input)) return string.Empty;
                using (var sha = SHA512.Create())
                {
                    var bytes = Encoding.UTF8.GetBytes(input);
                    var hash = sha.ComputeHash(bytes);
                    return Convert.ToBase64String(hash);
                }
            }
        }
    }
    
    
    
  • 相关阅读:
    JOptionPane&&Exception的使用
    CppUnit在VS2010上的正确使用
    怎样认识比你优秀的人并和他们成为朋友呢?
    二十岁出头的时候上,你一无所有,你拥有一切。
    C语言实现文件复制
    关于二维数组可以开多大
    exit(0)与exit(1)、return区别
    学语言究竟学什么?
    当oracle出现 格式与字符串格式不匹配解决办法
    javascript的系统函数
  • 原文地址:https://www.cnblogs.com/moonstars/p/15983479.html
Copyright © 2020-2023  润新知