Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Secure random password generation via /dev/random */
- /* James Hess Copyright (C) 2011, 2012; All Rights Reserved */
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/time.h>
- int main(argc,argv)
- int argc; char *argv[];
- {
- char buf[1024] = "", junk[80], *f; /* Buffer to read file data into */
- /* Buffer size, default password length */
- int bufsize = sizeof(buf), passlen = 15;
- struct timeval tv1, tv2;
- clock_t cl1 = clock(), cl2 = 0;
- /* Repeat XOR this many random values per password character */
- int repeats = 30;
- /* List of characters to use in passwords */
- //char maptop[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-.,/~!#$%^&*()+=[]{}|;:\"<>/";
- //char maptop[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- char maptop[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int maptop_size = sizeof maptop;
- unsigned char random_byteval;
- int i, j, k;
- /* Open /dev/random */
- FILE* fp = fopen("/dev/random","r");
- if (argv && argc > 1 && argv[1] && (j = atoi(argv[1])) && 0 < passlen )
- passlen = j;
- gettimeofday(&tv1, NULL);
- puts("Press [ENTER] to continue");
- fgets(junk, 10, stdin);
- gettimeofday(&tv2, NULL);
- cl2 = clock();
- if (fp) {
- for(i = 0; i < passlen; i++) {
- for (f = (char *)&cl1; f < ( sizeof(clock_t) + (char *)&cl1); f++, random_byteval ^= (*f) ) ;
- for (f = (char *)&cl2; f < ( sizeof(clock_t) + (char *)&cl2); f++, random_byteval ^= (*f) ) ;
- for (f = (char *)&tv1; f < ((char *)&tv1 + sizeof(struct timeval)); f++, random_byteval ^= (*f));
- for (f = (char *)&tv2; f < ( (char *)&tv2 + sizeof(struct timeval)); f++, random_byteval ^= (*f) );
- /* Read in that a buffer full of random bytes, and XOR each byte together */
- /* repeat 'repeats' time, for each password character to be generated */
- for(k = 0; k < repeats; k++) {
- if ( fread(buf, bufsize, 1, fp) >= 1 ) {
- for(j = 0 ; j < bufsize ; j++) {
- random_byteval ^= buf[j];
- }
- } else {
- perror("fread");
- exit(0);
- }
- }
- /* pick a password character in the chosen character set. */
- printf("%c", maptop[ random_byteval % maptop_size ] );
- }
- } else perror("fopen");
- puts("");
- }
- /*
- Copyright (c) 2013 James Hess
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- 3. All advertising materials mentioning features or use of this software
- must display the following acknowledgement:
- This product includes software developed by James Hess.
- 4. Neither the name of the organization nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement