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 <time.h>
- #include <sys/time.h>
- /* Number of entropy buffers to require for each byte of secret generated (More = longer wait) */
- #define _EXTRA_ITERATIONS 3
- /* Number bytes entropy to require for 1 iteration on each byte of secret generated (More = longer wait) */
- #define _ENTROPY_BUFSIZE 8
- /* You should replace this with a system-specific value*/
- static char _EXTRA_SECRET[] = "fido&9uct>_^/38:u{E$43ikOrQS{R9ln2L/f]cqlr1ce9-MviXIWn4dk[nBv2<xu\"kiL!xY;Ps5ez&Ec8^*^P[]I8nQeu12xk*lys69L-Tfi8lRmx1]]/a|";
- int main(argc,argv)
- int argc; char *argv[];
- {
- char buf[_ENTROPY_BUFSIZE] = "", junk[80], *f; /* Buffer to read file data into */
- /* Buffer size, default password length */
- int bufsize = sizeof(buf), passlen = 15, m;
- struct timeval tv1, tv2;
- clock_t cl1 = clock(), cl2 = 0, cl3=0;
- /* Repeat XOR this many random values per password character */
- int repeats = _EXTRA_ITERATIONS;
- /* 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;
- if (fread(buf, bufsize, 1, fp) < 1 ) {
- perror("fread");
- return 1;
- }
- gettimeofday(&tv1, NULL);
- puts("Press [ENTER] to continue");
- fgets(junk, 10, stdin);
- gettimeofday(&tv2, NULL);
- cl2 = clock();
- for(m = 4; m < bufsize; m++)
- buf[(m - 4)%4] ^= buf[m];
- cl3 = cl2 - cl1 + time(0);
- for (i = 0, f = (char *)&cl3; f < ( sizeof(clock_t) + (char *)&cl3); buf[i] ^= (*f), i++, f++ ) ;
- srandom( buf[0] | (buf[1] << 8) | (buf[2] << 16) || (buf[3] << 24) );
- if (fp) {
- for(i = 0; i < passlen; i++) {
- cl3 = clock() - cl3;
- for (f = (char *)&cl1; f < ( sizeof(clock_t) + (char *)&cl1); random_byteval ^= (*f), f++ ) ;
- for (f = (char *)&cl2; f < ( sizeof(clock_t) + (char *)&cl2); random_byteval ^= (*f), f++ ) ;
- for (f = (char *)&cl3; f < ( sizeof(clock_t) + (char *)&cl3); random_byteval ^= (*f), f++ ) ;
- for (f = (char *)&tv1; f < ((char *)&tv1 + sizeof(struct timeval)); random_byteval ^= (*f), f++ ) ;
- for (f = (char *)&tv2; f < ( (char *)&tv2 + sizeof(struct timeval)); random_byteval ^= (*f), 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 < sizeof(_EXTRA_SECRET); j++)
- random_byteval ^= _EXTRA_SECRET[j] ^ (char)random();
- for(j = 0 ; j < bufsize ; j++) {
- random_byteval ^= buf[j] ^ (char)random();
- }
- } else {
- perror("fread");
- exit(0);
- }
- }
- /* pick a password character in the chosen character set. */
- printf("%c", maptop[ random_byteval % maptop_size ] );
- }
- } else perror("fopen");
- memset(_EXTRA_SECRET, 0, sizeof(_EXTRA_SECRET));
- memset(buf, 0, sizeof(buf));
- srandom(0);
- random_byteval = 0;
- cl1 = cl2 = cl3 = 0;
- memset(&tv1, 0, sizeof(struct timeval) );
- memset(&tv2, 0, sizeof(struct timeval) );
- puts("");
- puts("Press [ENTER] to clear screen");
- fgets(junk, 10, stdin);
- printf("\e[H\e[J\nDone.\n");
- 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