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

namespace DeviceRepair.Utils.Security
{
    public static class EncryptionHelper
    {
        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string EncryptByMD5(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
                return "";

            input = input.Trim();
            // 创建一个 MD5 实例对象
            MD5 md5 = MD5.Create();

            // 将字符串转换为字节数组
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);

            // 使用 MD5 算法计算哈希值
            byte[] hashBytes = md5.ComputeHash(inputBytes);

            // 将结果转换为字符串
            StringBuilder sb = new StringBuilder();
            foreach (byte b in hashBytes)
            {
                sb.Append(b.ToString("x2"));
            }

            string result = sb.ToString().ToUpper();
            return result;
        }

        /// <summary>
        /// 字符串 转 Url Code
        /// </summary>
        /// <returns></returns>
        public static string UrlEncry(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
                return "";
            return HttpUtility.UrlEncode(input);
        }

        /// <summary>
        /// Url Code 转 字符串
        /// </summary>
        /// <returns></returns>
        public static string UrlDecode(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
                return "";
            return HttpUtility.UrlDecode(input);
        }
    }
}
