Add prefix support

This commit is contained in:
Jelle
2022-10-17 12:40:30 +02:00
parent 46ca817907
commit 986a84fa5f
4 changed files with 23 additions and 15 deletions

View File

@@ -21,15 +21,13 @@ public class FnvHashDecoder
private const ulong kFnvPrime_64 = 0x00000100000001b3;
private const ulong kOffsetBasis_64 = 0xCBF29CE484222645;
public Dictionary<ulong, HashSet<string>> FoundKeys = new();
public Dictionary<ulong, string> FoundKeys = new();
public Dictionary<ulong, HashSet<string>> Run(int minLength, int maxLength)
public Dictionary<ulong, string> 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<Task>();
for (int i = 0; i < FnvHashDecoderLib.AllowedCharCount; ++i)
@@ -37,14 +35,13 @@ public class FnvHashDecoder
int startChar = i;
tasks.Add(Task.Run(() =>
{
var dict = FnvHashDecoderLib.StartDecoding(length, startChar);
var dict = FnvHashDecoderLib.StartDecoding(15, startChar, "pm0025_");
if (dict.Count != 0)
{
foreach (var x in dict)
{
FoundKeys.TryAdd(x.Key, new());
FoundKeys[x.Key].Add(x.Value);
FoundKeys.TryAdd(x.Key, x.Value);
Debug.WriteLine($"{x.Key} {x.Value}");
}
}

View File

@@ -1,15 +1,17 @@
#pragma once
#include "../pkNX.HashDecoderConsole/HashDecoder.h"
#include <msclr/marshal_cppstd.h>
using namespace System;
namespace pkNXHashDecoder {
private ref class FnvHashDecoderWrapper
{
public:
std::unordered_map<uint64_t, std::string> StartDecoding(int length, int startChar)
std::unordered_map<uint64_t, std::string> StartDecoding(int length, int startChar, std::string_view prefix)
{
return nativeHandle_->StartDecoding(length, startChar);
return nativeHandle_->StartDecoding(length, startChar, prefix);
}
property int AllowedCharCount
@@ -27,10 +29,11 @@ namespace pkNXHashDecoder {
public ref class FnvHashDecoderLib
{
public:
static System::Collections::Generic::Dictionary<System::UInt64, System::String^>^ StartDecoding(int length, int startChar)
static System::Collections::Generic::Dictionary<System::UInt64, System::String^>^ StartDecoding(int length, int startChar, System::String^ prefix)
{
FnvHashDecoderWrapper decoder{};
auto map = decoder.StartDecoding(length, startChar);
const std::string p = msclr::interop::marshal_as<std::string>(prefix);
const auto map = decoder.StartDecoding(length, startChar, p);
auto dict = gcnew System::Collections::Generic::Dictionary<System::UInt64, System::String^>();

View File

@@ -16,7 +16,7 @@ std::string FnvHashDecoder::BuildString(const size_t length) const
return { chars.begin(), chars.end() };
}
std::unordered_map<uint64_t, std::string> FnvHashDecoder::StartDecoding(const int length, const int startChar)
std::unordered_map<uint64_t, std::string> FnvHashDecoder::StartDecoding(const int length, const int startChar, const std::string_view prefix)
{
std::unordered_map<uint64_t, std::string> foundKeys;
@@ -24,8 +24,16 @@ std::unordered_map<uint64_t, std::string> FnvHashDecoder::StartDecoding(const in
0xC75DDBE25402B11C,
};
const size_t startIndex = prefix.length();
// Reset all chars to starting point
charIndex[0] = (uint32_t)startChar;
for (int i = 0; i < prefix.length(); ++i)
{
const auto& ch = prefix[i];
charIndex[i] = AllowedChars.find_first_of(ch);
}
charIndex[startIndex] = (uint32_t)startChar;
// Build the hash of all chars except the last one
hashBases[0] = kOffsetBasis_64;
@@ -39,7 +47,7 @@ std::unordered_map<uint64_t, std::string> FnvHashDecoder::StartDecoding(const in
}
//LoopTimer timer{};
while (charIndex[0] == startChar)
while (charIndex[startIndex] == startChar)
{
// Loop through all AllowedChars
const uint64_t hashBase = hashBases[length - 1];

View File

@@ -5,7 +5,7 @@ namespace pkNXHashDecoder {
class __declspec(align(64)) FnvHashDecoder
{
public:
std::unordered_map<uint64_t, std::string> StartDecoding(int length, int startChar);
std::unordered_map<uint64_t, std::string> StartDecoding(int length, int startChar, std::string_view prefix);
constexpr static int MAX_LENGTH = 128;
constexpr static std::string_view AllowedChars = "etaoinshrdlcumw_0123456789fgypbvkjxqz/."; // Sorted by most common first