diff options
Diffstat (limited to 'Swiften/StringCodecs/MD5.cpp')
-rw-r--r-- | Swiften/StringCodecs/MD5.cpp | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/Swiften/StringCodecs/MD5.cpp b/Swiften/StringCodecs/MD5.cpp index 718f52e..0d36254 100644 --- a/Swiften/StringCodecs/MD5.cpp +++ b/Swiften/StringCodecs/MD5.cpp @@ -33,12 +33,13 @@ #pragma GCC diagnostic ignored "-Wold-style-cast" -#include "Swiften/StringCodecs/MD5.h" +#include <Swiften/StringCodecs/MD5.h> #include <cassert> +#include <string.h> -#include "Swiften/Base/ByteArray.h" -#include "Swiften/Base/Platform.h" +#include <Swiften/Base/ByteArray.h> +#include <Swiften/Base/Platform.h> namespace Swift { @@ -124,13 +125,13 @@ static void md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/) { a = pms->abcd[0], b = pms->abcd[1], c = pms->abcd[2], d = pms->abcd[3]; md5_word_t t; -#ifdef SWIFTEN_BIG_ENDIAN - /* Define storage only for big-endian CPUs. */ - md5_word_t X[16]; -#else +#ifdef SWIFTEN_LITTLE_ENDIAN /* Define storage for little-endian or both types of CPUs. */ md5_word_t xbuf[16]; const md5_word_t *X; +#else + /* Define storage only for big-endian CPUs. */ + md5_word_t X[16]; #endif { @@ -350,16 +351,27 @@ md5_finish(md5_state_t *pms, md5_byte_t digest[16]) digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3)); } -ByteArray MD5::getHash(const ByteArray& data) { - ByteArray digest; - digest.resize(16); +namespace { + template<typename SourceType> + ByteArray getMD5Hash(const SourceType& data) { + ByteArray digest; + digest.resize(16); + + md5_state_t state; + md5_init(&state); + md5_append(&state, reinterpret_cast<const md5_byte_t*>(vecptr(data)), data.size()); + md5_finish(&state, reinterpret_cast<md5_byte_t*>(vecptr(digest))); + + return digest; + } +} - md5_state_t state; - md5_init(&state); - md5_append(&state, reinterpret_cast<const md5_byte_t*>(data.getData()), data.getSize()); - md5_finish(&state, reinterpret_cast<md5_byte_t*>(digest.getData())); +ByteArray MD5::getHash(const ByteArray& data) { + return getMD5Hash(data); +} - return digest; +ByteArray MD5::getHash(const SafeByteArray& data) { + return getMD5Hash(data); } } |