0

So I need to crack a hash to solve a puzzle, and I've edited a Python program that iterates over all possible combinations, but the Python program is too slow as the hash changes every day, I know it's a hash of length: 8

The python program would work like this:

charset = "abcdefghijklmnopqrstuvwxyz012345679"
NUMBER_OF_CHARACTERS = len(charset)
sequence = list("a a a a a a a a".split(" "))
while(True):
    current = "".join(sequence)
    sequence = next(sequence)
    #check if hash of sequence matches target hash

the function: 'next' looks like this:

if len(string) <= 0:
    string.append(indexToCharacter(0))
else:
    string[0] = indexToCharacter((characterToIndex(string[0]) + 1) % NUMBER_OF_CHARACTERS)
    if characterToIndex(string[0]) is 0:
        return list(string[0]) + next(string[1:])
return string

indexToCharacter just returns the character in string charset at index (index)
characterToIndex returns the position of a given character in string index

so, characterToIndex("a") would return 0 and indexToCharacter(0) would return "a"

Now what I need is, to convert this Python program to a C++ program, as C++ is way faster, I have the indexToCharacter function, the characterToIndex function, but I can't seem to get the next function work.

I got this for the next function in C++

string next(string sequence)
{
    sequence[0] = indexToCharacter(characterToIndex(sequence[0])+1);
    if (characterToIndex(sequence[0]) == 0)
    {
        //Something
    }
}

string sequence = "aaaaaaaa";
next(sequence);

the code of indexToCharacter and characterToIndex:

int characterToIndex(char character)
{
    return charset.find(character);
}

char indexToCharacter(unsigned index)
{
    return charset[index];
}
11
  • So you just need to implement the next() function in c++? Commented Jul 15, 2016 at 16:49
  • Yes, only need the next function in c++ Commented Jul 15, 2016 at 16:51
  • Atleast provide what is the expected output to an input for the next function. Commented Jul 15, 2016 at 16:52
  • So... Just write whatever the Python version does in C++ and use string(1,sequence[0]) instead of list(string[0]). The problem is, such calls to string will allocate tons of memory in various places, so your memory will get very fragmented Commented Jul 15, 2016 at 16:52
  • Also please provide the entire code, implementation of indexToCharacter etc. Commented Jul 15, 2016 at 16:53

1 Answer 1

1

In this special case, I would not hassle around with the std::string objects all the time. You have a fixed length, right? You can do it very efficiently this way then (works both in C++ and C... – just adjust buffer length and characters list to your own needs):

char characters[] = { 'a', 'b', 'c' };
char buffer[3];
char* positions[sizeof(buffer)];
for(unsigned int i = 0; i < sizeof(buffer); ++i)
{
    buffer[i] = *characters;
    positions[i] = characters;
}

unsigned int i;
do
{
    printf("%.*s\n", (int)sizeof(buffer), buffer);
    for(i = 0; i < sizeof(buffer); ++i)
    {
        ++positions[i];
        if(positions[i] < characters + sizeof(characters))
        {
            buffer[i] = *positions[i];
            break;
        }
        positions[i] = characters;
    }
}
while(i < sizeof(buffer));

I just printed out the values, you can do what ever you need to do with...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Works great :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.