• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CRYPTO_SECURE_HASH_H_
6 #define CRYPTO_SECURE_HASH_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 
12 #include "base/containers/span.h"
13 #include "crypto/crypto_export.h"
14 
15 namespace crypto {
16 
17 // A wrapper to calculate secure hashes incrementally, allowing to
18 // be used when the full input is not known in advance. The end result will the
19 // same as if we have the full input in advance.
20 //
21 // TODO(https://issues.chromium.org/issues/374310081): Move this into
22 // crypto/hash.h along with the oneshot functions.
23 class CRYPTO_EXPORT SecureHash {
24  public:
25   enum Algorithm {
26     SHA256,
27     SHA512,
28   };
29 
30   SecureHash(const SecureHash&) = delete;
31   SecureHash& operator=(const SecureHash&) = delete;
32 
~SecureHash()33   virtual ~SecureHash() {}
34 
35   static std::unique_ptr<SecureHash> Create(Algorithm type);
36 
37   virtual void Update(base::span<const uint8_t> input) = 0;
38   virtual void Finish(base::span<uint8_t> output) = 0;
39 
40   virtual size_t GetHashLength() const = 0;
41 
42   // Deprecated non-span APIs - do not add new uses of them, and please remove
43   // existing uses.
44   // TODO(https://crbug.com/364687923): Delete these.
45   void Update(const void* input, size_t len);
46   void Finish(void* output, size_t len);
47 
48   // Create a clone of this SecureHash. The returned clone and this both
49   // represent the same hash state. But from this point on, calling
50   // Update()/Finish() on either doesn't affect the state of the other.
51   virtual std::unique_ptr<SecureHash> Clone() const = 0;
52 
53  protected:
SecureHash()54   SecureHash() {}
55 };
56 
57 }  // namespace crypto
58 
59 #endif  // CRYPTO_SECURE_HASH_H_
60