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 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 <string_view> 14 #include <vector> 15 16 #include "base/containers/span.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); HashValue(HashValueTag tag)57 explicit HashValue(HashValueTag tag) : tag_(tag) {} HashValue()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. Returns false if the input is not 72 // valid. 73 bool FromString(std::string_view input); 74 75 // Serializes the HashValue to a string. 76 std::string ToString() const; 77 78 size_t size() const; 79 unsigned char* data(); 80 const unsigned char* data() const; 81 tag()82 HashValueTag tag() const { return tag_; } 83 84 NET_EXPORT friend bool operator==(const HashValue& lhs, const HashValue& rhs); 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 91 private: 92 HashValueTag tag_; 93 94 union { 95 SHA256HashValue sha256; 96 } fingerprint; 97 }; 98 99 typedef std::vector<HashValue> HashValueVector; 100 101 102 // IsSHA256HashInSortedArray returns true iff |hash| is in |array|, a sorted 103 // array of SHA256 hashes. 104 bool IsSHA256HashInSortedArray(const HashValue& hash, 105 base::span<const SHA256HashValue> array); 106 107 // IsAnySHA256HashInSortedArray returns true iff any value in |hashes| is in 108 // |array|, a sorted array of SHA256 hashes. 109 bool IsAnySHA256HashInSortedArray(base::span<const HashValue> hashes, 110 base::span<const SHA256HashValue> array); 111 112 } // namespace net 113 114 #endif // NET_BASE_HASH_VALUE_H_ 115