replace printing code with somehting hopefully better?

This commit is contained in:
Lily Tsuru 2023-08-21 23:45:10 -04:00
parent ff97f2855e
commit 22c61ea451
1 changed files with 46 additions and 24 deletions

View File

@ -34,19 +34,19 @@ std::string RandomString(std::uint32_t length, TRandomAlgo& rng) {
constexpr std::uint32_t SwsfScramble(std::string_view code) {
std::uint32_t tally {};
auto cycle = [&tally](char c) {
tally = c + (tally * 5);
};
auto cycle = [&tally](char c) { tally = c + (tally * 5); };
cycle('c'); cycle('o'); cycle('d'); cycle('e'); cycle('_');
cycle('c');
cycle('o');
cycle('d');
cycle('e');
cycle('_');
for(auto* p = code.data(); *p; ++p)
cycle(tolower(*p));
return tally;
}
// using ChannelType = concurrent_channel<void(std::string& codeMatch)>;
// These is not atomic since no other threads update it
// there is no real point to making it atomic, other than
//
@ -95,20 +95,42 @@ struct BruteThreadState {
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 {}{} {}({:08x}){}", line, Color(172), code, Color(166), hash, Reset()) << std::flush;
struct FputcIterator {
using iterator_category = std::output_iterator_tag;
using value_type = void;
using difference_type = std::ptrdiff_t;
using pointer = void;
using reference = void;
FputcIterator(std::FILE* file) : file(file) {}
FputcIterator& operator*() { return *this; }
FputcIterator& operator++() { return *this; }
FputcIterator& operator++(int) { return *this; }
FputcIterator& operator=(const char& val) {
fputc(val, file);
return *this;
}
private:
std::FILE* file;
};
std::format_to(FputcIterator(stdout), "{}Thread {:2}: Trying code {}{} {}({:08x}){}", Line(line), line, Color(172), code, Color(166), hash,
Reset());
std::fflush(stdout);
}
void BruteForce(char prefix) {
//for(std::uint32_t i = 8; i <= code_length; ++i) {
// for(std::uint32_t i = 8; i <= code_length; ++i) {
std::string test_buffer(code_length, 'a');
test_buffer[0] = prefix;
// test all possible combinations
while(true) {
//std::string test_buffer = RandomString(code_length, rng);
// std::string test_buffer = RandomString(code_length, rng);
auto hash = SwsfScramble(test_buffer);
if( hash == code_hash) {
if(hash == code_hash) {
std::cerr << std::format("match: {} ({:08x})\n", test_buffer, hash);
}
@ -123,7 +145,7 @@ struct BruteThreadState {
private:
unsigned line;
std::random_device rd;
//swbf::Xoshiro256ss rng{rd};
// swbf::Xoshiro256ss rng{rd};
};
int main() {