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

namespace WS
{
    ///<summary>异或 Sha1</summary>
    public class OtherEncrypt
    {
        /// <summary>异或因子</summary>
        private static readonly byte[] xorScale = new byte[] { 45, 66, 38, 55, 23, 254, 9, 165, 90, 19, 41, 45, 201, 58, 55, 37, 254, 185, 165, 169, 19, 171 };

        /// <summary>随机字符串</summary>
        private static readonly string[] RandomScale = new string[]
        {
            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "*","+","-","/",".",">","<","&","%","#","@","$","!","^",
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
        };

        ///<summary>异或</summary>
        public static byte[] Xor(byte[] buffer)
        {
            int iScaleLen = xorScale.Length;
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)(buffer[i] ^ xorScale[i % iScaleLen]);
            }
            return buffer;
        }

        ///<summary>Sha1 加密</summary>
        public static string Sha1(string str)
        {
            byte[] buffer = Encoding.Default.GetBytes(str);
            byte[] data = SHA1.Create().ComputeHash(buffer);

            StringBuilder sub = new StringBuilder();
            foreach (var t in data)
            {
                sub.Append(t.ToString("X2"));
            }
            return sub.ToString();
        }

        ///<summary>获取随机字符串</summary>
        public static string GetRandomString(int count)
        {
            int maxcount = RandomScale.Length;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                int index = Random.Range(0, maxcount);
                sb.Append(RandomScale[index]);
            }
            return sb.ToString();
        }
    }
}