using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushPlatform.Common.Util
{
    public static class EncodeUtil
    {
        /// <summary>
        /// 把字符串编码为base64
        /// </summary>
        /// <param name="str">需要编码的字符串</param>
        /// <returns></returns>
        public static string ToBase64(this string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
            }
            else
            {
                return string.Empty;
            }

        }

        /// <summary>
        /// 从base64字符串解码
        /// </summary>
        /// <param name="str">需要解码的字符串</param>
        /// <returns>解码后的字符串</returns>
        public static string DecodeBase64(this string str)
        {
            try
            {
                if (!string.IsNullOrEmpty(str))
                {
                    return Encoding.UTF8.GetString(Convert.FromBase64String(str));
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
