Skip to content

Commit

Permalink
small improvements to crc32 things
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Dec 29, 2024
1 parent a9b8174 commit 5e1f2be
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 120 deletions.
69 changes: 37 additions & 32 deletions crc32c.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>crc32c tester</title>
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css"/>
</head>
<body>
<script src="https://cdn.sheetjs.com/crc-32-latest/package/crc32c.js"></script>
<script>
const keys = [
0xa8770416, 0xa98beb34, 0xb29b1d90, 0xd5354502, 0xce9df055, 0xe3230e2f,
0xe1dd1d34, 0xbd530797, 0xa2c7c909, 0xa047885e, 0x9b274d93, 0x7d86e792,
0x078ac8bd, 0x6c836947, 0x66ebfad1, 0x66df165f, 0x65e740d3, 0x65d75204,
0x4bb0092c, 0x442a34ac, 0x2ec0e736, 0x2c3ef42d, 0x27424d58, 0x1a081a93,
0x15e90814,
];
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css" />
</head>
<body>
<script src="https://cdn.sheetjs.com/crc-32-latest/package/crc32c.js"></script>
<script>
const keys = [
0x9b274d93, 0x051d7f6e, 0xc0f04c4d, 0x0f04f20d, 0x18877141, 0x46873e19,
0x37e9b030, 0x309078ee, 0xa98beb34, 0x65d75204, 0x65e740d3, 0x4bb0092c,
0x1a081a93, 0x26ef8f28, 0x66ebfad1, 0x7d86e792, 0x8653e035, 0xef76d125,
0xa098fd7c, 0xe3230e2f, 0xb29b1d90, 0xe63eed4a, 0xa047885e, 0xbbd28c5f,
0x6f014de4, 0x59e536e8, 0x442a34ac, 0x15e90814, 0xd5354502, 0x634ded4a,
0x7be12a49, 0x2b38cdb9, 0x2ec0e736, 0x60499f1e, 0x9fea7962, 0x44befe78,
0x078ac8bd, 0x86565c7b, 0x0218ee4e, 0xa2a13d1d, 0xca368ff2, 0x66df165f,
0x67448f99, 0x7f9c90c3, 0x6c836947, 0x56ade412, 0xd79f5991, 0x2c3ef42d,
0xe1dd1d34, 0x27424d58, 0x7d12578e, 0x1eb5ca39, 0x833b04eb, 0x0f003884,
0xf3a7acac, 0xa703f1d6, 0xbd530797, 0xa8770416, 0xce9df055,
];

function handleInput() {
function handleInput() {
const input = document.getElementById("input");
const output = document.getElementById("output");
const outputLowercase = document.getElementById("outputLowercase");
if (input.value.length === 0) {
output.innerText = "0x00000000";
outputLowercase.innerText = "0x00000000";
return;
output.innerText = "0x00000000";
outputLowercase.innerText = "0x00000000";
return;
}

const result = CRC32C.str(input.value) >>> 0;
Expand All @@ -34,20 +39,20 @@
outputLowercase.innerText = "0x" + resultLowercase.toString(16);

if (keys.includes(result)) {
log.innerText += `\n${result} - ${input.value}`;
log.innerText += `\n${result} - ${input.value}`;
}
if (keys.includes(resultLowercase)) {
log.innerText += `\n${resultLowercase} - ${input.value.toLowerCase()}`;
log.innerText += `\n${resultLowercase} - ${input.value.toLowerCase()}`;
}
}
</script>
<p></p>
<label for="input">Input</label>
<input type="text" id="input" oninput="handleInput()"/>
<label for="output">Output</label>
<pre id="output">0x00000000</pre>
<label for="outputLowercase">Output (lowercase)</label>
<pre id="outputLowercase">0x00000000</pre>
<pre id="log">Found:</pre>
</body>
}
</script>
<p></p>
<label for="input">Input</label>
<input type="text" id="input" oninput="handleInput()" />
<label for="output">Output</label>
<pre id="output">0x00000000</pre>
<label for="outputLowercase">Output (lowercase)</label>
<pre id="outputLowercase">0x00000000</pre>
<pre id="log">Found:</pre>
</body>
</html>
140 changes: 140 additions & 0 deletions src/StarBreaker.Sandbox/BruteForce.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System.Globalization;
using System.Numerics;

namespace StarBreaker.Sandbox;

internal static class BruteForce
{
public static byte[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-/\\"u8.ToArray();
public const int MAX_LENGTH = 8;
public const int CORES = 16;

public static uint[] _test = null!;

public static void Run()
{
_test = ReadKeys("keys.txt");

var tasks = Enumerable.Range(0, CORES).Select(i => Task.Run(() => Brute(i)));

Task.WaitAll(tasks);
}

public static void Brute(int i)
{
// Each core handles different starting characters for better distribution
int charsPerCore = chars.Length / CORES;
int startIndex = i * charsPerCore;
int endIndex = (i == CORES - 1) ? chars.Length : (i + 1) * charsPerCore;

// Try all lengths up to MAX_LENGTH
for (int len = 1; len <= MAX_LENGTH; len++)
{
BruteLength(len, startIndex, endIndex);
}
}

private static void BruteLength(int length, int startIndex, int endIndex)
{
Span<byte> current = stackalloc byte[length];

// Initialize the hasher with first character
for (int firstChar = startIndex; firstChar < endIndex; firstChar++)
{
current[0] = chars[firstChar];
var hasher = new PartHasher(current[..1]);

if (length == 1)
{
uint hash = hasher.Value;
if (Array.BinarySearch(_test, hash) >= 0)
{
Console.WriteLine($"Found: {System.Text.Encoding.ASCII.GetString(current)}");
}
continue;
}

BruteLengthRecursive(current, 1, hasher);
}
}

private static void BruteLengthRecursive(Span<byte> current, int position, PartHasher hasher)
{
// Try all possible characters at current position
foreach (byte c in chars)
{
current[position] = c;

// Calculate hash with new character
uint tempHash = hasher.WithChar(c);

// If we're at the last position, check if hash exists
if (position == current.Length - 1)
{
if (Array.BinarySearch(_test, tempHash) >= 0)
{
Console.WriteLine($"Found: {System.Text.Encoding.ASCII.GetString(current)}");
}
continue;
}

// For intermediate positions, create new hasher with added character and recurse
var newHasher = hasher;
newHasher.Add(c);
BruteLengthRecursive(current, position + 1, newHasher);
}
}

static uint[] ReadKeys(string file)
{
var lines = File.ReadAllLines(file);
var keys = new List<uint>();

foreach (var line in lines)
{
if (line.StartsWith("0x") &&
uint.TryParse(line[2..], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var key))
{
keys.Add(key);
}
}

return keys.Distinct().Order().ToArray();
}
}

public struct PartHasher
{
private uint Current;

public PartHasher()
{
Current = 0xFFFFFFFFu;
}

public PartHasher(ReadOnlySpan<byte> chars)
{
Current = 0xFFFFFFFFu;

foreach (var ch in chars)
Current = BitOperations.Crc32C(Current, ch);
}

public readonly uint WithChar(byte c) => ~BitOperations.Crc32C(Current, c);

public readonly uint WithChar(ReadOnlySpan<byte> chars) => WithChar(chars[0]);

public void Add(byte c)
{
Current = BitOperations.Crc32C(Current, c);
}

public void Add(ReadOnlySpan<byte> chars)
{
Add(chars[0]);
}

public readonly uint Value => ~Current;
}


7 changes: 4 additions & 3 deletions src/StarBreaker.Sandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

//Project just so I can run throwaway code without adding a project for each thing.


await ChfProcessing.Run();
//BruteForce.Run();
//await ChfProcessing.Run();
//ExtractChunkFiles.Run();
//ExtractSocPak.Run();
//await GrpcClient.RunAsync();
//TimeP4kExtract.Run();
//TagDatabase.Run();
//StringCrc32c.Run();
//DdsUnsplit.Run();
// DdsUnsplit.Run();
ZipFile.Run();
Loading

0 comments on commit 5e1f2be

Please sign in to comment.