HashFunction.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/ByteBuffer.h>
  8. #include <AK/StringView.h>
  9. #include <AK/Types.h>
  10. namespace Crypto {
  11. namespace Hash {
  12. template<size_t BlockS, typename DigestT>
  13. class HashFunction {
  14. public:
  15. static constexpr auto BlockSize = BlockS / 8;
  16. static constexpr auto DigestSize = DigestT::Size;
  17. using DigestType = DigestT;
  18. static size_t block_size() { return BlockSize; };
  19. static size_t digest_size() { return DigestSize; };
  20. virtual void update(const u8*, size_t) = 0;
  21. void update(const Bytes& buffer) { update(buffer.data(), buffer.size()); };
  22. void update(const ReadonlyBytes& buffer) { update(buffer.data(), buffer.size()); };
  23. void update(const ByteBuffer& buffer) { update(buffer.data(), buffer.size()); };
  24. void update(const StringView& string) { update((const u8*)string.characters_without_null_termination(), string.length()); };
  25. virtual DigestType peek() = 0;
  26. virtual DigestType digest() = 0;
  27. virtual void reset() = 0;
  28. virtual String class_name() const = 0;
  29. protected:
  30. virtual ~HashFunction() = default;
  31. };
  32. }
  33. }