Hashing a string with Sha256

前端 未结 7 2073
一向
一向 2020-12-02 04:31

I try to hash a string using SHA256, I\'m using the following code:

using System;
using System.Security.Cryptography;
using System.Text;
 public class Hash
          


        
7条回答
  •  伪装坚强ぢ
    2020-12-02 04:52

    This work for me in .NET Core 3.1.
    But not in .NET 5 preview 7.

    using System;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace PortalAplicaciones.Shared.Models
    {
        public class Encriptar
        {
            public static string EncriptaPassWord(string Password)
            {
                try
                {
                    SHA256Managed hasher = new SHA256Managed();
    
                    byte[] pwdBytes = new UTF8Encoding().GetBytes(Password);
                    byte[] keyBytes = hasher.ComputeHash(pwdBytes);
    
                    hasher.Dispose();
                    return Convert.ToBase64String(keyBytes);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message, ex);
                }
            }  
        }
    }
     
    

提交回复
热议问题