zoukankan      html  css  js  c++  java
  • C#获取文件/字节数组MD5值方法

    找了很多,就这个管用,有时间好好研究一番

    public static string GetMD5Hash(string fileName)
    {
    try
    {
    FileStream file = new FileStream(fileName, FileMode.Open);
    System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    byte[] retVal = md5.ComputeHash(file);
    file.Close();

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < retVal.Length; i++)
    {
    sb.Append(retVal[i].ToString("x2"));
    }
    return sb.ToString();
    }
    catch (Exception ex)
    {
    throw new Exception("GetMD5Hash() fail,error:" + ex.Message);
    }
    }

    public static string GetMD5Hash(byte[] bytedata)
    {
    try
    {
    System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    byte[] retVal = md5.ComputeHash(bytedata);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < retVal.Length; i++)
    {
    sb.Append(retVal[i].ToString("x2"));
    }
    return sb.ToString();
    }
    catch (Exception ex)
    {
    throw new Exception("GetMD5Hash() fail,error:" + ex.Message);
    }
    }

    来源网址:

    http://blog.csdn.net/snakorse/article/details/19578519

  • 相关阅读:
    75. 颜色分类
    排序链表
    两个数组的交集
    242. 有效的字母异位词
    排序优化
    622.设计循环队列
    比较含退格的字符串
    682.棒球比赛
    496.下一个更大的元素I
    线性排序算法
  • 原文地址:https://www.cnblogs.com/hxh88/p/5909026.html
Copyright © 2011-2022 走看看