secure_hash.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CRYPTO_SECURE_HASH_H_
  5. #define CRYPTO_SECURE_HASH_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include "crypto/crypto_export.h"
  9. namespace crypto {
  10. // A wrapper to calculate secure hashes incrementally, allowing to
  11. // be used when the full input is not known in advance. The end result will the
  12. // same as if we have the full input in advance.
  13. class CRYPTO_EXPORT SecureHash {
  14. public:
  15. enum Algorithm {
  16. SHA256,
  17. SHA512,
  18. };
  19. SecureHash(const SecureHash&) = delete;
  20. SecureHash& operator=(const SecureHash&) = delete;
  21. virtual ~SecureHash() {}
  22. static std::unique_ptr<SecureHash> Create(Algorithm type);
  23. virtual void Update(const void* input, size_t len) = 0;
  24. virtual void Finish(void* output, size_t len) = 0;
  25. virtual size_t GetHashLength() const = 0;
  26. // Create a clone of this SecureHash. The returned clone and this both
  27. // represent the same hash state. But from this point on, calling
  28. // Update()/Finish() on either doesn't affect the state of the other.
  29. virtual std::unique_ptr<SecureHash> Clone() const = 0;
  30. protected:
  31. SecureHash() {}
  32. };
  33. } // namespace crypto
  34. #endif // CRYPTO_SECURE_HASH_H_