xref: /aosp_15_r20/external/cronet/net/tools/transport_security_state_generator/spki_hash.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 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 NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_SPKI_HASH_H_
6 #define NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_SPKI_HASH_H_
7 
8 #include <stdint.h>
9 
10 #include <string_view>
11 
12 namespace net::transport_security_state {
13 
14 class SPKIHash {
15  public:
16   enum : size_t { kLength = 32 };
17 
18   SPKIHash();
19   ~SPKIHash();
20 
21   // Initalizes a hash from the form sha256/<base64-hash-value>. The preloaded
22   // SPKI hashes are SHA256. Other algorithms are not supported. Returns true
23   // on success and copies the decoded bytes to |data_|. Returns false on
24   // failure.
25   bool FromString(std::string_view hash_string);
26 
27   // Calculates the SHA256 digest over |*input| and copies the result to
28   // |data_|.
29   void CalculateFromBytes(const uint8_t* input, size_t input_length);
30 
31   // Returns the size of the hash in bytes. Harcoded to 32 which is the length
32   // of a SHA256 hash.
size()33   size_t size() const { return kLength; }
34 
data()35   uint8_t* data() { return data_; }
data()36   const uint8_t* data() const { return data_; }
37 
38  private:
39   // The bytes of the hash. Current hashes are SHA256 and thus 32 bytes long.
40   uint8_t data_[kLength];
41 };
42 
43 }  // namespace net::transport_security_state
44 
45 #endif  // NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_SPKI_HASH_H_
46