Home > front end >  How to generate an string iterating each character from array list c
How to generate an string iterating each character from array list c

Time:05-09

i want to generate an string of 64 characters from my char list but after each string generated it will iterate the first digit of the string to the next one and so on, after that will check wish is the result of the sha256 function for each string, for example i have the following char list char hex_numbers[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a' ,'b','c','d','e','f'};

i want to generate and check the sha256 result of each string containing each character in the list -> [string of 64 digits] from "00000...." until "fffff...."

i already have the sha256 function implemented my question is only about how can i iterate for each digit in the string and go to the next digit after the one that was used, if it makes sense

so i was thinking about looping in each one and going to the next iteration but how can i achieve such a thing? sorry if my question sounds confusing

for example the first string should be an 64 char strings all of zeros, the second one an '1' char [63] '0' chars, the third one an '2' char [63] '0' chars and so on until it reaches 'f[64 times]' by looping through all characters

CodePudding user response:

This problem is similar to finding the k-th number in X-ary

#include <algorithm>
#include <cstdio>
#include <iterator>
#include <unordered_map>
#include <vector>

int main(int argc, char const *argv[]) {
  const char alphabet[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
                             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

  auto generate_kth_string = [&alphabet](int len, size_t kth) {
    char *buf = new char[len   1];
    int size = sizeof(alphabet) / sizeof(alphabet[0]);
    for (int i = 0; i < len; i  ) {
      buf[len - i - 1] = alphabet[kth % size];
      kth /= size;
    }
    buf[len] = '\0';
    return buf;
  };

  int count = 256;
  int len = 64;
  for (int i = 0; i < count; i  ) {
    auto kth = generate_kth_string(len, i);
    printf("%s\n", kth);
    delete[] kth;
  }
}

Output:

0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000000000000000000000000000000000000000000003
0000000000000000000000000000000000000000000000000000000000000004
0000000000000000000000000000000000000000000000000000000000000005
0000000000000000000000000000000000000000000000000000000000000006
0000000000000000000000000000000000000000000000000000000000000007
0000000000000000000000000000000000000000000000000000000000000008
...
00000000000000000000000000000000000000000000000000000000000000fb
00000000000000000000000000000000000000000000000000000000000000fc
00000000000000000000000000000000000000000000000000000000000000fd
00000000000000000000000000000000000000000000000000000000000000fe
00000000000000000000000000000000000000000000000000000000000000ff

For a 64 bit number, it takes 2 ^ 64 calculations, so you can't rely on this algorithm to crack any password in a limited time

  •  Tags:  
  • c
  • Related