Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- int incrementPassword(char*, int);
- void initPassword(char*);
- int indexOf(char, int);
- const char* PASSWORD_CHARS = "abcdefghijklmnopqrstuvwxyz";
- const int PASSWORD_LENGTH = 8;
- const int DISPLAY = 0;
- int main(){
- char password[PASSWORD_LENGTH];
- int passwordCharPoolSize = strlen(PASSWORD_CHARS);
- clock_t start, stop;
- printf("Starting evaluation of %d-character length password, with %d characters\n",
- PASSWORD_LENGTH, passwordCharPoolSize);
- start = clock();
- while(incrementPassword(password, passwordCharPoolSize));
- stop = clock();
- double executionTime = (double)(stop - start) / CLOCKS_PER_SEC;
- printf("Execution time: %lf seconds\n", executionTime);
- return 0;
- }
- // The password keeps track of itself. In other words, the next
- // password can be derived from the previous one
- int incrementPassword(char* password, int PASSWORD_CHARS_LEN){
- int i = 0;
- while(i < PASSWORD_LENGTH){
- if(i == PASSWORD_LENGTH && password[0] == PASSWORD_CHARS[PASSWORD_CHARS_LEN - 1]){
- // The loop is over; the last character has been incremented
- break;
- }
- char currentChar = password[PASSWORD_LENGTH - 1 - i];
- if(currentChar == PASSWORD_CHARS[PASSWORD_CHARS_LEN - 1]){
- password[PASSWORD_LENGTH - 1 - i] = PASSWORD_CHARS[0];
- i++;
- }
- else{
- char nextChar= PASSWORD_CHARS[indexOf(currentChar, PASSWORD_CHARS_LEN) + 1];
- password[PASSWORD_LENGTH - 1 - i] = nextChar;
- if(DISPLAY){
- printf("%s\n", password);
- }
- return 1; // Keep the main loop running
- }
- }
- // Stops the main loop by returning false
- return 0;
- }
- // Initializes a password to the first character of the character pool
- void initPassword(char* password){
- int i = 0;
- while(i < PASSWORD_LENGTH){
- password[i] = PASSWORD_CHARS[0];
- i++;
- }
- }
- // Since the pool of characters isn't in order, this finds the
- // index of a given character
- int indexOf(char c, int PASSWORD_CHARS_LEN){
- int i = 0;
- while(i < PASSWORD_CHARS_LEN){
- if(PASSWORD_CHARS[i] == c){
- return i;
- }
- i++;
- }
- // Didn't find it
- return -1;
- }
Add Comment
Please, Sign In to add comment