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 | |
5 | #ifndef NET_BASE_HASH_VALUE_H_ |
6 | #define NET_BASE_HASH_VALUE_H_ |
7 | |
8 | #include <stddef.h> |
9 | #include <stdint.h> |
10 | #include <string.h> |
11 | |
12 | #include <string> |
13 | #include <vector> |
14 | |
15 | #include "base/containers/span.h" |
16 | #include "base/strings/string_piece.h" |
17 | #include "build/build_config.h" |
18 | #include "net/base/net_export.h" |
19 | |
20 | namespace net { |
21 | |
22 | struct NET_EXPORT SHA256HashValue { |
23 | unsigned char data[32]; |
24 | }; |
25 | |
26 | inline bool operator==(const SHA256HashValue& lhs, const SHA256HashValue& rhs) { |
27 | return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) == 0; |
28 | } |
29 | |
30 | inline bool operator!=(const SHA256HashValue& lhs, const SHA256HashValue& rhs) { |
31 | return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) != 0; |
32 | } |
33 | |
34 | inline bool operator<(const SHA256HashValue& lhs, const SHA256HashValue& rhs) { |
35 | return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; |
36 | } |
37 | |
38 | inline bool operator>(const SHA256HashValue& lhs, const SHA256HashValue& rhs) { |
39 | return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) > 0; |
40 | } |
41 | |
42 | inline bool operator<=(const SHA256HashValue& lhs, const SHA256HashValue& rhs) { |
43 | return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) <= 0; |
44 | } |
45 | |
46 | inline bool operator>=(const SHA256HashValue& lhs, const SHA256HashValue& rhs) { |
47 | return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) >= 0; |
48 | } |
49 | |
50 | enum HashValueTag { |
51 | HASH_VALUE_SHA256, |
52 | }; |
53 | |
54 | class NET_EXPORT HashValue { |
55 | public: |
56 | explicit HashValue(const SHA256HashValue& hash); |
57 | explicit HashValue(HashValueTag tag) : tag_(tag) {} |
58 | HashValue() : tag_(HASH_VALUE_SHA256) {} |
59 | |
60 | // Serializes/Deserializes hashes in the form of |
61 | // <hash-name>"/"<base64-hash-value> |
62 | // (eg: "sha256/...") |
63 | // This format may be persisted to permanent storage, so |
64 | // care should be taken before changing the serialization. |
65 | // |
66 | // This format is used for: |
67 | // - net_internals display/setting public-key pins |
68 | // - logging public-key pins |
69 | // - serializing public-key pins |
70 | |
71 | // Deserializes a HashValue from a string. On error, returns |
72 | // false and MAY change the contents of HashValue to contain invalid data. |
73 | bool FromString(const base::StringPiece input); |
74 | |
75 | // Serializes the HashValue to a string. If an invalid HashValue |
76 | // is supplied (eg: an unknown hash tag), returns "unknown"/<base64> |
77 | std::string ToString() const; |
78 | |
79 | size_t size() const; |
80 | unsigned char* data(); |
81 | const unsigned char* data() const; |
82 | |
83 | HashValueTag tag() const { return tag_; } |
84 | |
85 | NET_EXPORT friend bool operator==(const HashValue& lhs, const HashValue& rhs); |
86 | NET_EXPORT friend bool operator!=(const HashValue& lhs, const HashValue& rhs); |
87 | NET_EXPORT friend bool operator<(const HashValue& lhs, const HashValue& rhs); |
88 | NET_EXPORT friend bool operator>(const HashValue& lhs, const HashValue& rhs); |
89 | NET_EXPORT friend bool operator<=(const HashValue& lhs, const HashValue& rhs); |
90 | NET_EXPORT friend bool operator>=(const HashValue& lhs, const HashValue& rhs); |
91 | |
92 | private: |
93 | HashValueTag tag_; |
94 | |
95 | union { |
96 | SHA256HashValue sha256; |
97 | } fingerprint; |
98 | }; |
99 | |
100 | typedef std::vector<HashValue> HashValueVector; |
101 | |
102 | |
103 | // IsSHA256HashInSortedArray returns true iff |hash| is in |array|, a sorted |
104 | // array of SHA256 hashes. |
105 | bool IsSHA256HashInSortedArray(const HashValue& hash, |
106 | base::span<const SHA256HashValue> array); |
107 | |
108 | // IsAnySHA256HashInSortedArray returns true iff any value in |hashes| is in |
109 | // |array|, a sorted array of SHA256 hashes. |
110 | bool IsAnySHA256HashInSortedArray(base::span<const HashValue> hashes, |
111 | base::span<const SHA256HashValue> array); |
112 | |
113 | } // namespace net |
114 | |
115 | #endif // NET_BASE_HASH_VALUE_H_ |
116 | |