csharp/499116344/qqProtocol/QQ.Framework/Utils/Util.cs

Util.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace QQ.Framework.Utils
{
    public static clast Util
    {
        private static readonly Encoding DefaultEncoding = Encoding.GetEncoding(QQGlobal.QQCharsetDefault);
        private static readonly DateTime BaseDateTime = DateTime.Parse("1970-1-01 08:00:00.000");

        public static Random Random = new Random();

        /// 
        ///     把字节数组从offset开始的len个字节转换成一个unsigned int,
        ///     2008-02-15 14:47 
        /// 
        /// 字节数组
        /// 从哪里开始转换.
        /// 转换长度, 如果len超过8则忽略后面的.
        /// 
        public static uint GetUInt(byte[] inData, int offset, int len)
        {
            uint ret = 0;
            int end;
            if (len > 8)
            {
                end = offset + 8;
            }
            else
            {
                end = offset + len;
            }

            for (var i = 0; i < end; i++)
            {
                ret  8);
            array[1] = (byte) (int) (paramLong >> 16);
            array[0] = (byte) (int) (paramLong >> 24);
            return array;
        }

        public static string ConvertStringToHex(string text, string separator = null)
        {
            var bytes = Encoding.UTF8.GetBytes(text);
            return ToHex(bytes);
        }

        public static string GetQQNum(string six)
        {
            return Convert.ToInt64(six.Replace(" ", ""), 16).ToString();
        }

        public static ulong GetQQNumRetUint(string six)
        {
            return Convert.ToUInt64(six.Replace(" ", ""), 16);
        }

        /// 
        ///     转换hex  长度不够前置补0
        /// 
        /// 
        /// 
        /// 
        public static string NumToHexString(long qq, int length = 8)
        {
            var text = Convert.ToString(qq, 16);
            if (text.Length == length)
            {
                return text;
            }

            if (text.Length > length)
            {
                return null;
            }

            var num = length - text.Length;
            var str = "";
            for (var i = 0; i < num; i++)
            {
                str += "0";
            }

            text = (str + text).ToUpper();
            var stringBuilder = new StringBuilder();
            for (var j = 0; j < text.Length; j++)
            {
                stringBuilder.Append(text[j]);
                if ((j + 1) % 2 == 0)
                {
                    stringBuilder.Append(" ");
                }
            }

            return stringBuilder.ToString();
        }

        public static string ConvertHexToString(string hexValue)
        {
            var bytes = HexStringToByteArray(hexValue);
            return Encoding.UTF8.GetString(bytes);
        }

        /// 
        ///     根据某种编码方式将字节数组转换成字符串
        /// 
        /// 字节数组
        /// encoding 编码方式
        ///  如果encoding不支持,返回一个缺省编码的字符串
        public static string GetString(byte[] b, string encoding = QQGlobal.QQCharsetDefault)
        {
            try
            {
                return Encoding.GetEncoding(encoding).GetString(b);
            }
            catch
            {
                return Encoding.Default.GetString(b);
            }
        }

        /// 
        ///     根据某种编码方式将字节数组转换成字符串
        ///     2008-02-22 
        /// 
        /// The b.
        /// The offset.
        /// The len.
        /// The encoding.
        /// 
        public static string GetString(byte[] b, int offset, int len, string encoding = QQGlobal.QQCharsetDefault)
        {
            var temp = new byte[len];
            Array.Copy(b, offset, temp, 0, len);
            return GetString(temp, encoding);
        }

        /// 
        ///     把字符串转换成int
        /// 
        /// 字符串
        /// 如果转换失败,返回这个值
        /// 
        public static int GetInt(string s, int defaultValue)
        {
            return int.TryParse(s, out var value) ? value : defaultValue;
        }

        /// 
        ///     字符串转二进制字数组
        /// 
        /// The s.
        /// 
        public static byte[] GetBytes(string s)
        {
            return DefaultEncoding.GetBytes(s);
        }

        /// 
        ///     一个随机产生的密钥字节数组
        /// 
        /// 
        public static byte[] RandomKey()
        {
            var key = new byte[QQGlobal.QQLengthKey];
            new Random().NextBytes(key);
            return key;
        }

        /// 
        ///     一个随机产生的密钥字节数组
        /// 
        /// 
        public static byte[] RandomKey(int length)
        {
            var key = new byte[length];
            new Random().NextBytes(key);
            return key;
        }


        /// 
        ///     用于代替 System.currentTimeMillis()
        /// 
        /// The date time.
        /// 
        public static long GetTimeMillis(DateTime dateTime)
        {
            return (long) (dateTime - BaseDateTime).TotalMilliseconds;
        }

        public static long GetTimeSeconds(DateTime dateTime)
        {
            return (long) (dateTime - BaseDateTime).TotalSeconds;
        }

        /// 
        ///     根据服务器返回的毫秒表示的日期,获得实际的日期
        /// 
        /// The millis.
        /// 
        public static DateTime GetDateTimeFromMillis(long millis)
        {
            return BaseDateTime.AddTicks(millis * TimeSpan.TicksPerSecond);
        }

        /// 
        ///     判断IP是否全0
        /// 
        /// The ip.
        /// 
        public static bool IsIPZero(byte[] ip)
        {
            return ip.All(t => t == 0);
        }

        /// 
        ///     ip的字节数组形式转为字符串形式的ip
        /// 
        /// The ip.
        /// 
        public static string GetIpStringFromBytes(byte[] ip)
        {
            return $"{ip[0]}.{ip[1]}.{ip[2]}.{ip[3]}";
        }

        public static string ToHex(byte[] bs, string newLine = "", string format = "{0} ")
        {
            var num = 0;
            var stringBuilder = new StringBuilder();
            foreach (var b in bs)
            {
                if (num++ % 16 == 0)
                {
                    stringBuilder.Append(newLine);
                }

                stringBuilder.AppendFormat(format, b.ToString("X2"));
            }

            return stringBuilder.ToString().Trim();
        }

        public static byte[] IPStringToByteArray(string ip)
        {
            var array = new byte[4];
            var array2 = ip.Split('.');
            if (array2.Length == 4)
            {
                for (var i = 0; i < 4; i++)
                {
                    array[i] = (byte) int.Parse(array2[i]);
                }
            }

            return array;
        }

        /// 
        ///     获取本地外网 IP
        /// 
        /// 
        public static string GetExternalIp()
        {
            var mc = Regex.Match(
                new HttpClient().GetStringAsync("http://www.net.cn/static/customercare/yourip.asp").Result,
                @"您的本地上网IP是:(\d+\.\d+\.\d+\.\d+)");
            if (mc.Success && mc.Groups.Count > 1)
            {
                return mc.Groups[1].Value;
            }

            throw new Exception("获取IP失败");
        }

        /// 
        ///     根据域名获取IP
        /// 
        /// 域名
        /// 
        public static string GetHostAddresses(string hostname)
        {
            var ips = Dns.GetHostAddresses(hostname);

            return ips[0].ToString();
        }

        public static string GetBkn(string skey)
        {
            var num = 5381;
            for (var i = 0; i  24);
            tempByteArray[index + 1] = (byte) ((num & 0x00ff0000) >> 16);
            tempByteArray[index + 2] = (byte) ((num & 0x0000ff00) >> 8);
            tempByteArray[index + 3] = (byte) (num & 0x000000ff);
        }

        public static int buf_to_int16(byte[] tempByteArray, long index)
        {
            return (tempByteArray[index]