From 88adf3f7f2e9c6f1ea3c722ead41a5579b3292bf Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 5 Oct 2019 08:30:00 +0200 Subject: Add sha{1,256}::empty() --- libbutl/sha1.cxx | 10 ++++++++-- libbutl/sha1.mxx | 6 ++++++ libbutl/sha256.cxx | 10 ++++++++-- libbutl/sha256.mxx | 6 ++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/libbutl/sha1.cxx b/libbutl/sha1.cxx index 1ca8a2a..3c1cbdc 100644 --- a/libbutl/sha1.cxx +++ b/libbutl/sha1.cxx @@ -73,7 +73,7 @@ namespace butl { sha1:: sha1 () - : done_ (false) + : done_ (false), empty_ (true) { SHA1_Init (reinterpret_cast (buf_)); } @@ -81,7 +81,13 @@ namespace butl void sha1:: append (const void* b, size_t n) { - SHA1_Update (reinterpret_cast (buf_), b, n); + if (n != 0) + { + SHA1_Update (reinterpret_cast (buf_), b, n); + + if (empty_) + empty_ = false; + } } void sha1:: diff --git a/libbutl/sha1.mxx b/libbutl/sha1.mxx index 26d4b59..f69ae5a 100644 --- a/libbutl/sha1.mxx +++ b/libbutl/sha1.mxx @@ -74,6 +74,11 @@ LIBBUTL_MODEXPORT namespace butl explicit sha1 (ifdstream& i): sha1 () {append (i);} + // Check if any data has been hashed. + // + bool + empty () const {return empty_;} + // Extract result. // // It can be obtained as either a 20-byte binary digest or as a 40- @@ -119,5 +124,6 @@ LIBBUTL_MODEXPORT namespace butl mutable digest_type bin_; mutable bool done_; + bool empty_; }; } diff --git a/libbutl/sha256.cxx b/libbutl/sha256.cxx index 6dbe2a0..31730a2 100644 --- a/libbutl/sha256.cxx +++ b/libbutl/sha256.cxx @@ -67,7 +67,7 @@ namespace butl { sha256:: sha256 () - : done_ (false) + : done_ (false), empty_ (true) { SHA256_Init (reinterpret_cast (buf_)); } @@ -75,7 +75,13 @@ namespace butl void sha256:: append (const void* b, size_t n) { - SHA256_Update (reinterpret_cast (buf_), b, n); + if (n != 0) + { + SHA256_Update (reinterpret_cast (buf_), b, n); + + if (empty_) + empty_ = false; + } } void sha256:: diff --git a/libbutl/sha256.mxx b/libbutl/sha256.mxx index fca8b91..2bb58b3 100644 --- a/libbutl/sha256.mxx +++ b/libbutl/sha256.mxx @@ -108,6 +108,11 @@ LIBBUTL_MODEXPORT namespace butl explicit sha256 (ifdstream& i): sha256 () {append (i);} + // Check if any data has been hashed. + // + bool + empty () const {return empty_;} + // Extract result. // // It can be obtained as either a 32-byte binary digest or as a 64- @@ -143,6 +148,7 @@ LIBBUTL_MODEXPORT namespace butl mutable digest_type bin_; mutable bool done_; + bool empty_; }; // Convert a SHA256 string representation (64 hex digits) to the fingerprint -- cgit v1.1