Debian Codegraph @

gpw.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* GPW - Generate pronounceable passwords
2 This program uses statistics on the frequency of three-letter sequences
3 in your dictionary to generate passwords. The statistics are in trigram.h,
4 generated there by the program loadtris. Use different dictionaries
5 and you'll get different statistics.
6
7 This program can generate every word in the dictionary, and a lot of
8 non-words. It won't generate a bunch of other non-words, call them the
9 unpronounceable ones, containing letter combinations found nowhere in
10 the dictionary. My rough estimate is that if there are 10^6 words,
11 then there are about 10^9 pronounceables, out of a total population
12 of 10^11 8-character strings. I base this on running the program a lot
13 and looking for real words in its output.. they are very rare, on the
14 order of one in a thousand.
15
16 The best way to use this program is to generate multiple words. Then
17 pick one you like and transform it with punctuation, capitalization,
18 and other changes to come up with a new password.
19
20 THVV 6/1/94 Coded
21
22 */
23
24 #include "trigram.h"
25 #include "stdio.h"
26 #include "stdlib.h"
27 #include "math.h"
28 #include <sys/types.h>
29 /* following for SysV. */
30 /* #include <bsd/sys/time.h> */
31 /* following for BSD */
32 #include <sys/time.h>
•33 #include <limits.h>
34
35 long int pw_random_number(long int max_num);
36
37 int main (int argc, char ** argv) {
38 int password_length; /* how long should each password be */
39 int n_passwords; /* number of passwords to generate */
40 int pwnum; /* number generated so far */
41 int c1, c2, c3; /* array indices */
42 long sumfreq; /* total frequencies[c1][c2][*] */
43 double pik; /* raw random number in [0.1] from drand48() */
44 long ranno; /* random number in [0,sumfreq] */
45 long sum; /* running total of frequencies */
46 char password[100]; /* buffer to develop a password */
47 int nchar; /* number of chars in password so far */
48
49 password_length = 8; /* Default value for password length */
50 n_passwords = 10; /* Default value for number of pws to generate */
51
•52 srand48 (pw_random_number(LONG_MAX)); /* Set random seed. */
53
54 if (argc > 1) { /* If args are given, convert to numbers. */
55 n_passwords = atoi (&argv[1][0]);
56 if (argc > 2) {
57 password_length = atoi (&argv[2][0]);
58 }
59 }
60 if (argc > 3 || password_length > 99 ||
61 password_length < 0 || n_passwords < 0) {
62 printf (" USAGE: gpw [npasswds] [pwlength]\n");
63 exit (4);
64 }
65
66 /* Pick a random starting point. */
67 /* (This cheats a little; the statistics for three-letter
68 combinations beginning a word are different from the stats
69 for the general population. For example, this code happily
70 generates "mmitify" even though no word in my dictionary
71 begins with mmi. So what.) */
72 for (pwnum=0; pwnum < n_passwords; pwnum++) {
73 pik = drand48 (); /* random number [0,1] */
74 sumfreq = sigma; /* sigma calculated by loadtris */
75 ranno = pik * sumfreq; /* Weight by sum of frequencies. */
76 sum = 0;
77 for (c1=0; c1 < 26; c1++) {
78 for (c2=0; c2 < 26; c2++) {
79 for (c3=0; c3 < 26; c3++) {
80 sum += tris[c1][c2][c3];
81 if (sum > ranno) { /* Pick first value */
82 password[0] = 'a' + c1;
83 password[1] = 'a' + c2;
84 password[2] = 'a' + c3;
85 c1 = c2 = c3 = 26; /* Break all loops. */
86 } /* if sum */
87 } /* for c3 */
88 } /* for c2 */
89 } /* for c1 */
90
91 /* Do a random walk. */
92 nchar = 3; /* We have three chars so far. */
93 while (nchar < password_length) {
94 password[nchar] = '\0';
95 password[nchar+1] = '\0';
96 c1 = password[nchar-2] - 'a'; /* Take the last 2 chars */
97 c2 = password[nchar-1] - 'a'; /* .. and find the next one. */
98 sumfreq = 0;
99 for (c3=0; c3 < 26; c3++)
100 sumfreq += tris[c1][c2][c3];
101 /* Note that sum < duos[c1][c2] because
102 duos counts all digraphs, not just those
103 in a trigraph. We want sum. */
104 if (sumfreq == 0) { /* If there is no possible extension.. */
105 break; /* Break while nchar loop & print what we have. */
106 }
107 /* Choose a continuation. */
108 pik = drand48 ();
109 ranno = pik * sumfreq; /* Weight by sum of frequencies for row. */
110 sum = 0;
111 for (c3=0; c3 < 26; c3++) {
112 sum += tris[c1][c2][c3];
113 if (sum > ranno) {
114 password[nchar++] = 'a' + c3;
115 c3 = 26; /* Break the for c3 loop. */
116 }
117 } /* for c3 */
118 } /* while nchar */
119 printf ("%s\n", password);
120 } /* for pwnum */
121 exit (0);
122 }