mirror of
https://github.com/XRPLF/rippled.git
synced 2026-04-29 15:37:57 +00:00
md5_hash_string now works with arbitrary length strings
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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]);
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user