예제 #1
0
        /// <summary>
        /// 根据Stream计算MD5值
        /// </summary>
        /// <param name="stream">字节流</param>
        /// <returns>MD5Hash</returns>
        public static string GetMd5ByStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            MD5 md5Provider = MD5.Create();

            byte[] buffer = md5Provider.ComputeHash(stream);
            string result = BitConverter.ToString(buffer);

            result = result.Replace("-", "");
            md5Provider.Dispose();
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 转换为MD5加密后的字符串(默认加密为32位)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToMD5String(this string str)
        {
            MD5 md5 = MD5.Create();

            byte[] inputBytes = Encoding.UTF8.GetBytes(str);
            byte[] hashBytes  = md5.ComputeHash(inputBytes);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("x2"));
            }
            md5.Dispose();

            return(sb.ToString());
        }