From fd39c75a64e7ef703264a5e09cac2909e73882c8 Mon Sep 17 00:00:00 2001 From: CroakFang <1605296300@qq.com> Date: Tue, 13 Dec 2022 23:09:47 +0800 Subject: [PATCH] add game manifest hash algorithm --- Assets/Scripts/ManifestEntry.cs | 44 +++++++++++++++++---------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/Assets/Scripts/ManifestEntry.cs b/Assets/Scripts/ManifestEntry.cs index 0bef48d..ca498f1 100644 --- a/Assets/Scripts/ManifestEntry.cs +++ b/Assets/Scripts/ManifestEntry.cs @@ -3,31 +3,33 @@ using System; using System.Security.Cryptography; using System.Text; using UnityEngine; - -namespace Assets.Editor.RootMotion +public class ManifestEntry { - public class ManifestEntry + public static byte[] CalHame(ulong checksum, ulong size, byte[] name) { - public static byte[] CalHame(ulong checksum, ulong size, byte[] name) + if (name == null) return null; + SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); + byte[] allbyte = new byte[name.Length + 16]; + byte[] checksumByte = BitConverter.GetBytes(checksum); + byte[] sizeByte = BitConverter.GetBytes(size); + for (int i = 0; i < 16; i++) { - if (name == null) return null; - SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); - byte[] allbyte = new byte[name.Length + 16]; - byte[] checksumByte = BitConverter.GetBytes(checksum); - byte[] sizeByte = BitConverter.GetBytes(size); - for(int i=0; i < 16; i++) - { - allbyte[i] = (i < 8 ? checksumByte[7 - i] : sizeByte[15 - i]); - } - Buffer.BlockCopy(name, 0, allbyte, 16, name.Length); - return sha1.ComputeHash(allbyte); + allbyte[i] = (i < 8 ? checksumByte[7 - i] : sizeByte[15 - i]); } + Buffer.BlockCopy(name, 0, allbyte, 16, name.Length); + return sha1.ComputeHash(allbyte); + } - public static string CalHameString(ulong checksum, ulong size, byte[] name) - { - var bytes = CalHame(checksum, size, name); - Base32 base32 = new Base32(); - return base32.Encode(bytes); - } + public static string CalHameString(ulong checksum, ulong size, byte[] name) + { + var bytes = CalHame(checksum, size, name); + Base32 base32 = new Base32(); + return base32.Encode(bytes); + } + + public static string CalHameString(ulong checksum, ulong size, string name) + { + return CalHameString(checksum, size, Encoding.UTF8.GetBytes(name)); } } +