using System;
using System.IO;

namespace YGNT.Infrastructure.Helper
{
    /// <summary>
    /// 加密类
    /// </summary>
    public class EncryptHelp
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="txt">待加密字符串</param>
        /// <returns></returns>
        public static string EncryptMD5(string txt)
        {
            byte[] byteArray = System.Text.Encoding.Default.GetBytes(txt);
            byte[] buffer = System.Security.Cryptography.MD5.Create().ComputeHash(byteArray);
            return BitConverter.ToString(buffer).Replace("-", string.Empty);

            //using (MD5 mi = System.Security.Cryptography.MD5.Create())
            //{
            //    byte[] buffer = Encoding.Default.GetBytes(txt);
            //    //开始加密
            //    byte[] newBuffer = mi.ComputeHash(buffer);
            //    StringBuilder sb = new StringBuilder();
            //    for (int i = 0; i < newBuffer.Length; i++)
            //    {
            //        sb.Append(newBuffer[i].ToString("x2"));
            //    }
            //    return sb.ToString();
            //}
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="stream">待加密流</param>
        /// <returns></returns>
        public static string EncryptMD5(Stream stream)
        {
            byte[] buffer = System.Security.Cryptography.MD5.Create().ComputeHash(stream);
            return BitConverter.ToString(buffer).Replace("-", string.Empty);
        }

        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="txt">待加密字符串</param>
        /// <returns></returns>
        public static string EncryptSHA1(string txt)
        {
            byte[] byteArray = System.Text.Encoding.Default.GetBytes(txt);
            byte[] buffer = System.Security.Cryptography.SHA1.Create().ComputeHash(byteArray);
            return BitConverter.ToString(buffer).Replace("-", string.Empty);
        }

    }
}