[Semibug] Help with Random Password Generator

Jonathan Drews jondrews at fastmail.com
Thu Apr 11 00:28:41 EDT 2024


I get the following warning when I link the program RandomPasswdGen

$ cc -Wall -c randpass.c
$ cc randpass.o -o RandomPasswdGen                                              
randpass.c(randpass.o:(main)): warning: rand() may return deterministic values,
is that what you want?

How can I modify:
srand((int) time(NULL));              /*  Set the seed */

to make it less deterministic ?

I use this program all the time to generate random passwords.

$ ./RandomPasswdGen                                                             
IMLzukYuABUs2T3ffTaJg8RQkvCg3kNP^kaHOhtu

I am using OpenBSD 7.5 and 

OpenBSD clang version 16.0.6
Target: amd64-unknown-openbsd7.5
Thread model: posix
InstalledDir: /usr/bin

/*
                   
 * randpass.c -- generate really random passwords.
                   
 *
                   
 * Written by Carl S. Gutekunst.  Released into the Public Domain
95/09/28                 
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define PASSWD_LEN 40                   /* Number of chars in a
password */

char passchars[] =

"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxuz";
char symbchars[] =
        "!$%&()*+,-./:;<=>?[]^{|}";

int
main()
{
    int   passIndex;
    int   symbIndex;
    char  passwd[PASSWD_LEN+1];

    srand((int) time(NULL));                       /*  Set the seed
*/

    /*
                   
     * One position will have a symbol; the rest are alphanumeric
only.  This              
     * corresponds to the mandates of many UNIX password checkers.
                   
     */
    symbIndex = rand() % PASSWD_LEN;

    /*
                   
     * Fill the password string with random characters.
                   
     */
    for (passIndex = 0; passIndex < PASSWD_LEN; ++passIndex) {
        if (symbIndex == passIndex) {
            passwd[passIndex] = symbchars[rand() % (sizeof(symbchars) - 1)];
        }
        else {
            passwd[passIndex] = passchars[rand() % (sizeof(passchars) - 1)];
        }
    }


    /*
                   
     * Write new passwd to stdout.
                   
     */
    passwd[PASSWD_LEN] = '\0';
    puts(passwd);
    return 0;
} 



More information about the Semibug mailing list