C# 获取 sha256, 输入可以是 字符串,也可以是 字节流流:

自定义的输入类型的枚举:

        public enum Sha26ParseType
        {
            StringType,
            StreamType
        }

 

核心代码:

    public static string general_sha256_code(string str, Sha26ParseType type) {
            string result = string.Empty;
            byte[] by = null;
            //求字节流的SHA256
            if (type.Equals(Sha26ParseType.StreamType)) {
                if (!System.IO.File.Exists(str))
                    return result;

                System.IO.FileStream stream = new System.IO.FileStream(str, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.Security.Cryptography.SHA256Managed Sha256 = new System.Security.Cryptography.SHA256Managed();
                by = Sha256.ComputeHash(stream);
                stream.Close();
            }
            //求字符串的SHA256
            else {
                byte[] SHA256Data = Encoding.UTF8.GetBytes(str);

                System.Security.Cryptography.SHA256Managed Sha256 = new System.Security.Cryptography.SHA256Managed();
                by = Sha256.ComputeHash(SHA256Data);
            }

            result = BitConverter.ToString(by).Replace("-", "").ToLower(); //64
            //return Convert.ToBase64String(by);                         //44

            return result;
        }

 



 

相关文章:

  • 2021-04-08
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2021-11-23
猜你喜欢
  • 2022-01-30
  • 2021-03-31
  • 2021-09-03
  • 2021-10-12
  • 2022-12-23
  • 2022-01-05
相关资源
相似解决方案