diff --git a/pkNX.Containers/Misc/FnvHashDecoder.cs b/pkNX.Containers/Misc/FnvHashDecoder.cs index 5f1e52a5..d442ab67 100644 --- a/pkNX.Containers/Misc/FnvHashDecoder.cs +++ b/pkNX.Containers/Misc/FnvHashDecoder.cs @@ -1,3 +1,4 @@ +using pkNXHashDecoder; using System; using System.Collections; using System.Collections.Generic; @@ -20,28 +21,31 @@ public class FnvHashDecoder private const ulong kFnvPrime_64 = 0x00000100000001b3; private const ulong kOffsetBasis_64 = 0xCBF29CE484222645; - public Dictionary FoundKeys = new(); + public Dictionary> FoundKeys = new(); - public Dictionary Run(int minLength, int maxLength) + public Dictionary> Run(int minLength, int maxLength) { Debug.Assert(FnvHash.HashFnv1a_64("stickyball") == 0x15cb1f582d2b05ab); + Debug.Assert(FnvHash.HashFnv1a_64("enigma") == 0xC75DDBE25402B11C); int length = maxLength; var tasks = new List(); - for (int i = 0; i < AllowedChars.Length; ++i) + for (int i = 0; i < FnvHashDecoderLib.AllowedCharCount; ++i) { int startChar = i; tasks.Add(Task.Run(() => { - var dict = pkNXHashDecoder.FnvHashDecoderLib.StartDecoding(length, startChar); + var dict = FnvHashDecoderLib.StartDecoding(length, startChar); if (dict.Count != 0) { foreach (var x in dict) { - FoundKeys.Add(x.Key, x.Value); + FoundKeys.TryAdd(x.Key, new()); + FoundKeys[x.Key].Add(x.Value); + Debug.WriteLine($"{x.Key} {x.Value}"); } } //StartLoop(length, startChar); @@ -58,7 +62,7 @@ public class FnvHashDecoder return FoundKeys; } - private void StartLoop(int length, int startChar) + /*private void StartLoop(int length, int startChar) { Span chars = stackalloc char[length]; Span allowedCharIndex = stackalloc byte[length]; // Current AllowedChar @@ -122,5 +126,5 @@ private void StartLoop(int length, int startChar) if (FoundKeys.Count != 0) return; } - } + }*/ } diff --git a/pkNX.HashDecoder/HashDecoderLib.h b/pkNX.HashDecoder/HashDecoderLib.h index 4c10074b..d298cca0 100644 --- a/pkNX.HashDecoder/HashDecoderLib.h +++ b/pkNX.HashDecoder/HashDecoderLib.h @@ -12,6 +12,14 @@ namespace pkNXHashDecoder { return nativeHandle_->StartDecoding(length, startChar); } + property int AllowedCharCount + { + int get() + { + return FnvHashDecoder::AllowedCharsCount; + } + } + private: FnvHashDecoder* nativeHandle_ = new FnvHashDecoder(); }; @@ -31,5 +39,13 @@ namespace pkNXHashDecoder { return dict; } + + static property int AllowedCharCount + { + int get() + { + return FnvHashDecoder::AllowedCharsCount; + } + } }; } diff --git a/pkNX.HashDecoderConsole/HashDecoder.cpp b/pkNX.HashDecoderConsole/HashDecoder.cpp index 40f8d61a..f437f67a 100644 --- a/pkNX.HashDecoderConsole/HashDecoder.cpp +++ b/pkNX.HashDecoderConsole/HashDecoder.cpp @@ -2,6 +2,7 @@ #include "HashDecoder.h" #include "Timer.h" +#include using namespace pkNXHashDecoder; @@ -19,6 +20,10 @@ std::unordered_map FnvHashDecoder::StartDecoding(const in { std::unordered_map foundKeys; + std::unordered_set HashesToLookFor{ + 0xC75DDBE25402B11C, + }; + // Reset all chars to starting point charIndex[0] = (uint32_t)startChar; @@ -26,7 +31,7 @@ std::unordered_map FnvHashDecoder::StartDecoding(const in hashBases[0] = kOffsetBasis_64; for (int i = 0; i < length - 1; ++i) { - hashBases[i] ^= AllowedChars[0]; + hashBases[i] ^= AllowedChars[charIndex[i]]; hashBases[i] *= kFnvPrime_64; // Move the hash to the next index as basis @@ -44,7 +49,7 @@ std::unordered_map FnvHashDecoder::StartDecoding(const in hash ^= AllowedChars[i]; hash *= kFnvPrime_64; - if (hash == 9252365659083253459ul) + if (HashesToLookFor.find(hash) != HashesToLookFor.end()) { charIndex[length - 1] = i; foundKeys.emplace(hash, BuildString(length)); @@ -83,9 +88,9 @@ std::unordered_map FnvHashDecoder::StartDecoding(const in hashBases[i + 1] = hashBases[i]; // Automatically test for shorter length strings - if (hashBases[i] == 9252365659083253459ul) + if (HashesToLookFor.find(hashBases[i]) != HashesToLookFor.end()) { - foundKeys.emplace(hashBases[i], BuildString(i)); + foundKeys.emplace(hashBases[i], BuildString(i + 1)); } } diff --git a/pkNX.HashDecoderConsole/HashDecoder.h b/pkNX.HashDecoderConsole/HashDecoder.h index 29f17f74..f3453463 100644 --- a/pkNX.HashDecoderConsole/HashDecoder.h +++ b/pkNX.HashDecoderConsole/HashDecoder.h @@ -7,17 +7,17 @@ namespace pkNXHashDecoder { public: std::unordered_map StartDecoding(int length, int startChar); - private: constexpr static int MAX_LENGTH = 128; constexpr static std::string_view AllowedChars = "etaoinshrdlcumw_0123456789fgypbvkjxqz/."; // Sorted by most common first constexpr static size_t AllowedCharsCount = AllowedChars.size(); + private: + std::string BuildString(size_t length) const; + constexpr static uint64_t kFnvPrime_64 = 0x00000100000001b3; constexpr static uint64_t kOffsetBasis_64 = 0xCBF29CE484222645; std::array charIndex{}; // Current AllowedChar index std::array hashBases{}; // Current AllowedChar index - - std::string BuildString(size_t length) const; }; }