-hex option for openssl rand
authorBodo Möller <bodo@openssl.org>
Mon, 2 Feb 2009 00:27:56 +0000 (00:27 +0000)
committerBodo Möller <bodo@openssl.org>
Mon, 2 Feb 2009 00:27:56 +0000 (00:27 +0000)
PR: 1831
Submitted by: Damien Miller

CHANGES
apps/rand.c
doc/apps/rand.pod

diff --git a/CHANGES b/CHANGES
index d4a46f0aa441ef20c65ca6d732d9f5630d71ef17..fb9868fa47e9341f9ac4841e910563d1b4126064 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,9 @@
 
  Changes between 0.9.8j and 0.9.8k  [xx XXX xxxx]
 
+  *) New -hex option for openssl rand.
+     [Matthieu Herrb]
+
   *) Print out UTF8String and NumericString when parsing ASN1.
      [Steve Henson]
 
index c3b26c466d9e91847ade09a20c574531adc937a8..7214a9d2a80da033cc59edd5cf23e743924c84e1 100644 (file)
@@ -68,7 +68,8 @@
 
 /* -out file         - write to file
  * -rand file:file   - PRNG seed files
- * -base64           - encode output
+ * -base64           - base64 encode output
+ * -hex              - hex encode output
  * num               - write 'num' bytes
  */
 
@@ -84,6 +85,7 @@ int MAIN(int argc, char **argv)
        char *outfile = NULL;
        char *inrand = NULL;
        int base64 = 0;
+       int hex = 0;
        BIO *out = NULL;
        int num = -1;
 #ifndef OPENSSL_NO_ENGINE
@@ -133,6 +135,13 @@ int MAIN(int argc, char **argv)
                        else
                                badopt = 1;
                        }
+               else if (strcmp(argv[i], "-hex") == 0)
+                       {
+                       if (!hex)
+                               hex = 1;
+                       else
+                               badopt = 1;
+                       }
                else if (isdigit((unsigned char)argv[i][0]))
                        {
                        if (num < 0)
@@ -148,6 +157,9 @@ int MAIN(int argc, char **argv)
                        badopt = 1;
                }
 
+       if (hex && base64)
+               badopt = 1;
+
        if (num < 0)
                badopt = 1;
        
@@ -160,7 +172,8 @@ int MAIN(int argc, char **argv)
                BIO_printf(bio_err, "-engine e             - use engine e, possibly a hardware device.\n");
 #endif
                BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
-               BIO_printf(bio_err, "-base64               - encode output\n");
+               BIO_printf(bio_err, "-base64               - base64 encode output\n");
+               BIO_printf(bio_err, "-hex                  - hex encode output\n");
                goto err;
                }
 
@@ -210,7 +223,14 @@ int MAIN(int argc, char **argv)
                r = RAND_bytes(buf, chunk);
                if (r <= 0)
                        goto err;
-               BIO_write(out, buf, chunk);
+               if (!hex) 
+                       BIO_write(out, buf, chunk);
+               else
+                       {
+                       for (i = 0; i < chunk; i++)
+                               BIO_printf(out, "%02x", buf[i]);
+                       BIO_puts(out, "\n");
+                       }
                num -= chunk;
                }
        (void)BIO_flush(out);
index 75745ca0023c1ec3c1fd7559fa5f7a6ec804b03e..d1d213ef43cbd07f1fc885393422049f2f9c3685 100644 (file)
@@ -10,6 +10,7 @@ B<openssl rand>
 [B<-out> I<file>]
 [B<-rand> I<file(s)>]
 [B<-base64>]
+[B<-hex>]
 I<num>
 
 =head1 DESCRIPTION
@@ -41,6 +42,10 @@ all others.
 
 Perform base64 encoding on the output.
 
+=item B<-hex>
+
+Show the output as a hex string.
+
 =back
 
 =head1 SEE ALSO