diff options
| author | skallweitNV <64953474+skallweitNV@users.noreply.github.com> | 2022-11-30 22:26:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-30 13:26:55 -0800 |
| commit | 976f578585a4d4ed24e37d0c45a94a8e6afcff19 (patch) | |
| tree | 60897092962bfd562d026b81a29096efbfc85816 /source/core | |
| parent | 09684224d5ab63f530d66c0be65fa50e6fc5290b (diff) | |
Cleanup DigestBuilder and MD5HashGen (#2539)
* Cleanup DigestBuilder and MD5HashGen
* Fix templates
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-digest-builder.h | 88 | ||||
| -rw-r--r-- | source/core/slang-digest-util.cpp | 6 | ||||
| -rw-r--r-- | source/core/slang-md5.cpp | 94 | ||||
| -rw-r--r-- | source/core/slang-md5.h | 27 |
4 files changed, 128 insertions, 87 deletions
diff --git a/source/core/slang-digest-builder.h b/source/core/slang-digest-builder.h index 47c0cd8f7..c613b2239 100644 --- a/source/core/slang-digest-builder.h +++ b/source/core/slang-digest-builder.h @@ -1,6 +1,8 @@ #pragma once #include "slang-md5.h" #include "../../slang.h" +#include "../core/slang-string.h" +#include "../core/slang-list.h" namespace Slang { @@ -15,10 +17,90 @@ namespace Slang hashGen.init(&context); } - template <typename T> - void addToDigest(T item) + template<typename T, typename std::enable_if<std::is_arithmetic<T>::value || std::is_enum<T>::value, int>::type = 0> + DigestBuilder& operator<<(const T value) { - hashGen.update(&context, item); + append(value); + return *this; + } + + DigestBuilder& operator<<(const String& str) + { + append(str); + return *this; + } + + DigestBuilder& operator<<(const StringSlice& str) + { + append(str); + return *this; + } + + DigestBuilder& operator<<(const UnownedStringSlice& str) + { + append(str); + return *this; + } + + DigestBuilder& operator<<(ISlangBlob* blob) + { + append(blob); + return *this; + } + + DigestBuilder& operator<<(const slang::Digest& digest) + { + append(digest); + return *this; + } + + template<typename T, typename std::enable_if<std::is_pod<T>::value, int>::type = 0> + DigestBuilder& operator<<(const List<T>& list) + { + append(list); + return *this; + } + + void append(const void* data, SlangInt size) + { + hashGen.update(&context, data, size); + } + + template<typename T, typename std::enable_if<std::is_arithmetic<T>::value || std::is_enum<T>::value, int>::type = 0> + void append(const T value) + { + append(&value, sizeof(T)); + } + + void append(const String& str) + { + append(str.getBuffer(), str.getLength()); + } + + void append(const StringSlice& str) + { + append(str.begin(), str.getLength()); + } + + void append(const UnownedStringSlice& str) + { + append(str.begin(), str.getLength()); + } + + void append(ISlangBlob* blob) + { + append(blob->getBufferPointer(), blob->getBufferSize()); + } + + void append(const slang::Digest& digest) + { + append(&digest, sizeof(digest)); + } + + template<typename T, typename std::enable_if<std::is_pod<T>::value, int>::type = 0> + void append(const List<T>& list) + { + append(list.getBuffer(), list.getCount() * sizeof(T)); } Digest finalize() diff --git a/source/core/slang-digest-util.cpp b/source/core/slang-digest-util.cpp index d235f2cf0..8252dce65 100644 --- a/source/core/slang-digest-util.cpp +++ b/source/core/slang-digest-util.cpp @@ -12,15 +12,15 @@ namespace Slang /*static*/ Digest DigestUtil::computeDigestForStringSlice(UnownedStringSlice text) { DigestBuilder builder; - builder.addToDigest(text); + builder.append(text); return builder.finalize(); } /*static*/ Digest DigestUtil::combine(const Digest& digestA, const Digest& digestB) { DigestBuilder builder; - builder.addToDigest(digestA); - builder.addToDigest(digestB); + builder.append(digestA); + builder.append(digestB); return builder.finalize(); } diff --git a/source/core/slang-md5.cpp b/source/core/slang-md5.cpp index 7371b21d2..049ebae95 100644 --- a/source/core/slang-md5.cpp +++ b/source/core/slang-md5.cpp @@ -34,11 +34,11 @@ * optimizations are not included to reduce source code size and avoid * compile-time configuration. */ - + #ifndef HAVE_OPENSSL - + #include <string.h> - + #include "slang-md5.h" namespace Slang @@ -55,7 +55,7 @@ namespace Slang #define H(x, y, z) (((x) ^ (y)) ^ (z)) #define H2(x, y, z) ((x) ^ ((y) ^ (z))) #define I(x, y, z) ((y) ^ ((x) | ~(z))) - + /* * The MD5 transformation for all four rounds. */ @@ -63,7 +63,7 @@ namespace Slang (a) += f((b), (c), (d)) + (x) + (t); \ (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ (a) += (b); - + /* * SET reads 4 input bytes in little-endian byte order and stores them in a * properly aligned word in host byte order. @@ -94,7 +94,7 @@ namespace Slang #define GET(n) \ (ctx->block[(n)]) #endif - + /* * This processes one or more 64-byte data blocks, but does NOT update the bit * counters. There are no alignment requirements. @@ -104,20 +104,20 @@ namespace Slang const unsigned char* ptr; MD5_u32plus a, b, c, d; MD5_u32plus saved_a, saved_b, saved_c, saved_d; - + ptr = (const unsigned char*)data; - + a = ctx->a; b = ctx->b; c = ctx->c; d = ctx->d; - + do { saved_a = a; saved_b = b; saved_c = c; saved_d = d; - + /* Round 1 */ STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) @@ -135,7 +135,7 @@ namespace Slang STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) - + /* Round 2 */ STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) @@ -153,7 +153,7 @@ namespace Slang STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) - + /* Round 3 */ STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11) @@ -171,7 +171,7 @@ namespace Slang STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11) STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23) - + /* Round 4 */ STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) @@ -189,88 +189,68 @@ namespace Slang STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) - + a += saved_a; b += saved_b; c += saved_c; d += saved_d; - + ptr += 64; } while (size -= 64); - + ctx->a = a; ctx->b = b; ctx->c = c; ctx->d = d; - + return ptr; } - + void MD5HashGen::init(MD5Context* ctx) { ctx->a = 0x67452301; ctx->b = 0xefcdab89; ctx->c = 0x98badcfe; ctx->d = 0x10325476; - + ctx->lo = 0; ctx->hi = 0; } - void MD5HashGen::update(MD5Context* ctx, UnownedStringSlice string) - { - update(ctx, string.begin(), string.getLength()); - } - - void MD5HashGen::update(MD5Context* ctx, String str) - { - update(ctx, str.getBuffer(), str.getLength()); - } - - void MD5HashGen::update(MD5Context* ctx, const slang::Digest& hash) - { - update(ctx, hash.values, sizeof(hash.values)); - } - - void MD5HashGen::update(MD5Context* ctx, ISlangBlob* blob) - { - update(ctx, blob->getBufferPointer(), blob->getBufferSize()); - } - void MD5HashGen::update(MD5Context* ctx, const void* data, SlangInt size) { MD5_u32plus saved_lo; SlangInt used, available; - + saved_lo = ctx->lo; if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) ctx->hi++; ctx->hi += (MD5_u32plus)size >> 29; - + used = saved_lo & 0x3f; - + if (used) { available = 64 - used; - + if (size < available) { memcpy(&ctx->buffer[used], data, size); return; } - + memcpy(&ctx->buffer[used], data, available); data = (const unsigned char*)data + available; size -= available; body(ctx, ctx->buffer, 64); } - + if (size >= 64) { data = body(ctx, data, size & ~(SlangInt)0x3f); size &= 0x3f; } - + memcpy(ctx->buffer, data, size); } - + #define OUTUINT(dst, src) \ (dst)[0] = (uint32_t)src; \ @@ -279,39 +259,39 @@ namespace Slang (dst)[1] = (unsigned char)((src) >> 8); \ (dst)[2] = (unsigned char)((src) >> 16); \ (dst)[3] = (unsigned char)((src) >> 24); - + void MD5HashGen::finalize(MD5Context* ctx, slang::Digest* result) { unsigned long used, available; - + used = ctx->lo & 0x3f; - + ctx->buffer[used++] = 0x80; - + available = 64 - used; - + if (available < 8) { memset(&ctx->buffer[used], 0, available); body(ctx, ctx->buffer, 64); used = 0; available = 64; } - + memset(&ctx->buffer[used], 0, available - 8); - + ctx->lo <<= 3; OUTCHAR(&ctx->buffer[56], ctx->lo) OUTCHAR(&ctx->buffer[60], ctx->hi) - + body(ctx, ctx->buffer, 64); OUTUINT(&result->values[0], ctx->a) OUTUINT(&result->values[1], ctx->b) OUTUINT(&result->values[2], ctx->c) OUTUINT(&result->values[3], ctx->d) - + memset(ctx, 0, sizeof(*ctx)); } } - + #endif diff --git a/source/core/slang-md5.h b/source/core/slang-md5.h index 2e99fb667..d40102b69 100644 --- a/source/core/slang-md5.h +++ b/source/core/slang-md5.h @@ -22,7 +22,7 @@ * * See md5.c for more information. */ - + #ifdef HAVE_OPENSSL #include <openssl/md5.h> #elif !defined(_MD5_H) @@ -51,34 +51,13 @@ namespace Slang public: void init(MD5Context* ctx); - // Helper update function for raw values (e.g. ints, uints) - template<typename T, - typename = typename std::enable_if<std::is_enum<T>::value || std::is_arithmetic<T>::value>::type> - void update(MD5Context* ctx, const T& val) - { - update(ctx, &val, sizeof(T)); - } - // Helper update function for Slang::List - template<typename T> - void update(MD5Context* ctx, const List<T>& list) - { - update(ctx, list.getBuffer(), list.getCount()); - } - // Helper update function for UnownedStringSlice - void update(MD5Context* ctx, UnownedStringSlice string); - // Helper update function for Slang::String - void update(MD5Context* ctx, String str); - // Helper update function for Checksums - void update(MD5Context* ctx, const slang::Digest& checksum); - // Helper update function for ISlangBlob - void update(MD5Context* ctx, ISlangBlob* blob); + void update(MD5Context* ctx, const void* data, SlangInt size); void finalize(MD5Context* ctx, slang::Digest* result); private: static const void* body(MD5Context* ctx, const void* data, SlangInt size); - void update(MD5Context* ctx, const void* data, SlangInt size); }; } - + #endif |
