diff --git a/src/md5/md5.c b/src/md5/md5.c index c35d96c5ef..bc8e872b4c 100644 --- a/src/md5/md5.c +++ b/src/md5/md5.c @@ -320,10 +320,10 @@ md5_init(md5_state_t *pms) } void -md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes) +md5_append(md5_state_t *pms, const md5_byte_t *data, size_t nbytes) { const md5_byte_t *p = data; - int left = nbytes; + size_t left = nbytes; int offset = (pms->count[0] >> 3) & 63; md5_word_t nbits = (md5_word_t)(nbytes << 3); diff --git a/src/md5/md5.h b/src/md5/md5.h index 698c995d8f..d76ddcf2cf 100644 --- a/src/md5/md5.h +++ b/src/md5/md5.h @@ -79,7 +79,7 @@ extern "C" void md5_init(md5_state_t *pms); /* Append a string to the message. */ -void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes); +void md5_append(md5_state_t *pms, const md5_byte_t *data, size_t nbytes); /* Finish the message and return the digest. */ void md5_finish(md5_state_t *pms, md5_byte_t digest[16]); diff --git a/src/md5/md5.hpp b/src/md5/md5.hpp index fa7e2b017c..c05997a7a2 100644 --- a/src/md5/md5.hpp +++ b/src/md5/md5.hpp @@ -29,6 +29,9 @@ #define WEBSOCKETPP_MD5_WRAPPER_HPP #include "md5.h" +#include + +#include namespace websocketpp { @@ -39,13 +42,27 @@ inline std::string md5_hash_string(const std::string& s) { md5_state_t state; md5_init(&state); - md5_append(&state, (const md5_byte_t *)s.c_str(), 16); + md5_append(&state, (const md5_byte_t *)s.c_str(), s.size()); md5_finish(&state, (md5_byte_t *)digest); digest[16] = '\0'; return std::string(digest); } +const char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + +inline std::string md5_hash_hex(const std::string& input) { + std::string hash = md5_hash_string(input); + std::string hex; + + for (size_t i = 0; i < hash.size(); i++) { + hex.push_back(hexval[((hash[i] >> 4) & 0xF)]); + hex.push_back(hexval[(hash[i]) & 0x0F]); + } + + return hex; +} + } // websocketpp #endif // WEBSOCKETPP_MD5_WRAPPER_HPP \ No newline at end of file