Want more features on Pastebin? Sign Up, it's FREE!
Guest

Password generator

By: a guest on Aug 27th, 2013  |  syntax: C  |  size: 1.28 KB  |  views: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. /*
  5.  * computes a password consisting of random words
  6.  */
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.   if (argc<3){
  11.     printf("Usage:\n%s <path to wordlist> <number of words in password>\n", argv[0]);
  12.     exit(EXIT_FAILURE);
  13.   }
  14.    
  15.   char* path = argv[1];
  16.   int count = atoi(argv[2]);
  17.   char buffer[100];
  18.   int rand_i, lc=0;
  19.   char c=0;
  20.   FILE* rand = NULL;
  21.   FILE* fp = NULL;
  22.  
  23.   /* for extra entropy use /dev/random */
  24.   if(!(rand = fopen("/dev/urandom", "r"))) {
  25.     printf("Failed to open /dev/urandom\n");
  26.     exit(EXIT_FAILURE);
  27.   }
  28.   if(!(fp = fopen(path, "r"))) {
  29.     printf("Failed to open %s\n", path);
  30.     exit(EXIT_FAILURE);
  31.   }
  32.  
  33.   /* get number of lines */
  34.   while ((c = getc(fp)) != EOF) {
  35.     if ( c=='\n')
  36.       lc++;
  37.   }
  38.   rewind(fp);
  39.  
  40.   /* find 'count' number of words and output them */
  41.   while(count--) {
  42.     fread(&rand_i, sizeof(rand_i), 1, rand);
  43.     if (rand_i<0)
  44.       rand_i = -rand_i;
  45.     rand_i = (rand_i % lc);
  46.     int pos = 0;
  47.     while(pos<rand_i) {
  48.       c = getc(fp);
  49.       if ( c=='\n')
  50.         pos++;
  51.     }
  52.     c = 'a';
  53.     while((c=getc(fp)) && c!='\n'){
  54.       putchar(c);
  55.     }
  56.     putchar(' ');
  57.     rewind(fp);
  58.   }
  59.  
  60.   putchar('\n');
  61.   fclose(fp);
  62.   fclose(rand);
  63.   return 0;
  64. }
clone this paste RAW Paste Data