当前位置:首页 > 开发 > C# > 正文内容

C# 获取MD5

C#3年前 (2022-10-30)
命名空间    
using System.Security.Cryptography;


GetMd5    
        public static string GetMd5(string filePath)
        {
            try
            {
                StringBuilder sb = new StringBuilder();

                using (FileStream file = new FileStream(filePath, FileMode.Open))
                {
                    MD5 md5 = new MD5CryptoServiceProvider();
                    byte[] retVal = md5.ComputeHash(file);
                    foreach (byte item in retVal)
                    {
                        sb.Append(item.ToString("x2"));
                    }
                    md5.Dispose();
                }
                return sb.ToString().ToUpper();
            }
            catch (Exception)
            {
            }
            return String.Empty;
        }


GetMd5    
        public static string GetMd5(byte[] bits)
        {
            try
            {
                
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(bits);
                md5.Dispose();
                StringBuilder sb = new StringBuilder();
                foreach (byte item in retVal)
                {
                    sb.Append(item.ToString("x2"));
                }
                return sb.ToString().ToUpper();
            }
            catch (Exception)
            {
            }
            return String.Empty;
        }




转载请注明出处。

本文链接:http://pythonopen.com/?id=238

返回列表

上一篇:C# MDI例子

下一篇:C# 冒泡排序

相关文章

C# 数据类型

Type ByteLenghtMinMax.NET Framework Typedefau...

C# 缩减代码量的一些方式

static void Main() { Thread thre...

C# [OnPaint]和[OnPaintBackground]的区别

OnPaint和OnPaintBackground的主要功能区别OnPaint:OnPaint方法主...

C# double转为string并保留两位小数

在 C# 中,可以使用多种方式将 double 类型的数据转换为 string 类型并保留两位小数,...

C# System.IO.Path

System.IO.Path.GetExtension返回指定的路径字符串的扩展名。string&n...

C# Winform 拖放文件

private void Form1_Load(object send...