shithub: blake2

Download patch

ref: 70acf250b99973c7b44dd476065ea53fd14e874a
parent: 918e39086b0a686a6a0417fd8e898a830567bc28
author: Artem Chudinov <arzeth0@gmail.com>
date: Fri Oct 30 17:42:01 EDT 2015

b2sum: Support a BSD-style checksum and improve the --help message

--- a/b2sum/b2sum.c
+++ b/b2sum/b2sum.c
@@ -20,6 +20,7 @@
 #include <ctype.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <stdbool.h>
 
 #include "blake2.h"
 
@@ -231,9 +232,14 @@
 static void usage( char **argv, int errcode )
 {
   FILE *out = errcode ? stderr : stdout;
-  fprintf( out, "Usage: %s [-a HASH] [FILE]...\n", argv[0] );
-  fprintf( out, "\tHASH in blake2b blake2s blake2bp blake2sp\n" );
-  fprintf( out, "\tWith no FILE, or when FILE is -, read standard input.\n" );
+  fprintf( out, "Usage: %s [OPTION]... [FILE]...\n", argv[0] );
+  fprintf( out, "\n" );
+  fprintf( out, "With no FILE, or when FILE is -, read standard input.\n" );
+  fprintf( out, "\n" );
+  fprintf( out, "  -a <hash>    hash algorithm (blake2b is default): \n"
+                "               [blake2b|blake2s|blake2bp|blake2sp]\n" );
+  fprintf( out, "  --tag        create a BSD-style checksum\n" );
+  fprintf( out, "  --help       display this help and exit\n" );
   exit( errcode );
 }
 
@@ -243,6 +249,7 @@
   blake2fn blake2_stream = blake2b_stream;
   size_t outlen   = BLAKE2B_OUTBYTES;
   unsigned char hash[BLAKE2B_OUTBYTES] = {0};
+  bool bsdstyle = false;
   int c;
   opterr = 1;
 
@@ -252,6 +259,7 @@
     int option_index = 0;
     static struct option long_options[] = {
       { "help", no_argument, 0, 0 },
+      { "tag", no_argument, 0, 0 },
       { NULL, 0, NULL, 0 }
     };
     c = getopt_long( argc, argv, "a:", long_options, &option_index );
@@ -258,10 +266,6 @@
     if( c == -1 ) break;
     switch( c )
     {
-    case 0:
-      if( 0 == strcmp( "help", long_options[option_index].name ) )
-        usage( argv, 0 );
-      break;
     case 'a':
       if( 0 == strcmp( optarg, "blake2b" ) )
       {
@@ -291,6 +295,13 @@
 
       break;
 
+    case 0:
+      if( 0 == strcmp( "help", long_options[option_index].name ) )
+        usage( argv, 0 );
+      else if( 0 == strcmp( "tag", long_options[option_index].name ) )
+        bsdstyle = true;
+      break;
+
     case '?':
       usage( argv, 1 );
       break;
@@ -320,10 +331,22 @@
       goto end1;
     }
 
+    if( bsdstyle )
+    {
+      if( blake2_stream == blake2b_stream ) printf( "BLAKE2b" );
+      else if( blake2_stream == blake2bp_stream ) printf( "BLAKE2bp" );
+      else if( blake2_stream == blake2s_stream ) printf( "BLAKE2s" );
+      else if( blake2_stream == blake2sp_stream ) printf( "BLAKE2sp" );
+      printf( " (%s) = ", argv[i] );
+    }
+
     for( int j = 0; j < outlen; ++j )
       printf( "%02x", hash[j] );
 
-    printf( " %s\n", argv[i] );
+    if( bsdstyle )
+      printf( "\n" );
+    else
+      printf( " %s\n", argv[i] );
 end1:
     if( f != stdin ) fclose( f );
 end0: ;
--