ResoniteCacheCleaner/FrooxHelper.cs
2024-10-29 15:23:31 -04:00

123 lines
3.1 KiB
C#

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Wiry.Base32;
namespace ResoniteCacheCleaner {
internal static class FrooxHelper
{
private static ushort[] SEED_TABLE = new ushort[31]
{
25185, 25699, 26213, 26727, 27241, 27755, 28269, 28783, 29297, 29811,
30325, 30839, 31353, 16961, 17475, 17989, 18503, 19017, 19531, 20045,
20559, 21073, 21587, 22101, 22615, 23129, 12592, 13106, 13620, 14134,
14648
};
private static ushort[] INIT_TABLE = new ushort[10] { 0, 1, 39, 52, 48, 55, 50, 49, 30, 44 };
public unsafe static string ProcessConnection(string connection, string seed)
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < connection.Length; i++)
{
stringBuilder.Append(connection[i]);
}
byte* ptr = stackalloc byte[64];
MemoryExtensions.AsSpan(SEED_TABLE).CopyTo(new Span<ushort>(ptr, 32));
for (int j = 0; j < 128; j++)
{
for (int k = 0; k < seed.Length; k++)
{
for (int l = 0; l < seed.Length; l++)
{
if (l != k)
{
int num = (seed[l] * 43690 + j) % 62;
int num2 = seed[k] * 43690 % 62;
if (((j ^ k ^ l) & 1) == 0)
{
num2 = 61 - num2;
}
byte b = ptr[num];
ptr[num] = ptr[num2];
ptr[num2] = b;
}
}
}
}
for (int m = 0; m < seed.Length; m++)
{
ushort num3 = seed[m];
byte* ptr2 = ptr + (int)num3 % 62;
stringBuilder.Insert(stringBuilder.Length - (int)num3 % (m + 1), (char)(*ptr2));
}
for (int n = 0; n < INIT_TABLE.Length; n++)
{
int num4 = INIT_TABLE[n] + n + 59;
if (n == 0)
{
stringBuilder.Append((char)num4);
}
else
{
stringBuilder.Insert(stringBuilder.Length - (seed.Length + n), (char)num4);
}
}
return stringBuilder.ToString();
}
public static string GetMachineID(string keyPath)
{
using FileStream input = File.OpenRead(keyPath);
BinaryReader reader = new BinaryReader(input);
var exp = ReadArray();
var mod = ReadArray();
byte[] ReadArray()
{
int num = (int)reader.Read7BitEncoded();
if (num > 4096)
{
throw new InvalidDataException("Local key is corrupted");
}
byte[] array = new byte[num];
if (reader.Read(array, 0, num) != num)
{
throw new InvalidDataException("Local key is corrupted");
}
return array;
}
byte[] array = new byte[exp.Length + mod.Length];
Array.Copy(exp, array, exp.Length);
Array.Copy(mod, 0, array, exp.Length, mod.Length);
using SHA256Managed sHA256Managed = new SHA256Managed();
byte[] hash = sHA256Managed.ComputeHash(array);
return Base32Encoding.ZBase32.GetString(hash);
}
public static ulong Read7BitEncoded(this BinaryReader br)
{
ulong num = 0uL;
int num2 = 0;
bool flag;
do
{
byte b = br.ReadByte();
flag = (b & 0x80) != 0;
num |= (ulong)((long)(b & 0x7F) << num2);
num2 += 7;
}
while (flag);
return num;
}
}
}