Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <string.h> | |
| 9 | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/string_piece.h" | |
| 15 #include "build/build_config.h" | |
| 16 #include "net/base/net_export.h" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 struct NET_EXPORT SHA1HashValue { | |
| 21 bool Equals(const SHA1HashValue& other) const { | |
| 22 return memcmp(data, other.data, sizeof(data)) == 0; | |
|
agl
2012/12/10 17:13:44
I would be tempted to use a constant-time compare
unsafe
2012/12/10 20:59:18
Done - also moved implementation to C file instead
| |
| 23 } | |
| 24 | |
| 25 unsigned char data[20]; | |
| 26 }; | |
| 27 | |
| 28 struct NET_EXPORT SHA256HashValue { | |
| 29 bool Equals(const SHA256HashValue& other) const { | |
| 30 return memcmp(data, other.data, sizeof(data)) == 0; | |
| 31 } | |
| 32 | |
| 33 unsigned char data[32]; | |
| 34 }; | |
| 35 | |
| 36 enum HashValueTag { | |
| 37 HASH_VALUE_SHA1, | |
| 38 HASH_VALUE_SHA256, | |
| 39 | |
| 40 // This must always be last. | |
| 41 HASH_VALUE_TAGS_COUNT | |
| 42 }; | |
| 43 | |
| 44 class NET_EXPORT HashValue { | |
| 45 public: | |
| 46 explicit HashValue(HashValueTag tag) : tag(tag) {} | |
| 47 HashValue() : tag(HASH_VALUE_SHA1) {} | |
| 48 | |
| 49 bool Equals(const HashValue& other) const; | |
| 50 | |
| 51 // Serializes/Deserializes hashes in the form of | |
| 52 // <hash-name>"/"<base64-hash-value> | |
| 53 // (eg: "sha1/...") | |
| 54 // This format may be persisted to permanent storage, so | |
| 55 // care should be taken before changing the serialization. | |
| 56 // | |
| 57 // This format is used for: | |
| 58 // - net_internals display/setting public-key pins | |
| 59 // - logging public-key pins | |
| 60 // - serializing public-key pins | |
| 61 | |
| 62 // Deserializes a HashValue from a string. On error, returns | |
| 63 // false and MAY change the contents of HashValue to contain invalid data. | |
| 64 bool FromString(const base::StringPiece input); | |
| 65 | |
| 66 // Serializes the HashValue to a string. If an invalid HashValue | |
| 67 // is supplied (eg: an unknown hash tag), returns "unknown"/<base64> | |
| 68 std::string ToString() const; | |
| 69 | |
| 70 size_t size() const; | |
| 71 unsigned char* data(); | |
| 72 const unsigned char* data() const; | |
| 73 | |
| 74 HashValueTag tag; | |
| 75 | |
| 76 private: | |
| 77 union { | |
| 78 SHA1HashValue sha1; | |
| 79 SHA256HashValue sha256; | |
| 80 } fingerprint; | |
| 81 }; | |
| 82 | |
| 83 typedef std::vector<HashValue> HashValueVector; | |
| 84 | |
| 85 | |
| 86 class SHA1HashValueLessThan { | |
| 87 public: | |
| 88 bool operator()(const SHA1HashValue& lhs, | |
| 89 const SHA1HashValue& rhs) const { | |
| 90 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; | |
| 91 } | |
| 92 }; | |
| 93 | |
| 94 class SHA256HashValueLessThan { | |
| 95 public: | |
| 96 bool operator()(const SHA256HashValue& lhs, | |
| 97 const SHA256HashValue& rhs) const { | |
| 98 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; | |
| 99 } | |
| 100 }; | |
| 101 | |
| 102 class HashValuesEqual { | |
| 103 public: | |
| 104 explicit HashValuesEqual(const HashValue& fingerprint) : | |
| 105 fingerprint_(fingerprint) {} | |
| 106 | |
| 107 bool operator()(const HashValue& other) const { | |
| 108 return fingerprint_.Equals(other); | |
| 109 } | |
| 110 | |
| 111 const HashValue& fingerprint_; | |
| 112 }; | |
| 113 | |
| 114 | |
| 115 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted | |
| 116 // array of SHA1 hashes. | |
| 117 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, | |
| 118 const uint8* array, | |
| 119 size_t array_byte_len); | |
| 120 | |
| 121 } // namespace net | |
| 122 | |
| 123 #endif // NET_BASE_HASH_VALUE_H_ | |
| OLD | NEW |