Commit a3b7e09114 for openssl.org

commit a3b7e09114922fb273a9079c9bd6e78daf751916
Author: Evy Garden <evysgarden@protonmail.com>
Date:   Tue Jun 30 17:48:45 2026 +0200

    Add -n option to openssl-rand to not output the trailing newline

    Reviewed-by: Matt Caswell <matt@openssl.foundation>
    Reviewed-by: Paul Dale <paul.dale@oracle.com>
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    MergeDate: Thu Jul  2 10:59:26 2026
    (Merged from https://github.com/openssl/openssl/pull/31795)

diff --git a/apps/rand.c b/apps/rand.c
index 7aec7b6e17..3b1647d9b1 100644
--- a/apps/rand.c
+++ b/apps/rand.c
@@ -23,6 +23,7 @@ typedef enum OPTION_choice {
     OPT_OUT,
     OPT_BASE64,
     OPT_HEX,
+    OPT_NO_NEWLINE,
     OPT_R_ENUM,
     OPT_PROV_ENUM
 } OPTION_CHOICE;
@@ -37,6 +38,7 @@ const OPTIONS rand_options[] = {
     { "out", OPT_OUT, '>', "Output file" },
     { "base64", OPT_BASE64, '-', "Base64 encode output" },
     { "hex", OPT_HEX, '-', "Hex encode output" },
+    { "n", OPT_NO_NEWLINE, '-', "Do not output the trailing newline" },

     OPT_R_OPTIONS,
     OPT_PROV_OPTIONS,
@@ -51,7 +53,7 @@ int rand_main(int argc, char **argv)
     BIO *out = NULL;
     char *outfile = NULL, *prog;
     OPTION_CHOICE o;
-    int format = FORMAT_BINARY, r, i, ret = 1;
+    int format = FORMAT_BINARY, r, i, ret = 1, newline = 1;
     size_t buflen = (1 << 16); /* max rand chunk size is 2^16 bytes */
     long num = -1;
     uint64_t scaled_num = 0;
@@ -82,6 +84,9 @@ int rand_main(int argc, char **argv)
         case OPT_HEX:
             format = FORMAT_TEXT;
             break;
+        case OPT_NO_NEWLINE:
+            newline = 0;
+            break;
         case OPT_PROV_CASES:
             if (!opt_provider(o))
                 goto end;
@@ -208,7 +213,7 @@ int rand_main(int argc, char **argv)
         }
         scaled_num -= chunk;
     }
-    if (format == FORMAT_TEXT)
+    if (newline && format == FORMAT_TEXT)
         BIO_puts(out, "\n");
     if (BIO_flush(out) <= 0)
         goto end;
diff --git a/doc/man1/openssl-rand.pod.in b/doc/man1/openssl-rand.pod.in
index d38961acc3..4d4cda2b4d 100644
--- a/doc/man1/openssl-rand.pod.in
+++ b/doc/man1/openssl-rand.pod.in
@@ -12,6 +12,7 @@ B<openssl rand>
 [B<-out> I<file>]
 [B<-base64>]
 [B<-hex>]
+[B<-n>]
 {- $OpenSSL::safe::opt_r_synopsis -}
 {- $OpenSSL::safe::opt_provider_synopsis -}
 I<num>[K|M|G|T]
@@ -55,6 +56,10 @@ Perform base64 encoding on the output.

 Show the output as a hex string.

+=item B<-n>
+
+Do not output the trailing newline.
+
 {- $OpenSSL::safe::opt_r_item -}

 {- $OpenSSL::safe::opt_provider_item -}