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; | |
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 // Parse/write in this format: "sha1/Guzek9lMwR3KeIS8wwS9gBvVtIg=" | |
52 // i.e. <hash-name>"/"<base64-hash-value> | |
53 // This format is used for: | |
54 // - net_internals display/setting public-key pins | |
55 // - logging public-key pins | |
56 // - serializing public-key pins | |
Ryan Sleevi
2012/12/07 23:37:21
comment nit: This feels like an abstraction leak,
unsafe
2012/12/08 09:22:42
Added your comment but left mine as I think its us
| |
57 // | |
58 // FromString() parse errors SHALL return false, and MAY leave the | |
59 // HashValue containing incorrect data | |
60 // ToString() errors (ie unknown tag) returns "unknown/"<base64> | |
61 // (but ToString() errors should not occur!) | |
Ryan Sleevi
2012/12/07 23:37:21
comment nit: don't indent the comment here
commen
unsafe
2012/12/08 09:22:42
Replaced with your comments, but your FromString()
| |
62 bool FromString(const base::StringPiece input); | |
63 std::string ToString() const; | |
64 | |
65 size_t size() const; | |
66 unsigned char* data(); | |
67 const unsigned char* data() const; | |
68 | |
69 HashValueTag tag; | |
70 | |
71 private: | |
72 union { | |
73 SHA1HashValue sha1; | |
74 SHA256HashValue sha256; | |
75 } fingerprint; | |
76 }; | |
77 | |
78 typedef std::vector<HashValue> HashValueVector; | |
79 | |
80 | |
81 class SHA1HashValueLessThan { | |
82 public: | |
83 bool operator()(const SHA1HashValue& lhs, | |
84 const SHA1HashValue& rhs) const { | |
85 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; | |
86 } | |
87 }; | |
88 | |
89 class SHA256HashValueLessThan { | |
90 public: | |
91 bool operator()(const SHA256HashValue& lhs, | |
92 const SHA256HashValue& rhs) const { | |
93 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; | |
94 } | |
95 }; | |
96 | |
97 class HashValuesEqual { | |
98 public: | |
99 explicit HashValuesEqual(const HashValue& fingerprint) : | |
100 fingerprint_(fingerprint) {} | |
101 | |
102 bool operator()(const HashValue& other) const { | |
103 return fingerprint_.Equals(other); | |
104 } | |
105 | |
106 const HashValue& fingerprint_; | |
107 }; | |
108 | |
109 | |
110 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted | |
111 // array of SHA1 hashes. | |
112 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, | |
113 const uint8* array, | |
114 size_t array_byte_len); | |
115 | |
116 } // namespace net | |
117 | |
118 #endif // NET_BASE_HASH_VALUE_H_ | |
OLD | NEW |