zoukankan      html  css  js  c++  java
  • MD5

    using (var md5 = MD5.Create())
    {
        using (var stream = File.OpenRead(filename))
        {
            return md5.ComputeHash(stream);
        }
    }
    or
    public static string GetMD5HashFromFile(string filename)
    {
        using (var md5 = new MD5CryptoServiceProvider())
        {
            var buffer = md5.ComputeHash(File.ReadAllBytes(filename));
            var sb = new StringBuilder();
            for (int i = 0; i < buffer.Length; i++)
            {
                sb.Append(buffer[i].ToString("x2"));
            }
            return sb.ToString();
        }
    }
    

    (I believe that actually the MD5 implementation used doesn't need to be disposed, but I'd probably still do so anyway.)

    How you compare the results afterwards is up to you; you can convert the byte array to base64 for example, or compare the bytes directly. (Just be aware that arrays don't override Equals. Using base64 is simpler to get right, but slightly less efficient if you're really only interested in comparing the hashes.)

    If you want the "standard" looking md5, you can do: return

    BitConverter.ToString(md5.ComputeHash(stream)).Replace("-","").ToLower();

    判断一字符串是否是MD5字符串

    Regex: [0-9a-f]{32}

  • 相关阅读:
    2. C++ continue
    1. Vector
    1007. 行相等的最少多米诺旋转
    4. 寻找两个正序数组的中位数
    3.无重复字符的最长子串
    1. 两数之和
    509. 斐波那契数
    Linux内核源码分析之setup_arch (三)
    1018-可被5整除的二进制前缀
    605-种花问题
  • 原文地址:https://www.cnblogs.com/zeroone/p/3861161.html
Copyright © 2011-2022 走看看