GIS数据共享:官方网站

C#

当前位置:首页 > language > C#

C# 获取 sha256

C# 获取 sha256, 输入可以是 字符串,也可以是 字节流流:自定义的输入类型的枚举: public enum Sha26ParseType { StringType, ...

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;
        }


扫码查看

上一篇:已经是第一篇

下一篇:C#获取网速

相关内容

文章评论

表情

共 0 条评论,查看全部
  • 这篇文章还没有收到评论,赶紧来抢沙发吧~

评论排行榜

热门标签