HashManager.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/Optional.h>
  8. #include <AK/OwnPtr.h>
  9. #include <AK/Variant.h>
  10. #include <LibCrypto/Hash/HashFunction.h>
  11. #include <LibCrypto/Hash/MD5.h>
  12. #include <LibCrypto/Hash/SHA1.h>
  13. #include <LibCrypto/Hash/SHA2.h>
  14. namespace Crypto {
  15. namespace Hash {
  16. enum class HashKind {
  17. None,
  18. SHA1,
  19. SHA256,
  20. SHA384,
  21. SHA512,
  22. MD5,
  23. };
  24. struct MultiHashDigestVariant {
  25. constexpr static size_t Size = 0;
  26. MultiHashDigestVariant(Empty digest)
  27. : m_digest(move(digest))
  28. {
  29. }
  30. MultiHashDigestVariant(MD5::DigestType digest)
  31. : m_digest(move(digest))
  32. {
  33. }
  34. MultiHashDigestVariant(SHA1::DigestType digest)
  35. : m_digest(move(digest))
  36. {
  37. }
  38. MultiHashDigestVariant(SHA256::DigestType digest)
  39. : m_digest(move(digest))
  40. {
  41. }
  42. MultiHashDigestVariant(SHA384::DigestType digest)
  43. : m_digest(move(digest))
  44. {
  45. }
  46. MultiHashDigestVariant(SHA512::DigestType digest)
  47. : m_digest(move(digest))
  48. {
  49. }
  50. [[nodiscard]] const u8* immutable_data() const
  51. {
  52. return m_digest.visit(
  53. [&](const Empty&) -> const u8* { VERIFY_NOT_REACHED(); },
  54. [&](const auto& value) { return value.immutable_data(); });
  55. }
  56. [[nodiscard]] size_t data_length() const
  57. {
  58. return m_digest.visit(
  59. [&](const Empty&) -> size_t { VERIFY_NOT_REACHED(); },
  60. [&](const auto& value) { return value.data_length(); });
  61. }
  62. using DigestVariant = Variant<Empty, MD5::DigestType, SHA1::DigestType, SHA256::DigestType, SHA384::DigestType, SHA512::DigestType>;
  63. DigestVariant m_digest {};
  64. };
  65. class Manager final : public HashFunction<0, 0, MultiHashDigestVariant> {
  66. public:
  67. using HashFunction::update;
  68. Manager()
  69. {
  70. m_pre_init_buffer = ByteBuffer();
  71. }
  72. Manager(const Manager& other) // NOT a copy constructor!
  73. {
  74. m_pre_init_buffer = ByteBuffer(); // will not be used
  75. initialize(other.m_kind);
  76. }
  77. Manager(HashKind kind)
  78. {
  79. m_pre_init_buffer = ByteBuffer();
  80. initialize(kind);
  81. }
  82. ~Manager()
  83. {
  84. m_algorithm = Empty {};
  85. }
  86. inline size_t digest_size() const
  87. {
  88. return m_algorithm.visit(
  89. [&](const Empty&) -> size_t { return 0; },
  90. [&](const auto& hash) { return hash.digest_size(); });
  91. }
  92. inline size_t block_size() const
  93. {
  94. return m_algorithm.visit(
  95. [&](const Empty&) -> size_t { return 0; },
  96. [&](const auto& hash) { return hash.block_size(); });
  97. }
  98. inline void initialize(HashKind kind)
  99. {
  100. if (!m_algorithm.has<Empty>()) {
  101. VERIFY_NOT_REACHED();
  102. }
  103. m_kind = kind;
  104. switch (kind) {
  105. case HashKind::MD5:
  106. m_algorithm = MD5();
  107. break;
  108. case HashKind::SHA1:
  109. m_algorithm = SHA1();
  110. break;
  111. case HashKind::SHA256:
  112. m_algorithm = SHA256();
  113. break;
  114. case HashKind::SHA384:
  115. m_algorithm = SHA384();
  116. break;
  117. case HashKind::SHA512:
  118. m_algorithm = SHA512();
  119. break;
  120. default:
  121. case HashKind::None:
  122. m_algorithm = Empty {};
  123. break;
  124. }
  125. }
  126. virtual void update(const u8* data, size_t length) override
  127. {
  128. auto size = m_pre_init_buffer.size();
  129. if (size) {
  130. m_algorithm.visit(
  131. [&](Empty&) {},
  132. [&](auto& hash) { hash.update(m_pre_init_buffer); });
  133. }
  134. m_algorithm.visit(
  135. [&](Empty&) { m_pre_init_buffer.append(data, length); },
  136. [&](auto& hash) { hash.update(data, length); });
  137. if (size && m_kind != HashKind::None)
  138. m_pre_init_buffer.clear();
  139. }
  140. virtual DigestType peek() override
  141. {
  142. return m_algorithm.visit(
  143. [&](Empty&) -> DigestType { VERIFY_NOT_REACHED(); },
  144. [&](auto& hash) -> DigestType { return hash.peek(); });
  145. }
  146. virtual DigestType digest() override
  147. {
  148. auto digest = peek();
  149. reset();
  150. return digest;
  151. }
  152. virtual void reset() override
  153. {
  154. m_pre_init_buffer.clear();
  155. m_algorithm.visit(
  156. [&](Empty&) {},
  157. [&](auto& hash) { hash.reset(); });
  158. }
  159. virtual String class_name() const override
  160. {
  161. return m_algorithm.visit(
  162. [&](const Empty&) -> String { return "UninitializedHashManager"; },
  163. [&](const auto& hash) { return hash.class_name(); });
  164. }
  165. inline bool is(HashKind kind) const
  166. {
  167. return m_kind == kind;
  168. }
  169. private:
  170. using AlgorithmVariant = Variant<Empty, MD5, SHA1, SHA256, SHA384, SHA512>;
  171. AlgorithmVariant m_algorithm {};
  172. HashKind m_kind { HashKind::None };
  173. ByteBuffer m_pre_init_buffer;
  174. };
  175. }
  176. }