aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2016-02-29 08:10:16 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2016-02-29 08:10:16 +0200
commit104de2e0872d37cf4291d92aa9bee191a01f9c15 (patch)
tree106dd25f11444c070995799b4950357175658a9a
parentd928de165f8bb896ee77f5668f35611f57429c93 (diff)
Add '\0' string terminator to sha256 calculation
Failed that, an empty string will be indistinguishable from no string.
-rw-r--r--butl/sha25632
-rw-r--r--tests/sha256/driver.cxx10
2 files changed, 28 insertions, 14 deletions
diff --git a/butl/sha256 b/butl/sha256
index 3fc9e63..d583701 100644
--- a/butl/sha256
+++ b/butl/sha256
@@ -23,27 +23,35 @@ namespace butl
{
public:
sha256 ();
- explicit sha256 (const std::string& s): sha256 () {append (s);}
- explicit sha256 (const char* s): sha256 () {append (s);}
- sha256 (const void* b, std::size_t n): sha256 () {append (b, n);}
- // Append string (without the traling '\0').
+ // Append binary data.
//
void
- append (const std::string& s) {append (s.c_str (), s.size ());}
+ append (const void*, std::size_t);
- // Append C-string (without the traling '\0').
+ sha256 (const void* b, std::size_t n): sha256 () {append (b, n);}
+
+ // Append string.
+ //
+ // Note that the hash includes the '\0' terminator. Failed that, a call
+ // with an empty string will be indistinguishable from no call at all.
//
void
- append (const char* s) {append (s, std::strlen (s));}
+ append (const std::string& s) {append (s.c_str (), s.size () + 1);}
- // Append binary data.
- //
void
- append (const void*, std::size_t);
+ append (const char* s) {append (s, std::strlen (s) + 1);}
- // Extract result. It can be obtained as either a 32-byte binary digest or
- // as a 64- character hex-encoded C-string.
+ explicit
+ sha256 (const std::string& s): sha256 () {append (s);}
+
+ explicit
+ sha256 (const char* s): sha256 () {append (s);}
+
+ // Extract result.
+ //
+ // It can be obtained as either a 32-byte binary digest or as a 64-
+ // character hex-encoded C-string.
//
using digest_type = std::uint8_t[32];
diff --git a/tests/sha256/driver.cxx b/tests/sha256/driver.cxx
index 12928b2..8c2e525 100644
--- a/tests/sha256/driver.cxx
+++ b/tests/sha256/driver.cxx
@@ -17,7 +17,13 @@ main ()
assert (string (sha256 ().string ()) ==
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+ assert (string (sha256 ("").string ()) !=
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+
assert (string (sha256 ("123").string ()) ==
+ "a787b6772e3e4df1b2a04d5eee56f8570ab38825eed1b6a9bda288429b7f29a1");
+
+ assert (string (sha256 ("123", 3).string ()) ==
"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3");
sha256 h;
@@ -26,9 +32,9 @@ main ()
h.append ("3", 1);
auto& b (h.binary ());
- assert (b[0] == 0xa6 && b[31] == 0xe3);
+ assert (b[0] == 0x20 && b[31] == 0x9d);
string s (h.string ());
assert (s ==
- "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3");
+ "204d9db65789fbede7829ed77f72ba1f0fe21a833d95abad4849b82f33a69b9d");
}