MD5.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/Types.h>
  9. #include <LibCrypto/Hash/HashFunction.h>
  10. namespace Crypto {
  11. namespace Hash {
  12. struct MD5Digest {
  13. constexpr static size_t Size = 16;
  14. u8 data[Size];
  15. const u8* immutable_data() const { return data; }
  16. size_t data_length() const { return Size; }
  17. };
  18. namespace MD5Constants {
  19. constexpr u32 init_A = 0x67452301;
  20. constexpr u32 init_B = 0xefcdab89;
  21. constexpr u32 init_C = 0x98badcfe;
  22. constexpr u32 init_D = 0x10325476;
  23. constexpr u32 S11 = 7;
  24. constexpr u32 S12 = 12;
  25. constexpr u32 S13 = 17;
  26. constexpr u32 S14 = 22;
  27. constexpr u32 S21 = 5;
  28. constexpr u32 S22 = 9;
  29. constexpr u32 S23 = 14;
  30. constexpr u32 S24 = 20;
  31. constexpr u32 S31 = 4;
  32. constexpr u32 S32 = 11;
  33. constexpr u32 S33 = 16;
  34. constexpr u32 S34 = 23;
  35. constexpr u32 S41 = 6;
  36. constexpr u32 S42 = 10;
  37. constexpr u32 S43 = 15;
  38. constexpr u32 S44 = 21;
  39. constexpr u8 PADDING[] = {
  40. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0
  45. };
  46. }
  47. class MD5 final : public HashFunction<512, MD5Digest> {
  48. public:
  49. using HashFunction::update;
  50. virtual void update(const u8*, size_t) override;
  51. virtual DigestType digest() override;
  52. virtual DigestType peek() override;
  53. virtual String class_name() const override { return "MD5"; }
  54. inline static DigestType hash(const u8* data, size_t length)
  55. {
  56. MD5 md5;
  57. md5.update(data, length);
  58. return md5.digest();
  59. }
  60. inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
  61. inline static DigestType hash(const StringView& buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
  62. inline virtual void reset() override
  63. {
  64. m_A = MD5Constants::init_A;
  65. m_B = MD5Constants::init_B;
  66. m_C = MD5Constants::init_C;
  67. m_D = MD5Constants::init_D;
  68. m_count[0] = 0;
  69. m_count[1] = 0;
  70. __builtin_memset(m_data_buffer, 0, sizeof(m_data_buffer));
  71. }
  72. private:
  73. inline void transform(const u8*);
  74. static void encode(const u32* from, u8* to, size_t length);
  75. static void decode(const u8* from, u32* to, size_t length);
  76. u32 m_A { MD5Constants::init_A }, m_B { MD5Constants::init_B }, m_C { MD5Constants::init_C }, m_D { MD5Constants::init_D };
  77. u32 m_count[2] { 0, 0 };
  78. u8 m_data_buffer[64] {};
  79. };
  80. }
  81. }