md5_hash_string now works with arbitrary length strings

This commit is contained in:
Peter Thorson
2011-12-14 15:08:03 -06:00
parent 190cf42231
commit 9e14fba2f7
3 changed files with 21 additions and 4 deletions

View File

@@ -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);

View File

@@ -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]);

View File

@@ -29,6 +29,9 @@
#define WEBSOCKETPP_MD5_WRAPPER_HPP
#include "md5.h"
#include <string>
#include <iostream>
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