diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2016-02-29 08:10:16 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2016-02-29 08:10:16 +0200 |
commit | 104de2e0872d37cf4291d92aa9bee191a01f9c15 (patch) | |
tree | 106dd25f11444c070995799b4950357175658a9a /butl/sha256 | |
parent | d928de165f8bb896ee77f5668f35611f57429c93 (diff) |
Add '\0' string terminator to sha256 calculation
Failed that, an empty string will be indistinguishable from no string.
Diffstat (limited to 'butl/sha256')
-rw-r--r-- | butl/sha256 | 32 |
1 files changed, 20 insertions, 12 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]; |