bug-datamash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Add SHA-224 and SHA-384 options.


From: Shawn
Subject: [PATCH] Add SHA-224 and SHA-384 options.
Date: Mon, 09 Mar 2020 11:15:49 -0000

Adds sha224 and sha384 options for completeness to go along with the
existing sha digests.

---
 doc/datamash.texi     |  8 ++++++--
 src/datamash.c        |  2 +-
 src/field-ops.c       | 18 ++++++++++++++++++
 src/op-defs.c         |  2 ++
 src/op-defs.h         |  2 ++
 tests/datamash-sha.pl |  6 +++++-
 6 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/doc/datamash.texi b/doc/datamash.texi
index 4d87e18..2d8ae1d 100644
--- a/doc/datamash.texi
+++ b/doc/datamash.texi
@@ -169,8 +169,8 @@ all the values in the input file.
 
 @item Per-Line operations:
 @code{base64}, @code{debase64}, @code{md5}, @code{sha1},
-@code{sha256}, @code{sha512}, @code{bin}, @code{strbin},
-@code{round}, @code{floor}, @code{ceil}, @code{trunc},
+@code{sha224}, @code{sha256}, @code{sha384}, @code{sha512}, @code{bin},
+@code{strbin}, @code{round}, @code{floor}, @code{ceil}, @code{trunc},
 @code{frac}, @code{dirname}, @code{basename}, @code{extname}, @code{barename},
 @code{getnum}, @code{cut}
 
@@ -378,8 +378,12 @@ value which cannot be decoded.
 calculates md5 hash of the field
 @item sha1
 calculates sha1 hash of the field
+@item sha224
+calculates sha224 hash of the field
 @item sha256
 calculates sha256 hash of the field
+@item sha384
+calculates sha384 hash of the field
 @item sha512
 calculates sha512 hash of the field
 @item dirname
diff --git a/src/datamash.c b/src/datamash.c
index 57844a3..5284f1f 100644
--- a/src/datamash.c
+++ b/src/datamash.c
@@ -195,7 +195,7 @@ which require a pair of fields (e.g. 'pcov 2:6').\n"), 
stdout);
       fputs ("  rmdup\n",stdout);
 
       fputs (_("Per-Line operations:\n"),stdout);
-      fputs ("  base64, debase64, md5, sha1, sha256, sha512,\n", stdout);
+      fputs ("  base64, debase64, md5, sha1, sha224, sha256, sha384, 
sha512,\n", stdout);
       fputs ("  bin, strbin, round, floor, ceil, trunc, frac,\n", stdout);
       fputs ("  dirname, basename, barename, extname, getnum, cut\n", stdout);
 
diff --git a/src/field-ops.c b/src/field-ops.c
index 582b9a6..ffb6ec6 100644
--- a/src/field-ops.c
+++ b/src/field-ops.c
@@ -128,8 +128,12 @@ struct operation_data operations[] =
   {STRING_SCALAR, IGNORE_FIRST, STRING_RESULT},
   /* OP_SHA1 */
   {STRING_SCALAR, IGNORE_FIRST, STRING_RESULT},
+  /* OP_SHA224 */
+  {STRING_SCALAR, IGNORE_FIRST, STRING_RESULT},
   /* OP_SHA256 */
   {STRING_SCALAR, IGNORE_FIRST, STRING_RESULT},
+  /* OP_SHA384 */
+  {STRING_SCALAR, IGNORE_FIRST, STRING_RESULT},
   /* OP_SHA512 */
   {STRING_SCALAR, IGNORE_FIRST, STRING_RESULT},
   /* OP_P_COVARIANCE */
@@ -479,7 +483,9 @@ field_op_collect (struct fieldop *op,
     case OP_BASE64:
     case OP_MD5:
     case OP_SHA1:
+    case OP_SHA224:
     case OP_SHA256:
+    case OP_SHA384:
     case OP_SHA512:
     case OP_DIRNAME:
     case OP_BASENAME:
@@ -755,7 +761,9 @@ field_op_summarize_empty (struct fieldop *op)
     case OP_DEBASE64:
     case OP_MD5:
     case OP_SHA1:
+    case OP_SHA224:
     case OP_SHA256:
+    case OP_SHA384:
     case OP_SHA512:
     case OP_DIRNAME:
     case OP_BASENAME:
@@ -985,11 +993,21 @@ field_op_summarize (struct fieldop *op)
       field_op_to_hex (op, tmpbuf, 20);
       break;
 
+    case OP_SHA224:
+      sha224_buffer (op->str_buf, op->str_buf_used-1, tmpbuf);
+      field_op_to_hex (op, tmpbuf, 28);
+      break;
+
     case OP_SHA256:
       sha256_buffer (op->str_buf, op->str_buf_used-1, tmpbuf);
       field_op_to_hex (op, tmpbuf, 32);
       break;
 
+    case OP_SHA384:
+      sha384_buffer (op->str_buf, op->str_buf_used-1, tmpbuf);
+      field_op_to_hex (op, tmpbuf, 48);
+      break;
+
     case OP_SHA512:
       sha512_buffer (op->str_buf, op->str_buf_used-1, tmpbuf);
       field_op_to_hex (op, tmpbuf, 64);
diff --git a/src/op-defs.c b/src/op-defs.c
index bddbcc5..f1f5111 100644
--- a/src/op-defs.c
+++ b/src/op-defs.c
@@ -72,7 +72,9 @@ struct field_operation_definition field_operations[] =
   {"debase64",OP_DEBASE64,MODE_PER_LINE},
   {"md5",     OP_MD5,     MODE_PER_LINE},
   {"sha1",    OP_SHA1,    MODE_PER_LINE},
+  {"sha224",  OP_SHA224,  MODE_PER_LINE},
   {"sha256",  OP_SHA256,  MODE_PER_LINE},
+  {"sha384",  OP_SHA384,  MODE_PER_LINE},
   {"sha512",  OP_SHA512,  MODE_PER_LINE},
   {"dirname", OP_DIRNAME, MODE_PER_LINE},
   {"basename",OP_BASENAME,MODE_PER_LINE},
diff --git a/src/op-defs.h b/src/op-defs.h
index 898c441..860bd78 100644
--- a/src/op-defs.h
+++ b/src/op-defs.h
@@ -63,7 +63,9 @@ enum field_operation
   OP_DEBASE64,      /* Decode Base64 field */
   OP_MD5,           /* Calculate MD5 of a field */
   OP_SHA1,          /* Calculate SHA1 of a field */
+  OP_SHA224,        /* Calculate SHA224 of a field */
   OP_SHA256,        /* Calculate SHA256 of a field */
+  OP_SHA384,        /* Calculate SHA384 of a field */
   OP_SHA512,        /* Calculate SHA512 of a field */
   OP_P_COVARIANCE,  /* Population Covariance */
   OP_S_COVARIANCE,  /* Sample Covariance */
diff --git a/tests/datamash-sha.pl b/tests/datamash-sha.pl
index b63e3c1..b177184 100755
--- a/tests/datamash-sha.pl
+++ b/tests/datamash-sha.pl
@@ -37,7 +37,7 @@ use CuTmpdir qw(datamash);
 ## Perl 5.8 and earlier do not have Digest::SHA as core module.
 ## Skip the test if it is not found.
 my $have_sha =
-   eval qq{use Digest::SHA qw(sha1_hex sha256_hex  sha512_hex);1;};
+   eval qq{use Digest::SHA qw(sha1_hex sha224_hex sha256_hex sha384_hex 
sha512_hex);1;};
 
 CuSkip::skip "requires Perl>5.8 with Digest::SHA module\nload error:\n$@"
    unless $have_sha;
@@ -97,13 +97,17 @@ sub transform_column($$$)
 }
 
 my $out_g1_sha1 = transform_column ($in_g1, 2, \&sha1_hex);
+my $out_g1_sha224 = transform_column ($in_g1, 2, \&sha224_hex);
 my $out_g1_sha256 = transform_column ($in_g1, 2, \&sha256_hex);
+my $out_g1_sha384 = transform_column ($in_g1, 2, \&sha384_hex);
 my $out_g1_sha512 = transform_column ($in_g1, 2, \&sha512_hex);
 
 my @Tests =
 (
    ['sha1-1',  '-W sha1 2',   {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha1}],
+   ['sha224-1','-W sha224 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha224}],
    ['sha256-1','-W sha256 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha256}],
+   ['sha384-1','-W sha384 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha384}],
    ['sha512-1','-W sha512 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha512}],
 );
 
-- 
2.17.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]