From 6a8df2f5f811c6c07e07a496184b382a5e4a0b71 Mon Sep 17 00:00:00 2001 From: Glax Date: Sun, 12 Jan 2025 19:09:37 +0100 Subject: [PATCH] TokenGen CreateTokenHash from array of strings. --- API/TokenGen.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/API/TokenGen.cs b/API/TokenGen.cs index 2464ae0..d7ca0da 100644 --- a/API/TokenGen.cs +++ b/API/TokenGen.cs @@ -1,4 +1,5 @@ using System.Security.Cryptography; +using System.Text; namespace API; @@ -20,4 +21,20 @@ public static class TokenGen key = string.Join('-', prefix, key); return key; } + + public static string CreateTokenHash(string prefix, uint fullLength, string[] keys) + { + if (prefix.Length + 1 >= fullLength - MinimumLength) + throw new ArgumentException("Prefix to long to create Token of meaningful length."); + int l = (int)(fullLength - prefix.Length - 1); + MD5 md5 = MD5.Create(); + byte[][] hashes = keys.Select(key => md5.ComputeHash(Encoding.UTF8.GetBytes(key))).ToArray(); + byte[] xOrHash = new byte[l]; + foreach (byte[] hash in hashes) + for(int i = 0; i < hash.Length; i++) + xOrHash[i] = (byte)(xOrHash[i] ^ (i >= hash.Length ? 0 : hash[i])); + string key = new (xOrHash.Select(b => Chars[b % Chars.Length]).ToArray()); + key = string.Join('-', prefix, key); + return key; + } } \ No newline at end of file