using System;
using System.IO;

namespace LJLib.Tools.Encry
{
    public class MD5
    {
        public byte[] ComputeHash(byte[] bytes)
        {
            using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
            {
                return md5.ComputeHash(bytes);
            }
        }

        public byte[] ComputeHash(byte[] bytes, int offset, int count)
        {
            using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
            {
                return md5.ComputeHash(bytes, offset, count);
            }
        }

        public byte[] ComputeHash(System.IO.Stream inputStream)
        {
            using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
            {
                return md5.ComputeHash(inputStream);
            }
        }

        public string GetMD5(byte[] bytes)
        {
                byte[] rslt = this.ComputeHash(bytes);
                string str_rslt = BitConverter.ToString(rslt);
                str_rslt = str_rslt.Replace("-", "");
                return str_rslt;
        }

        public string GetMD5(byte[] bytes, int offset, int count)
        {
                byte[] rslt = this.ComputeHash(bytes, offset, count);
                string str_rslt = BitConverter.ToString(rslt);
                str_rslt = str_rslt.Replace("-", "");
                return str_rslt;
        }

        public string GetMD5(System.IO.Stream inputStream)
        {
            byte[] rslt = this.ComputeHash(inputStream);
            string str_rslt = BitConverter.ToString(rslt);
            str_rslt = str_rslt.Replace("-", "");
            return str_rslt;
        }

        public string GetMD5(string filename)
        {
            using (var fs = System.IO.File.OpenRead(filename))
            {
                return GetMD5(fs);
            }
        }
    }
}
