Advertisement
mysidia

Random password generation using /dev/random

Sep 24th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.11 KB | None | 0 0
  1. /* Secure random password generation via /dev/random */
  2. /* James Hess  Copyright (C) 2011, 2012;  All Rights Reserved */
  3.  
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <sys/time.h>
  9.  
  10. int main(argc,argv)
  11.   int argc;  char *argv[];
  12. {
  13.    char buf[1024] = "", junk[80], *f;   /* Buffer to read file data into */
  14.  
  15.                  /* Buffer size, default password length */
  16.    int bufsize = sizeof(buf), passlen = 15;
  17.    struct timeval tv1, tv2;
  18.    clock_t cl1 = clock(), cl2 = 0;
  19.    
  20.  
  21.                  /* Repeat XOR this many random values per password character */
  22.    int repeats = 30;
  23.  
  24.    /*  List of characters to use in passwords */
  25.     //char maptop[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-.,/~!#$%^&*()+=[]{}|;:\"<>/";
  26.    //char maptop[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  27.    char maptop[]   = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  28.    int maptop_size = sizeof maptop;
  29.  
  30.    unsigned char random_byteval;
  31.    int i, j, k;
  32.  
  33.    /* Open /dev/random */
  34.    FILE* fp = fopen("/dev/random","r");
  35.  
  36.    if (argv && argc > 1 && argv[1] && (j = atoi(argv[1])) && 0 < passlen  )
  37.        passlen = j;
  38.  
  39.    gettimeofday(&tv1, NULL);
  40.    puts("Press [ENTER] to continue");
  41.    fgets(junk, 10, stdin);
  42.    gettimeofday(&tv2, NULL);
  43.    cl2 = clock();
  44.  
  45.  
  46.    if (fp) {
  47.      for(i = 0; i < passlen; i++) {
  48.          for (f = (char *)&cl1; f <  ( sizeof(clock_t) + (char *)&cl1); f++, random_byteval ^= (*f)  ) ;
  49.          for (f = (char *)&cl2; f <  ( sizeof(clock_t) + (char *)&cl2); f++, random_byteval ^= (*f)  ) ;
  50.          for (f = (char *)&tv1; f < ((char *)&tv1 + sizeof(struct timeval));  f++, random_byteval ^= (*f));
  51.          for (f = (char *)&tv2; f <  ( (char *)&tv2 + sizeof(struct timeval));  f++, random_byteval ^= (*f) );
  52.  
  53.        /* Read in that a buffer full of random bytes,  and XOR each byte together */
  54.        /* repeat 'repeats'  time, for each  password character to be generated  */
  55.  
  56.        for(k = 0; k < repeats; k++) {
  57.           if (  fread(buf, bufsize, 1, fp)  >= 1 ) {
  58.               for(j = 0 ; j < bufsize ; j++) {
  59.                   random_byteval ^= buf[j];
  60.               }
  61.            } else {
  62.               perror("fread");
  63.           exit(0);
  64.           }
  65.        }
  66.  
  67.        /* pick a password character in the chosen character set. */
  68.        printf("%c", maptop[ random_byteval % maptop_size ]  );
  69.  
  70.      }
  71.    } else  perror("fopen");
  72.  
  73.    puts("");
  74. }
  75.  
  76. /*
  77. Copyright (c) 2013  James Hess
  78. All rights reserved.
  79.  
  80. Redistribution and use in source and binary forms, with or without
  81. modification, are permitted provided that the following conditions are met:
  82. 1. Redistributions of source code must retain the above copyright
  83.    notice, this list of conditions and the following disclaimer.
  84. 2. Redistributions in binary form must reproduce the above copyright
  85.    notice, this list of conditions and the following disclaimer in the
  86.    documentation and/or other materials provided with the distribution.
  87. 3. All advertising materials mentioning features or use of this software
  88.    must display the following acknowledgement:
  89.    This product includes software developed by James Hess.
  90. 4. Neither the name of the organization nor the
  91.    names of its contributors may be used to endorse or promote products
  92.    derived from this software without specific prior written permission.
  93.  
  94. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  95. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  96. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  97. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  98. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  99. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  100. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  101. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  102. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  103. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  104. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement