mkphrase.c: Allow a range for phrase entropy.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 8 Apr 2013 16:10:50 +0000 (17:10 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 8 Apr 2013 16:10:50 +0000 (17:10 +0100)
mkphrase.c

index 34ca8cc..1a5beab 100644 (file)
@@ -54,7 +54,7 @@
 /*----- Global state ------------------------------------------------------*/
 
 static unsigned min = 0, max = 256;    /* Word length bounds */
 /*----- Global state ------------------------------------------------------*/
 
 static unsigned min = 0, max = 256;    /* Word length bounds */
-static unsigned bits = 128;            /* Minimum acceptable entropy */
+static unsigned minbits = 128, maxbits = UINT_MAX; /* Acceptable entropy */
 static unsigned count = 1;             /* How many passphrases to make */
 
 static const char wchars[] = "abcdefghijklmnopqrstuvwxyz'";
 static unsigned count = 1;             /* How many passphrases to make */
 
 static const char wchars[] = "abcdefghijklmnopqrstuvwxyz'";
@@ -274,7 +274,8 @@ static void version(FILE *fp)
 static void usage(FILE *fp)
 {
   pquis(fp, "\
 static void usage(FILE *fp)
 {
   pquis(fp, "\
-Usage: $ [-p] [-b BITS] [-g GEN] [-n COUNT] [-r [MIN-]MAX] WORDLIST...\n\
+Usage: $ [-p] [-b MIN[-MAX]] [-g GEN] [-n COUNT]\n\
+\t[-r [MIN-]MAX] WORDLIST...\n                   \
 ");
 }
 
 ");
 }
 
@@ -291,7 +292,7 @@ supported are:\n\
 -h, --help             Show this help text.\n\
 -v, --version          Show the program's version number.\n\
 -u, --usage            Show a terse usage summary.\n\
 -h, --help             Show this help text.\n\
 -v, --version          Show the program's version number.\n\
 -u, --usage            Show a terse usage summary.\n\
--b, --bits=BITS                Produce at least BITS bits of entropy.\n\
+-b, --bits=MIN[-MAX]   Minimum and maximum bits of entropy.\n\
 -g, --generator=GEN    Use passphrase generator GEN.\n\
 -n, --count=COUNT      Generate COUNT passphrases.\n\
 -p, --probability      Show -log_2 of probability for each phrase.\n\
 -g, --generator=GEN    Use passphrase generator GEN.\n\
 -n, --count=COUNT      Generate COUNT passphrases.\n\
 -p, --probability      Show -log_2 of probability for each phrase.\n\
@@ -344,10 +345,13 @@ int main(int argc, char *argv[])
        exit(0);
       case 'b': {
        char *p;
        exit(0);
       case 'b': {
        char *p;
-       unsigned long n = strtoul(optarg, &p, 0);
-       if (*p)
-         die(EXIT_FAILURE, "bad integer `%s'", optarg);
-       bits = n;
+       minbits = strtoul(optarg, &p, 0);
+       if (*p == '-')
+         maxbits = strtoul(p + 1, &p, 0);
+       else
+         maxbits = UINT_MAX;
+       if (*p || minbits > maxbits)
+         die(EXIT_FAILURE, "bad entropy range `%s'", optarg);
       } break;
       case 'g': {
        ppgen_ops **p;
       } break;
       case 'g': {
        ppgen_ops **p;
@@ -422,10 +426,10 @@ int main(int argc, char *argv[])
   if (ops->endscan)
     ops->endscan(ctx);
 
   if (ops->endscan)
     ops->endscan(ctx);
 
-  for (i = 0; !count || i < count; i++) {
+  for (i = 0; !count || i < count; ) {
     double logp = 0;
     DRESET(&d);
     double logp = 0;
     DRESET(&d);
-    while (logp < bits) {
+    while (logp < minbits) {
       double pp;
       DRESET(&dd);
       pp = ops->gen(&dd, &rand_global, ctx);
       double pp;
       DRESET(&dd);
       pp = ops->gen(&dd, &rand_global, ctx);
@@ -436,10 +440,13 @@ int main(int argc, char *argv[])
       DPUTD(&d, &dd);
       logp += pp;
     }
       DPUTD(&d, &dd);
       logp += pp;
     }
+    if (logp >= (double)maxbits + 1)
+      continue;
     dstr_write(&d, stdout);
     if (f & f_showp)
       printf(" [%g]", logp);
     fputc('\n', stdout);
     dstr_write(&d, stdout);
     if (f & f_showp)
       printf(" [%g]", logp);
     fputc('\n', stdout);
+    i++;
   }
 
   ops->done(ctx);
   }
 
   ops->done(ctx);