diff options
Diffstat (limited to 'include/llvm/Support/MD5.h')
-rw-r--r-- | include/llvm/Support/MD5.h | 59 |
1 files changed, 47 insertions, 12 deletions
diff --git a/include/llvm/Support/MD5.h b/include/llvm/Support/MD5.h index eb181bfe8a5c..2c0dc76485f8 100644 --- a/include/llvm/Support/MD5.h +++ b/include/llvm/Support/MD5.h @@ -1,4 +1,4 @@ -/* +/* -*- C++ -*- * This code is derived from (original license follows): * * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. @@ -29,24 +29,55 @@ #define LLVM_SUPPORT_MD5_H #include "llvm/ADT/SmallString.h" -#include "llvm/Support/DataTypes.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Support/Endian.h" #include <array> +#include <cstdint> namespace llvm { + template <typename T> class ArrayRef; class MD5 { // Any 32-bit or wider unsigned integer data type will do. typedef uint32_t MD5_u32plus; - MD5_u32plus a, b, c, d; - MD5_u32plus hi, lo; + MD5_u32plus a = 0x67452301; + MD5_u32plus b = 0xefcdab89; + MD5_u32plus c = 0x98badcfe; + MD5_u32plus d = 0x10325476; + MD5_u32plus hi = 0; + MD5_u32plus lo = 0; uint8_t buffer[64]; MD5_u32plus block[16]; public: - typedef uint8_t MD5Result[16]; + struct MD5Result { + std::array<uint8_t, 16> Bytes; + + operator std::array<uint8_t, 16>() const { return Bytes; } + + const uint8_t &operator[](size_t I) const { return Bytes[I]; } + uint8_t &operator[](size_t I) { return Bytes[I]; } + + SmallString<32> digest() const; + + uint64_t low() const { + // Our MD5 implementation returns the result in little endian, so the low + // word is first. + using namespace support; + return endian::read<uint64_t, little, unaligned>(Bytes.data()); + } + + uint64_t high() const { + using namespace support; + return endian::read<uint64_t, little, unaligned>(Bytes.data() + 8); + } + std::pair<uint64_t, uint64_t> words() const { + using namespace support; + return std::make_pair(high(), low()); + } + }; MD5(); @@ -70,18 +101,22 @@ private: const uint8_t *body(ArrayRef<uint8_t> Data); }; +inline bool operator==(const MD5::MD5Result &LHS, const MD5::MD5Result &RHS) { + return LHS.Bytes == RHS.Bytes; +} + /// Helper to compute and return lower 64 bits of the given string's MD5 hash. inline uint64_t MD5Hash(StringRef Str) { + using namespace support; + MD5 Hash; Hash.update(Str); - llvm::MD5::MD5Result Result; + MD5::MD5Result Result; Hash.final(Result); - // Return the least significant 8 bytes. Our MD5 implementation returns the - // result in little endian, so we may need to swap bytes. - using namespace llvm::support; - return endian::read<uint64_t, little, unaligned>(Result); + // Return the least significant word. + return Result.low(); } -} +} // end namespace llvm -#endif +#endif // LLVM_SUPPORT_MD5_H |