MD5.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. /* -*- C++ -*-
  7. * This code is derived from (original license follows):
  8. *
  9. * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
  10. * MD5 Message-Digest Algorithm (RFC 1321).
  11. *
  12. * Homepage:
  13. * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
  14. *
  15. * Author:
  16. * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
  17. *
  18. * This software was written by Alexander Peslyak in 2001. No copyright is
  19. * claimed, and the software is hereby placed in the public domain.
  20. * In case this attempt to disclaim copyright and place the software in the
  21. * public domain is deemed null and void, then the software is
  22. * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
  23. * general public under the following terms:
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. * modification, are permitted.
  27. *
  28. * There's ABSOLUTELY NO WARRANTY, express or implied.
  29. *
  30. * See md5.c for more information.
  31. */
  32. #ifndef LLVM_SUPPORT_MD5_H
  33. #define LLVM_SUPPORT_MD5_H
  34. #include "llvm/ADT/StringRef.h"
  35. #include "llvm/Support/Endian.h"
  36. #include <array>
  37. #include <cstdint>
  38. namespace llvm {
  39. template <unsigned N> class SmallString;
  40. template <typename T> class ArrayRef;
  41. class MD5 {
  42. public:
  43. struct MD5Result {
  44. std::array<uint8_t, 16> Bytes;
  45. operator std::array<uint8_t, 16>() const { return Bytes; }
  46. const uint8_t &operator[](size_t I) const { return Bytes[I]; }
  47. uint8_t &operator[](size_t I) { return Bytes[I]; }
  48. SmallString<32> digest() const;
  49. uint64_t low() const {
  50. // Our MD5 implementation returns the result in little endian, so the low
  51. // word is first.
  52. using namespace support;
  53. return endian::read<uint64_t, little, unaligned>(Bytes.data());
  54. }
  55. uint64_t high() const {
  56. using namespace support;
  57. return endian::read<uint64_t, little, unaligned>(Bytes.data() + 8);
  58. }
  59. std::pair<uint64_t, uint64_t> words() const {
  60. using namespace support;
  61. return std::make_pair(high(), low());
  62. }
  63. };
  64. MD5();
  65. /// Updates the hash for the byte stream provided.
  66. void update(ArrayRef<uint8_t> Data);
  67. /// Updates the hash for the StringRef provided.
  68. void update(StringRef Str);
  69. /// Finishes off the hash and puts the result in result.
  70. void final(MD5Result &Result);
  71. /// Finishes off the hash, and returns a reference to the 16-byte hash data.
  72. StringRef final();
  73. /// Finishes off the hash, and returns a reference to the 16-byte hash data.
  74. /// This is suitable for getting the MD5 at any time without invalidating the
  75. /// internal state, so that more calls can be made into `update`.
  76. StringRef result();
  77. /// Translates the bytes in \p Res to a hex string that is
  78. /// deposited into \p Str. The result will be of length 32.
  79. static void stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str);
  80. /// Computes the hash for a given bytes.
  81. static std::array<uint8_t, 16> hash(ArrayRef<uint8_t> Data);
  82. private:
  83. // Any 32-bit or wider unsigned integer data type will do.
  84. typedef uint32_t MD5_u32plus;
  85. // Internal State
  86. struct {
  87. MD5_u32plus a = 0x67452301;
  88. MD5_u32plus b = 0xefcdab89;
  89. MD5_u32plus c = 0x98badcfe;
  90. MD5_u32plus d = 0x10325476;
  91. MD5_u32plus hi = 0;
  92. MD5_u32plus lo = 0;
  93. uint8_t buffer[64];
  94. MD5_u32plus block[16];
  95. } InternalState;
  96. MD5Result Result;
  97. const uint8_t *body(ArrayRef<uint8_t> Data);
  98. };
  99. inline bool operator==(const MD5::MD5Result &LHS, const MD5::MD5Result &RHS) {
  100. return LHS.Bytes == RHS.Bytes;
  101. }
  102. /// Helper to compute and return lower 64 bits of the given string's MD5 hash.
  103. inline uint64_t MD5Hash(StringRef Str) {
  104. using namespace support;
  105. MD5 Hash;
  106. Hash.update(Str);
  107. MD5::MD5Result Result;
  108. Hash.final(Result);
  109. // Return the least significant word.
  110. return Result.low();
  111. }
  112. } // end namespace llvm
  113. #endif // LLVM_SUPPORT_MD5_H
  114. #ifdef __GNUC__
  115. #pragma GCC diagnostic pop
  116. #endif