473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

random password generator

Is there anything wrong with this program? It seems to behave
strangely if I give stdin EOF when asked for the character set...

/* BEGIN pwdgen.c */
#include <stdio.h>
#include "random.h"
#include <stdlib.h>
#include <string.h>

#define MAX_PWD_LEN 128
#define MAX_CHARSET 128

char *pwdgen(size_t length, const char *charset, char *dest);
char *ord(unsigned n);
long input(const char *prompt);

int main(void)
{
char charset[MAX_CHARSET] = "";
char password[MAX_PWD_LEN];
char *charset_end;
char prompt[56];
unsigned long pwd_len;
printf("Insert characters from which to make passwords "
"(at most %d characters):\n", MAX_CHARSET - 1);
fgets(charset, MAX_CHARSET, stdin);
charset_end = strchr(charset, '\n');
if (charset_end != NULL)
*charset_end = '\0';
else if (getchar() != '\n') {
fprintf(stderr, "Characters after the %d%s will be discarded\n",
MAX_CHARSET - 1, ord(MAX_CHARSET - 1));
scanf("%*[^\n]%*c");
}
sprintf(prompt, "Insert password length (at most %d): ",
MAX_PWD_LEN - 1);
pwd_len = (unsigned long)input(prompt);
if (pwd_len MAX_PWD_LEN - 1) {
printf("Maximum password length is %d.\n", MAX_PWD_LEN - 1);
pwd_len = MAX_PWD_LEN - 1;
}
fputs("Please wait...", stdout);
fflush(stdout);
pwdgen(pwd_len, charset, password);
puts("\rGenerated password is:");
puts(password);
return 0;
}

char *pwdgen(size_t length, const char *charset, char *dest)
/* Fills dest with length random characters from charset */
{
if (dest == NULL && (dest = malloc(length+1)) == NULL) {
perror("Unable to allocate memory");
return(NULL);
}
if (charset == NULL || charset[0] == '\0') {
fputs("Empty or null charset\n", stderr);
*dest = '\0';
} else {
char *cur = dest;
size_t n_chars = strlen(charset);
while (length-- 0)
*cur++ = charset[randlong(n_chars)];
*cur = '\0';
}
return dest;
}

long input(const char *prompt)
{
long result;
int flag;

fputs(prompt, stdout);
fflush(stdout);
do {
flag = scanf("%ld", &result);
switch (flag) {
case EOF:
fputs("EOF in stdin\n", stderr);
exit(EXIT_FAILURE);
case 0:
scanf("%*[^\n]%*c");
fputs("Please enter a numeric value: ", stdout);
fflush(stdout);
continue;
default:
break;
}
} while (flag < 1);
return result;
}

char *ord(unsigned n)
{
char *result;
if (n%100 - n%10 == 10) /* 10th, 11th, 12th ... */
result = "th";
else switch (n % 10) {
case 1: /* 1st, 21st, ... */
result = "st";
break;
case 2: /* 2nd, 22nd, ... */
result = "nd";
break;
case 3: /* 3rd, 23rd, ... */
result = "rd";
break;
default: /* all others */
result = "th";
break;
}
return result;
}
/* END pwdgen.c */

/* BEGIN random.h */
#ifndef RANDOM_H__
#define RANDOM_H__
unsigned long randlong(unsigned long max);
unsigned long randomize(unsigned long *max);
#endif
/* END random.h */

/* BEGIN random.c */
#include <stdio.h>
#include "random.h"

unsigned long randlong(unsigned long max)
/* Return a uniformly distributed random integer from 0 to max-1, or from 0
to
* ULONG_MAX if max is 0. Print an error message to stderr if the actual
range
* is smaller. */
{
static unsigned long randcurr = 0;
static unsigned long maxrcurr = 0;
unsigned long result;

if (max == 0) {
unsigned long max_;

result = randomize(&max_);
if (max_ < (unsigned long)(-1))
fprintf(stderr, "Not enough entropy. Effective maximum value is
"
" %lu.\n", max_);
return result;
}
while (maxrcurr - randcurr < (maxrcurr % max + 1) % max) { /* Unbias */
randcurr = randomize(&maxrcurr);
if (maxrcurr < max) {
fprintf(stderr, "Not enough entropy. Effective maximum value is
"
" %lu.\n", max);
break;
}
}
result = randcurr % max;
randcurr /= max;
maxrcurr /= max;
return result;
}
/* END random.c */

/* BEGIN randomize.c (Linux version -- YMMV) */
#include <stdio.h>
#include <stdlib.h>
unsigned long randomize(unsigned long *max)
/* Set *max to a large value, and return a uniformly distributed random
* integer from 0 to *max */
{
FILE *random = fopen("/dev/random", "rb");
unsigned long result;
unsigned long max_;

if (random != NULL && fread(&result, sizeof result, 1, random) == 1)
max_ = (unsigned long)(-1);
else {
result = rand();
max_ = RAND_MAX;
}
if (max != NULL)
*max = max_;
if (random != NULL)
fclose(random);
return result;
}
/* END randomize.c */

--
char s[]="\16Jsa ukenethr ,cto haCr\n";int main(void){*s*=5;*
s%=23;putchar(s[0][s]);return*s-14?main():!putchar(9[s+*s]);}
Apr 28 '07 #1
3 5276
Army1987 said:
Is there anything wrong with this program? It seems to behave
strangely if I give stdin EOF when asked for the character set...
Don't you think it would be a good idea to test for this condition and
take appropriate action if it is encountered?
printf("Insert characters from which to make passwords "
"(at most %d characters):\n", MAX_CHARSET - 1);
fgets(charset, MAX_CHARSET, stdin);
You need to check whether fgets succeeded, and take appropriate action
if it did not.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 28 '07 #2
On Sat, 28 Apr 2007 14:44:05 +0200, "Army1987" <pl********@for.it>
wrote:
>Is there anything wrong with this program? It seems to behave
strangely if I give stdin EOF when asked for the character set...
Define strangely. How do you give stdin EOF? If you give stdin EOF,
will there ever be a '\n'?

snip
fgets(charset, MAX_CHARSET, stdin);
charset_end = strchr(charset, '\n');
if (charset_end != NULL)
*charset_end = '\0';
else if (getchar() != '\n') {
fprintf(stderr, "Characters after the %d%s will be discarded\n",
MAX_CHARSET - 1, ord(MAX_CHARSET - 1));
scanf("%*[^\n]%*c");
snip
Remove del for email
Apr 29 '07 #3

"Barry Schwarz" <sc******@doezl.netha scritto nel messaggio
news:pm********************************@4ax.com...
On Sat, 28 Apr 2007 14:44:05 +0200, "Army1987" <pl********@for.it>
wrote:
>>Is there anything wrong with this program? It seems to behave
strangely if I give stdin EOF when asked for the character set...

Define strangely. How do you give stdin EOF? If you give stdin EOF,
will there ever be a '\n'?
Yeah... It turns out that the way I used to end stdin <ot>(pressing ctrl-D,
in Linux)</otworks only if it is the first thing input on a line...
May 11 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: trs1800 | last post by:
/*i think someone should use this to name their baby*/ import java.util.Random; public class name { public static void main(String args) { String string1; char char1 = 7; string1 = " "; int...
1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
5
by: Alistair | last post by:
Hello folks... this is my first post in here. I'm new to ASP having done all my previous work in Flash and bog standard HTML. Only been learning for a couple of weeks. anyway...I have been...
14
by: Miranda | last post by:
Hi, I have a ASP/vbscript program that generates random passwords. The problem is I need to insert those passwords into an Access database of 327 clients. I have the random password program...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
4
by: David Eadie | last post by:
G'Day all, I cant work out how to create a random password generator. In specific the password (or well the output as a string) needs to be in the following format: abc123 So no capitals or...
14
by: avanti | last post by:
Hi, I need to generate random alphanumeric password strings for the users in my application using Javascript. Are there any links that will have pointers on the same? Thanks, Avanti
2
by: RYAN1214 | last post by:
How can I use this random password code, and then insert the password into email which is sent to the user after the registration has been finished? thx <html> <head> <title>Javascript:...
3
by: ashishpandey | last post by:
i am in great need of source code of random password generator in c language. please help me by sending the c code random password generator. my email id:<removed>
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.