TokenGen CreateTokenHash from array of strings.

This commit is contained in:
Glax 2025-01-12 19:09:37 +01:00
parent 524596ad85
commit 6a8df2f5f8

View File

@ -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;
}
}