optimized gameuuid handling

This commit is contained in:
Matthew Toro 2026-04-04 02:17:59 -04:00
parent 6169b133ec
commit 692a1ffc8e
13 changed files with 61 additions and 45 deletions

View file

@ -2,32 +2,37 @@
#include "UUID.h"
#include "Random.h"
#include <cstring>
static void sha1_block(uint32_t h[5], const uint8_t block[64])
{
uint32_t w[80];
for (int i = 0; i < 16; i++)
w[i] = (block[i * 4] << 24) | (block[i * 4 + 1] << 16) | (block[i * 4 + 2] << 8) | block[i * 4 + 3];
for (int i = 16; i < 80; i++) {
uint32_t x = w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16];
w[i] = (x << 1) | (x >> 31);
}
uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
for (int i = 0; i < 80; i++) {
uint32_t f, k;
if (i < 20) { f = (b & c) | (~b & d); k = 0x5A827999; }
else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; }
else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; }
else { f = b ^ c ^ d; k = 0xCA62C1D6; }
uint32_t tmp = ((a << 5) | (a >> 27)) + f + e + k + w[i];
e = d; d = c; c = (b << 30) | (b >> 2); b = a; a = tmp;
}
h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e;
}
static void sha1(const uint8_t* data, size_t len, uint8_t out[20])
{
uint32_t h[5] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
uint64_t bitLen = len * 8;
size_t fullBlocks = len / 64;
for (size_t blk = 0; blk < fullBlocks; blk++) {
const uint8_t* p = data + blk * 64;
uint32_t w[80];
for (int i = 0; i < 16; i++)
w[i] = (p[i * 4] << 24) | (p[i * 4 + 1] << 16) | (p[i * 4 + 2] << 8) | p[i * 4 + 3];
for (int i = 16; i < 80; i++) {
uint32_t x = w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16];
w[i] = (x << 1) | (x >> 31);
}
uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
for (int i = 0; i < 80; i++) {
uint32_t f, k;
if (i < 20) { f = (b & c) | (~b & d); k = 0x5A827999; }
else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; }
else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; }
else { f = b ^ c ^ d; k = 0xCA62C1D6; }
uint32_t tmp = ((a << 5) | (a >> 27)) + f + e + k + w[i];
e = d; d = c; c = (b << 30) | (b >> 2); b = a; a = tmp;
}
h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e;
}
for (size_t blk = 0; blk < fullBlocks; blk++)
sha1_block(h, data + blk * 64);
uint8_t tail[128] = {};
size_t rem = len - fullBlocks * 64;
if (rem) memcpy(tail, data + fullBlocks * 64, rem);
@ -36,27 +41,8 @@ static void sha1(const uint8_t* data, size_t len, uint8_t out[20])
for (int i = 0; i < 8; i++)
tail[tailLen - 1 - i] = (uint8_t)(bitLen >> (i * 8));
for (size_t off = 0; off < tailLen; off += 64) {
uint32_t w[80];
for (int i = 0; i < 16; i++)
w[i] = (tail[off + i * 4] << 24) | (tail[off + i * 4 + 1] << 16) |
(tail[off + i * 4 + 2] << 8) | tail[off + i * 4 + 3];
for (int i = 16; i < 80; i++) {
uint32_t x = w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16];
w[i] = (x << 1) | (x >> 31);
}
uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
for (int i = 0; i < 80; i++) {
uint32_t f, k;
if (i < 20) { f = (b & c) | (~b & d); k = 0x5A827999; }
else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; }
else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; }
else { f = b ^ c ^ d; k = 0xCA62C1D6; }
uint32_t tmp = ((a << 5) | (a >> 27)) + f + e + k + w[i];
e = d; d = c; c = (b << 30) | (b >> 2); b = a; a = tmp;
}
h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e;
}
for (size_t off = 0; off < tailLen; off += 64)
sha1_block(h, tail + off);
for (int i = 0; i < 5; i++) {
out[i * 4] = (uint8_t)(h[i] >> 24);