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 #include "net/base/hash_value.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/logging.h" | |
9 #include "base/sha1.h" | |
10 #include "base/string_split.h" | |
11 #include "base/string_util.h" | |
12 #include "crypto/secure_util.h" | |
13 | |
14 namespace net { | |
15 | |
16 namespace { | |
17 | |
18 // CompareSHA1Hashes is a helper function for using bsearch() with an array of | |
19 // SHA1 hashes. | |
20 int CompareSHA1Hashes(const void* a, const void* b) { | |
21 return memcmp(a, b, base::kSHA1Length); | |
22 } | |
23 | |
24 } // namespace | |
25 | |
26 | |
27 bool SHA1HashValue::Equals(const SHA1HashValue& other) const { | |
28 return crypto::SecureMemEqual(data, other.data, sizeof(data)); | |
29 } | |
30 | |
31 bool SHA256HashValue::Equals(const SHA256HashValue& other) const { | |
32 return crypto::SecureMemEqual(data, other.data, sizeof(data)); | |
Ryan Sleevi
2012/12/20 23:44:13
I missed this change from your moving data.
I wou
unsafe
2012/12/21 02:56:49
This was a new change in last patch set, based on
| |
33 } | |
34 | |
35 bool HashValue::Equals(const HashValue& other) const { | |
36 if (tag != other.tag) | |
37 return false; | |
38 switch (tag) { | |
39 case HASH_VALUE_SHA1: | |
40 return fingerprint.sha1.Equals(other.fingerprint.sha1); | |
41 case HASH_VALUE_SHA256: | |
42 return fingerprint.sha256.Equals(other.fingerprint.sha256); | |
43 default: | |
44 NOTREACHED() << "Unknown HashValueTag " << tag; | |
45 return false; | |
46 } | |
47 } | |
48 | |
49 bool HashValue::FromString(const base::StringPiece value) { | |
50 base::StringPiece base64_str; | |
51 /* Cannot take substr(pos, ...) with 'pos' past end of string, | |
52 so check for adequate string length first */ | |
Ryan Sleevi
2012/12/20 23:44:13
Nit: This comment is unnecessary.
unsafe
2012/12/21 02:56:49
Done.
| |
53 if (value.size() > 5 && value.substr(0, 5) == "sha1/") { | |
Ryan Sleevi
2012/12/20 23:44:13
if (value.compare(0, 5, "sha1/") == 0) {
...
} e
unsafe
2012/12/21 02:56:49
StringPiece doesn't have the 3-arg compare() but s
| |
54 tag = HASH_VALUE_SHA1; | |
55 base64_str = value.substr(5); | |
56 } else if (value.size() > 7 && value.substr(0, 7) == "sha256/") { | |
57 tag = HASH_VALUE_SHA256; | |
58 base64_str = value.substr(7); | |
59 } else { | |
60 return false; | |
61 } | |
62 | |
63 std::string decoded; | |
64 if (!base::Base64Decode(base64_str, &decoded) || | |
65 decoded.size() != size()) { | |
Ryan Sleevi
2012/12/20 23:44:13
style nit: Does this fit on one line? If so, do th
unsafe
2012/12/21 02:56:49
Done.
| |
66 return false; | |
67 } | |
68 | |
69 memcpy(data(), decoded.data(), size()); | |
70 return true; | |
71 } | |
72 | |
73 std::string HashValue::ToString() const { | |
74 std::string base64_str; | |
75 base::Base64Encode(base::StringPiece(reinterpret_cast<const char*>(data()), | |
76 size()), &base64_str); | |
77 switch (tag) { | |
78 case HASH_VALUE_SHA1: | |
79 return std::string("sha1/") + base64_str; | |
80 case HASH_VALUE_SHA256: | |
81 return std::string("sha256/") + base64_str; | |
82 default: | |
83 NOTREACHED() << "Unknown HashValueTag " << tag; | |
84 return std::string("unknown/" + base64_str); | |
85 } | |
86 } | |
87 | |
88 size_t HashValue::size() const { | |
89 switch (tag) { | |
90 case HASH_VALUE_SHA1: | |
91 return sizeof(fingerprint.sha1.data); | |
92 case HASH_VALUE_SHA256: | |
93 return sizeof(fingerprint.sha256.data); | |
94 default: | |
95 NOTREACHED() << "Unknown HashValueTag " << tag; | |
96 // While an invalid tag should not happen, return a non-zero length | |
97 // to avoid compiler warnings when the result of size() is | |
98 // used with functions like memset. | |
99 return sizeof(fingerprint.sha1.data); | |
100 } | |
101 } | |
102 | |
103 unsigned char* HashValue::data() { | |
104 return const_cast<unsigned char*>(const_cast<const HashValue*>(this)->data()); | |
105 } | |
106 | |
107 const unsigned char* HashValue::data() const { | |
108 switch (tag) { | |
109 case HASH_VALUE_SHA1: | |
110 return fingerprint.sha1.data; | |
111 case HASH_VALUE_SHA256: | |
112 return fingerprint.sha256.data; | |
113 default: | |
114 NOTREACHED() << "Unknown HashValueTag " << tag; | |
115 return NULL; | |
116 } | |
117 } | |
118 | |
119 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, | |
120 const uint8* array, | |
121 size_t array_byte_len) { | |
122 DCHECK_EQ(0u, array_byte_len % base::kSHA1Length); | |
123 const size_t arraylen = array_byte_len / base::kSHA1Length; | |
124 return NULL != bsearch(hash.data, array, arraylen, base::kSHA1Length, | |
125 CompareSHA1Hashes); | |
126 } | |
127 | |
128 } // namespace net | |
OLD | NEW |