using System;
using System.Security.Cryptography;
using System.Text;

namespace GxPress.Common.Encrypt
{
    /// <summary>
    /// 加密Provider
    /// </summary>
    public static class EncryptProvider
    {
        /// <summary>
        /// MD5
        /// </summary>
        /// <param name="srcStr"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Md5(string srcStr, Md5Length length = Md5Length.L32)
        {
            if (string.IsNullOrEmpty(srcStr))
                throw new ArgumentException();

            using MD5 md5 = MD5.Create();
            byte[] bytesMd5In = Encoding.UTF8.GetBytes(srcStr);
            byte[] bytesMd5Out = md5.ComputeHash(bytesMd5In);

            var strMd5Out = length == Md5Length.L32
                ? BitConverter.ToString(bytesMd5Out)
                : BitConverter.ToString(bytesMd5Out, 4, 8);

            strMd5Out = strMd5Out.Replace("-", "");
            return strMd5Out;
        }
    }
}