473,729 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(prom pt);
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("\rGenerat ed 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_char s)];
*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_FAILU RE);
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(unsign ed long max);
unsigned long randomize(unsig ned long *max);
#endif
/* END random.h */

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

unsigned long randlong(unsign ed 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(&maxr curr);
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(unsig ned 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():!putc har(9[s+*s]);}
Apr 28 '07 #1
3 5310
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.c om...
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
3790
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 temp, length;
1
3701
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 specified range), random strings (specifying allowed characters and a distribution of lengths), or choose randomly between several generators (for better control of the distribution of values). Is there any library for this sort of thing in Python?...
5
8537
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 building a guestbook type page, but as with quite a lot of guestbooks and stuff some of the comments that get placed are not exactly suitable, and tracking people down is a little difficult.
14
4070
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 generating the 327 passwords, but have had no luck inserting them. =============================================== Here is the code that generates the passwords: =============================================== <% Option Explicit %>
5
3349
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 double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. Anybody have an easy and fast...
4
3496
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 other characters, Just the first three being letters and the last three being numbers in random. Basically im creating a list if these to write to a text file (I can do that part :-) )
14
4749
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
1970
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: Password Generator</title> <style type="text/css"> input, select { font-family: Verdana, Arial, sans-serif;
3
1714
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
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.