xref: /aosp_15_r20/external/cronet/crypto/secure_hash.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 "crypto/crypto_export.h"
13 
14 namespace crypto {
15 
16 // A wrapper to calculate secure hashes incrementally, allowing to
17 // be used when the full input is not known in advance. The end result will the
18 // same as if we have the full input in advance.
19 class CRYPTO_EXPORT SecureHash {
20  public:
21   enum Algorithm {
22     SHA256,
23     SHA512,
24   };
25 
26   SecureHash(const SecureHash&) = delete;
27   SecureHash& operator=(const SecureHash&) = delete;
28 
~SecureHash()29   virtual ~SecureHash() {}
30 
31   static std::unique_ptr<SecureHash> Create(Algorithm type);
32 
33   virtual void Update(const void* input, size_t len) = 0;
34   virtual void Finish(void* output, size_t len) = 0;
35   virtual size_t GetHashLength() const = 0;
36 
37   // Create a clone of this SecureHash. The returned clone and this both
38   // represent the same hash state. But from this point on, calling
39   // Update()/Finish() on either doesn't affect the state of the other.
40   virtual std::unique_ptr<SecureHash> Clone() const = 0;
41 
42  protected:
SecureHash()43   SecureHash() {}
44 };
45 
46 }  // namespace crypto
47 
48 #endif  // CRYPTO_SECURE_HASH_H_
49