Comments (1)
The pw_random_number function can be re-written as follows to remove the modulo bias
/*
-
Generate a random number n, where 0 <= n < max_num, using
-
/dev/urandom if possible.
-
max_num is never greater than 127 for this application
*/
int pw_random_number(max_num)
int max_num;
{
unsigned char rand_num = 0xff;
unsigned char mask = 0x7f;
int i, fd = get_random_fd();
int lose_counter = 0x1f;
char *cp = (char *) &rand_num;if (max_num <= 0x3f) mask = 0x3f;
if (max_num <= 0x1f) mask = 0x1f;if (fd >= 0) {
while (rand_num >= (unsigned int)max_num) { // while loop removes modulo bias
i = read(fd, cp, 1);
rand_num &= mask;
if ((i < 0) &&
((errno == EINTR) || (errno == EAGAIN)))
continue;
if (i <= 0) {
if (!lose_counter--) break;
continue;
}
}
close(fd);
return (rand_num);
}/* We weren't able to use /dev/random, fail hard */
fprintf(stderr, "No entropy available!\n");
exit(1);
}
from pwgen.
Related Issues (9)
- Add an option to fully use /dev/random to as a random source for pwgen. HOT 2
- option -c to include at least one capital letter fails HOT 1
- Update SourceForge to point to this GitHub repo
- The `-n` flag fails with short passwords HOT 1
- `-y` includes backlash HOT 1
- `-B -y` includes symbols that might be confused.
- [feature suggestion] Generate passphrases too
- 8 character passwords are now easily crackable, change the default to 12 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pwgen.