Reputation: 55
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];
}
Upvotes: 0
Views: 510
Reputation: 25536
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...
Upvotes: 1