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 "build/build_config.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 struct SHA1HashValue { |
| 19 bool Equals(const SHA1HashValue& other) const { |
| 20 return memcmp(data, other.data, sizeof(data)) == 0; |
| 21 } |
| 22 |
| 23 unsigned char data[20]; |
| 24 }; |
| 25 |
| 26 struct SHA256HashValue { |
| 27 bool Equals(const SHA256HashValue& other) const { |
| 28 return memcmp(data, other.data, sizeof(data)) == 0; |
| 29 } |
| 30 |
| 31 unsigned char data[32]; |
| 32 }; |
| 33 |
| 34 enum HashValueTag { |
| 35 HASH_VALUE_SHA1, |
| 36 HASH_VALUE_SHA256, |
| 37 |
| 38 // This must always be last. |
| 39 HASH_VALUE_TAGS_COUNT |
| 40 }; |
| 41 |
| 42 class HashValue { |
| 43 public: |
| 44 explicit HashValue(HashValueTag tag) : tag(tag) {} |
| 45 HashValue() : tag(HASH_VALUE_SHA1) {} |
| 46 |
| 47 bool Equals(const HashValue& other) const; |
| 48 |
| 49 // Parse/write in this format: "sha1/Guzek9lMwR3KeIS8wwS9gBvVtIg=" |
| 50 // i.e. <hash-name>"/"<base64-hash-value> |
| 51 // This format is used for: |
| 52 // - net_internals display/setting public-key pins |
| 53 // - logging public-key pins |
| 54 // - serializing public-key pins |
| 55 // |
| 56 // FromString() parse errors SHALL return false, and MAY leave the |
| 57 // HashValue containing incorrect data |
| 58 // ToString() errors (ie unknown tag) returns "unknown/"<base64> |
| 59 // (but ToString() errors should not occur!) |
| 60 bool FromString(const std::string& input); |
| 61 std::string ToString() const; |
| 62 |
| 63 size_t size() const; |
| 64 unsigned char* data(); |
| 65 const unsigned char* data() const; |
| 66 |
| 67 HashValueTag tag; |
| 68 |
| 69 private: |
| 70 union { |
| 71 SHA1HashValue sha1; |
| 72 SHA256HashValue sha256; |
| 73 } fingerprint; |
| 74 }; |
| 75 |
| 76 typedef std::vector<HashValue> HashValueVector; |
| 77 |
| 78 |
| 79 class SHA1HashValueLessThan { |
| 80 public: |
| 81 bool operator()(const SHA1HashValue& lhs, |
| 82 const SHA1HashValue& rhs) const { |
| 83 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; |
| 84 } |
| 85 }; |
| 86 |
| 87 class SHA256HashValueLessThan { |
| 88 public: |
| 89 bool operator()(const SHA256HashValue& lhs, |
| 90 const SHA256HashValue& rhs) const { |
| 91 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; |
| 92 } |
| 93 }; |
| 94 |
| 95 class HashValuesEqual { |
| 96 public: |
| 97 explicit HashValuesEqual(const HashValue& fingerprint) : |
| 98 fingerprint_(fingerprint) {} |
| 99 |
| 100 bool operator()(const HashValue& other) const { |
| 101 return fingerprint_.Equals(other); |
| 102 } |
| 103 |
| 104 const HashValue& fingerprint_; |
| 105 }; |
| 106 |
| 107 |
| 108 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted |
| 109 // array of SHA1 hashes. |
| 110 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, |
| 111 const uint8* array, |
| 112 size_t array_byte_len); |
| 113 |
| 114 } // namespace net |
| 115 |
| 116 #endif // NET_BASE_HASH_VALUE_H_ |
OLD | NEW |