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