use MD5 hash

This commit is contained in:
Mister_Nebula 2022-10-09 13:22:53 +01:00
parent 69a56b9783
commit 7bfbd8b6f1
3 changed files with 16 additions and 10 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
using Object = UnityEngine.Object;
@ -214,17 +215,22 @@ public static class Extensions
}
}
// Adapted from https://stackoverflow.com/a/30758270
public static int GetSequenceHash(this IEnumerable<string> list)
// https://stackoverflow.com/a/24031467
public static string GetMD5Hash(this IEnumerable<string> list)
{
const int seed = 487;
const int modifier = 31;
using var md5 = System.Security.Cryptography.MD5.Create();
unchecked
var longString = string.Concat(list);
var bytes = Encoding.ASCII.GetBytes(longString);
var hashBytes = md5.ComputeHash(bytes);
var sb = new StringBuilder();
for (var i = 0; i < hashBytes.Length; i++)
{
return list.Aggregate(seed, (current, item) =>
(current * modifier) + item.GetStableHashCode());
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
#endregion

View File

@ -20,7 +20,7 @@ namespace QSB.WorldSync;
public static class QSBWorldSync
{
public static WorldObjectManager[] Managers;
public static int WorldObjectsHash { get; private set; }
public static string WorldObjectsHash { get; private set; }
/// <summary>
/// Set when all WorldObjectManagers have called Init() on all their objects (AKA all the objects are created)
@ -83,7 +83,7 @@ public static class QSBWorldSync
AllObjectsAdded = true;
DebugLog.DebugWrite("World Objects added.", MessageType.Success);
WorldObjectsHash = WorldObjects.Select(x => x.GetType().Name).GetSequenceHash();
WorldObjectsHash = WorldObjects.Select(x => x.GetType().Name).GetMD5Hash();
DebugLog.DebugWrite($"WorldObject hash is {WorldObjectsHash}");
if (!QSBCore.IsHost)

View File

@ -8,7 +8,7 @@ namespace QSB.WorldSync;
/// <summary>
/// sends QSBWorldSync.WorldObjectsHash to the server for sanity checking
/// </summary>
internal class WorldObjectsHashMessage : QSBMessage<int>
internal class WorldObjectsHashMessage : QSBMessage<string>
{
public WorldObjectsHashMessage() : base(QSBWorldSync.WorldObjectsHash) => To = 0;