aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2019-10-05 08:30:00 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2019-10-05 08:30:00 +0200
commit88adf3f7f2e9c6f1ea3c722ead41a5579b3292bf (patch)
tree8329c7f984ea18c6cb35cc1b5ec77f1b39d8656f
parentf6675a4626267c08e53bbc20bd3dd41ac8744605 (diff)
Add sha{1,256}::empty()
-rw-r--r--libbutl/sha1.cxx10
-rw-r--r--libbutl/sha1.mxx6
-rw-r--r--libbutl/sha256.cxx10
-rw-r--r--libbutl/sha256.mxx6
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<SHA1_CTX*> (buf_));
}
@@ -81,7 +81,13 @@ namespace butl
void sha1::
append (const void* b, size_t n)
{
- SHA1_Update (reinterpret_cast<SHA1_CTX*> (buf_), b, n);
+ if (n != 0)
+ {
+ SHA1_Update (reinterpret_cast<SHA1_CTX*> (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<SHA256_CTX*> (buf_));
}
@@ -75,7 +75,13 @@ namespace butl
void sha256::
append (const void* b, size_t n)
{
- SHA256_Update (reinterpret_cast<SHA256_CTX*> (buf_), b, n);
+ if (n != 0)
+ {
+ SHA256_Update (reinterpret_cast<SHA256_CTX*> (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