Refactor hash generation algorithm to not allocate

Should in theory speed stuff up a LOT.
This commit is contained in:
Lily Tsuru 2023-08-21 23:14:49 -04:00
parent cce929ec66
commit ff97f2855e
1 changed files with 33 additions and 20 deletions

View File

@ -4,6 +4,8 @@
#include <format> #include <format>
#include <iostream> #include <iostream>
#include "xoshiro.hpp"
namespace asio = boost::asio; namespace asio = boost::asio;
// nvm doesnt work lol :) // nvm doesnt work lol :)
// using asio::experimental::concurrent_channel; // using asio::experimental::concurrent_channel;
@ -17,9 +19,9 @@ std::string RandomString(std::uint32_t length, TRandomAlgo& rng) {
auto acc = 0u; auto acc = 0u;
// I love bit hacks!!! // 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); std::uint8_t c = (res >> acc);
if(c >= 0x20 && c <= 0x7e) { if(islower(c)) {
if(length-- <= 0) if(length-- <= 0)
return s; return s;
s.push_back(c); 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) { constexpr std::uint32_t SwsfScramble(std::string_view code) {
auto copy = std::format("code_{}", code);
for(auto& c : copy)
c = tolower(c);
std::uint32_t tally {}; 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; return tally;
} }
@ -47,7 +52,7 @@ std::uint32_t SwsfScramble(const std::string& code) {
// //
// maybe preventing some false sharing or other perf problem // maybe preventing some false sharing or other perf problem
// ... but I don't think that matters, lol // ... 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; std::uint32_t code_length = 8;
bool NextCode(std::string& buffer, size_t start) { bool NextCode(std::string& buffer, size_t start) {
@ -67,8 +72,12 @@ bool NextCode(std::string& buffer, size_t start) {
namespace { namespace {
unsigned ln = 1; unsigned ln = 1;
auto Color(int n, const std::string& s) { auto Color(int n) {
return std::format("\33[38;5;{}m{}\33[m", n, s); return std::format("\33[38;5;{}m", n);
}
std::string_view Reset() {
return "\33[m";
} }
auto Line(int l) { auto Line(int l) {
@ -84,9 +93,9 @@ namespace {
struct BruteThreadState { struct BruteThreadState {
BruteThreadState(unsigned line) : line(line) {} 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::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) { void BruteForce(char prefix) {
@ -94,15 +103,17 @@ struct BruteThreadState {
std::string test_buffer(code_length, 'a'); std::string test_buffer(code_length, 'a');
test_buffer[0] = prefix; test_buffer[0] = prefix;
// test all possible combinations // test all possible combinations
while(true) { while(true) {
// std::cout << std::format("trying code \"{}\"...\r", test_buffer); //std::string test_buffer = RandomString(code_length, rng);
DisplayProgress(test_buffer); auto hash = SwsfScramble(test_buffer);
if( hash == code_hash) {
if(SwsfScramble(test_buffer) == code_hash) { std::cerr << std::format("match: {} ({:08x})\n", test_buffer, hash);
std::cerr << std::format("match: {}\n", test_buffer);
} }
DisplayProgress(test_buffer, hash);
if(!NextCode(test_buffer, 1)) if(!NextCode(test_buffer, 1))
break; break;
} }
@ -111,6 +122,8 @@ struct BruteThreadState {
private: private:
unsigned line; unsigned line;
std::random_device rd;
//swbf::Xoshiro256ss rng{rd};
}; };
int main() { int main() {
@ -120,7 +133,7 @@ int main() {
auto line = 0u; 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) for(int i = 0; i < 26; ++i)
asio::post(pool, [i, l = line++]() { asio::post(pool, [i, l = line++]() {
BruteThreadState state(l); BruteThreadState state(l);