Esempio n. 1
0
        /// <summary>
        /// Gets a SHA256 hash of parts of the NetworkingConfiguration instance
        /// </summary>
        /// <param name="cache"></param>
        /// <returns></returns>
        public byte[] GetConfig(bool cache = true)
        {
            if (ConfigHash != null && cache)
            {
                return(ConfigHash);
            }

            using (BitWriter writer = new BitWriter())
            {
                writer.WriteUShort(ProtocolVersion);
                for (int i = 0; i < Channels.Count; i++)
                {
                    writer.WriteString(Channels[i].Name);
                    writer.WriteByte((byte)Channels[i].Type);
                    if (EnableEncryption)
                    {
                        writer.WriteBool(Channels[i].Encrypted);
                    }
                }
                for (int i = 0; i < MessageTypes.Count; i++)
                {
                    writer.WriteString(MessageTypes[i].Name);
                    if (AllowPassthroughMessages)
                    {
                        writer.WriteBool(MessageTypes[i].Passthrough);
                    }
                }
                if (EnableSceneSwitching)
                {
                    for (int i = 0; i < RegisteredScenes.Count; i++)
                    {
                        writer.WriteString(RegisteredScenes[i]);
                    }
                }
                if (HandleObjectSpawning)
                {
                    for (int i = 0; i < NetworkedPrefabs.Count; i++)
                    {
                        writer.WriteString(NetworkedPrefabs[i].name);
                    }
                }
                writer.WriteBool(HandleObjectSpawning);
                writer.WriteBool(EnableEncryption);
                writer.WriteBool(AllowPassthroughMessages);
                writer.WriteBool(EnableSceneSwitching);
                writer.WriteBool(SignKeyExchange);

                //using (SHA256Managed sha256 = new SHA256Managed())
                //{
                // Returns a 160 bit / 20 byte / 5 int checksum of the config
                if (cache)
                {
                    ConfigHash = MessageDigest.SHA1_Opt(writer.Finalize()).ToArray();     //sha256.ComputeHash(writer.Finalize());
                    return(ConfigHash);
                }
                return(MessageDigest.SHA1_Opt(writer.Finalize()).ToArray());    //sha256.ComputeHash(writer.Finalize());
                //}
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets a ushort hash from a string
        /// </summary>
        /// <param name="input">Input data</param>
        /// <param name="cache">Wheter or not cache the hash</param>
        /// <returns>The hash result</returns>
        public static ushort GetUShortHash(string input, bool cache = false)
        {
            if (cache && ushortCachedHashes.ContainsKey(input))
            {
                return(ushortCachedHashes[input]);
            }

            ushort value = MessageDigest.SHA1_Opt(Encoding.UTF8.GetBytes(input)).CastUShort();

            if (cache)
            {
                ushortCachedHashes.Add(input, value);
            }
            return(value);
        }
Esempio n. 3
0
        /// <summary>
        /// Gets a ulong hash from a string
        /// </summary>
        /// <param name="input">Input data</param>
        /// <param name="cache">Wheter or not cache the hash</param>
        /// <returns>The hash result</returns>
        public static ulong GetULongHash(string input, bool cache = false)
        {
            if (cache && ulongCachedHashes.ContainsKey(input))
            {
                return(ulongCachedHashes[input]);
            }

            ulong value = MessageDigest.SHA1_Opt(Encoding.UTF8.GetBytes(input)).CastULong();

            if (cache)
            {
                ulongCachedHashes.Add(input, value);
            }
            return(value);
        }
Esempio n. 4
0
        public static ushort GetUShortHash(string input, bool cache = false)
        {
            if (cache && ushortCachedHashes.ContainsKey(input))
            {
                return(ushortCachedHashes[input]);
            }

            //using (SHA256Managed sha = new SHA256Managed())
            //{
            //byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
            ushort value = MessageDigest.SHA1_Opt(Encoding.UTF8.GetBytes(input)).CastUShort();     //(ushort)(hash[0] | (ushort)(hash[1] << 8));

            if (cache)
            {
                ushortCachedHashes.Add(input, value);
            }
            return(value);
            //}
        }
Esempio n. 5
0
        public static ulong GetUIntHash(string input, bool cache = false)
        {
            if (cache && uintCachedHashes.ContainsKey(input))
            {
                return(uintCachedHashes[input]);
            }

            //using (SHA256Managed sha = new SHA256Managed())
            //{
            //byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
            uint value = MessageDigest.SHA1_Opt(Encoding.UTF8.GetBytes(input)).CastUInt();     //hash[0] | ((uint)hash[1] << 8) | ((uint)hash[2] << 16) | ((uint)hash[3] << 24);

            if (cache)
            {
                uintCachedHashes.Add(input, value);
            }
            return(value);
            //}
        }