Refactor hash generation algorithm to not allocate
Should in theory speed stuff up a LOT.
This commit is contained in:
parent
cce929ec66
commit
ff97f2855e
53
src/main.cpp
53
src/main.cpp
|
@ -4,6 +4,8 @@
|
|||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
#include "xoshiro.hpp"
|
||||
|
||||
namespace asio = boost::asio;
|
||||
// nvm doesnt work lol :)
|
||||
// using asio::experimental::concurrent_channel;
|
||||
|
@ -17,9 +19,9 @@ std::string RandomString(std::uint32_t length, TRandomAlgo& rng) {
|
|||
auto acc = 0u;
|
||||
|
||||
// I love bit hacks!!!
|
||||
for(int i = 0; i < sizeof(res); ++i) {
|
||||
for(std::size_t i = 0; i < sizeof(res); ++i) {
|
||||
std::uint8_t c = (res >> acc);
|
||||
if(c >= 0x20 && c <= 0x7e) {
|
||||
if(islower(c)) {
|
||||
if(length-- <= 0)
|
||||
return s;
|
||||
s.push_back(c);
|
||||
|
@ -29,14 +31,17 @@ std::string RandomString(std::uint32_t length, TRandomAlgo& rng) {
|
|||
}
|
||||
}
|
||||
|
||||
std::uint32_t SwsfScramble(const std::string& code) {
|
||||
auto copy = std::format("code_{}", code);
|
||||
for(auto& c : copy)
|
||||
c = tolower(c);
|
||||
|
||||
constexpr std::uint32_t SwsfScramble(std::string_view code) {
|
||||
std::uint32_t tally {};
|
||||
for(auto* p = copy.data(); *p; ++p)
|
||||
tally = *p + (tally * 5);
|
||||
|
||||
auto cycle = [&tally](char c) {
|
||||
tally = c + (tally * 5);
|
||||
};
|
||||
|
||||
cycle('c'); cycle('o'); cycle('d'); cycle('e'); cycle('_');
|
||||
|
||||
for(auto* p = code.data(); *p; ++p)
|
||||
cycle(tolower(*p));
|
||||
return tally;
|
||||
}
|
||||
|
||||
|
@ -47,7 +52,7 @@ std::uint32_t SwsfScramble(const std::string& code) {
|
|||
//
|
||||
// maybe preventing some false sharing or other perf problem
|
||||
// ... but I don't think that matters, lol
|
||||
std::uint32_t code_hash = 0x2c75af55;
|
||||
std::uint32_t code_hash = 0x2c73af55;
|
||||
std::uint32_t code_length = 8;
|
||||
|
||||
bool NextCode(std::string& buffer, size_t start) {
|
||||
|
@ -67,8 +72,12 @@ bool NextCode(std::string& buffer, size_t start) {
|
|||
namespace {
|
||||
unsigned ln = 1;
|
||||
|
||||
auto Color(int n, const std::string& s) {
|
||||
return std::format("\33[38;5;{}m{}\33[m", n, s);
|
||||
auto Color(int n) {
|
||||
return std::format("\33[38;5;{}m", n);
|
||||
}
|
||||
|
||||
std::string_view Reset() {
|
||||
return "\33[m";
|
||||
}
|
||||
|
||||
auto Line(int l) {
|
||||
|
@ -84,9 +93,9 @@ namespace {
|
|||
struct BruteThreadState {
|
||||
BruteThreadState(unsigned line) : line(line) {}
|
||||
|
||||
void DisplayProgress(const std::string& code) {
|
||||
void DisplayProgress(const std::string& code, std::uint32_t hash) {
|
||||
std::lock_guard<std::mutex> lk(print_lock);
|
||||
std::cout << Line(line) << std::format("Thread {:2}: Trying code {}", line, Color(140, code)) << std::flush;
|
||||
std::cout << Line(line) << std::format("Thread {:2}: Trying code {}{} {}({:08x}){}", line, Color(172), code, Color(166), hash, Reset()) << std::flush;
|
||||
}
|
||||
|
||||
void BruteForce(char prefix) {
|
||||
|
@ -94,15 +103,17 @@ struct BruteThreadState {
|
|||
std::string test_buffer(code_length, 'a');
|
||||
test_buffer[0] = prefix;
|
||||
|
||||
|
||||
// test all possible combinations
|
||||
while(true) {
|
||||
// std::cout << std::format("trying code \"{}\"...\r", test_buffer);
|
||||
DisplayProgress(test_buffer);
|
||||
|
||||
if(SwsfScramble(test_buffer) == code_hash) {
|
||||
std::cerr << std::format("match: {}\n", test_buffer);
|
||||
//std::string test_buffer = RandomString(code_length, rng);
|
||||
auto hash = SwsfScramble(test_buffer);
|
||||
if( hash == code_hash) {
|
||||
std::cerr << std::format("match: {} ({:08x})\n", test_buffer, hash);
|
||||
}
|
||||
|
||||
DisplayProgress(test_buffer, hash);
|
||||
|
||||
if(!NextCode(test_buffer, 1))
|
||||
break;
|
||||
}
|
||||
|
@ -111,6 +122,8 @@ struct BruteThreadState {
|
|||
|
||||
private:
|
||||
unsigned line;
|
||||
std::random_device rd;
|
||||
//swbf::Xoshiro256ss rng{rd};
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
@ -120,7 +133,7 @@ int main() {
|
|||
|
||||
auto line = 0u;
|
||||
|
||||
// post onto the thread pool
|
||||
// post worker threads to run onto the thread pool & wait for them to complete
|
||||
for(int i = 0; i < 26; ++i)
|
||||
asio::post(pool, [i, l = line++]() {
|
||||
BruteThreadState state(l);
|
||||
|
|
Loading…
Reference in New Issue