Hashing a string with Sha256

前端 未结 7 2065
一向
一向 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:58

    In the PHP version you can send 'true' in the last parameter, but the default is 'false'. The following algorithm is equivalent to the default PHP's hash function when passing 'sha256' as the first parameter:

    public static string GetSha256FromString(string strData)
        {
            var message = Encoding.ASCII.GetBytes(strData);
            SHA256Managed hashString = new SHA256Managed();
            string hex = "";
    
            var hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }
    

提交回复
热议问题