Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,185 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,393 people online right now. Registration is fast and FREE... Join Now!




random password generator

 
Reply to this topicStart new topic

random password generator, generate random password for the text entered by the user according to

code fusion
8 Jul, 2008 - 10:03 PM
Post #1

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 1

cpp

#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;
}


** Edit ** code.gif
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Random Password Generator
8 Jul, 2008 - 10:20 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,504



Thanked: 67 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Do you have a specific question, or are you just sharing code?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 11:26AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month