From 104de2e0872d37cf4291d92aa9bee191a01f9c15 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 29 Feb 2016 08:10:16 +0200 Subject: Add '\0' string terminator to sha256 calculation Failed that, an empty string will be indistinguishable from no string. --- butl/sha256 | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'butl/sha256') 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]; -- cgit v1.1