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 #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 | |
| 13 namespace net { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // CompareSHA1Hashes is a helper function for using bsearch() with an array of | |
| 18 // SHA1 hashes. | |
| 19 int CompareSHA1Hashes(const void* a, const void* b) { | |
| 20 return memcmp(a, b, base::kSHA1Length); | |
| 21 } | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 bool HashValue::Equals(const HashValue& other) const { | |
| 26 if (tag != other.tag) | |
| 27 return false; | |
| 28 switch (tag) { | |
| 29 case HASH_VALUE_SHA1: | |
| 30 return fingerprint.sha1.Equals(other.fingerprint.sha1); | |
| 31 case HASH_VALUE_SHA256: | |
| 32 return fingerprint.sha256.Equals(other.fingerprint.sha256); | |
| 33 default: | |
| 34 NOTREACHED() << "Unknown HashValueTag " << tag; | |
| 35 return false; | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 bool HashValue::FromString(const base::StringPiece value) { | |
| 40 base::StringPiece base64_str; | |
| 41 if (value.substr(0, 5) == "sha1/") { | |
|
agl
2012/12/10 17:13:44
Here's this assumption that we can index 0 of an e
unsafe
2012/12/10 20:59:18
Done.
| |
| 42 tag = HASH_VALUE_SHA1; | |
| 43 base64_str = value.substr(5); | |
| 44 } else if (value.substr(0, 7) == "sha256/") { | |
| 45 tag = HASH_VALUE_SHA256; | |
| 46 base64_str = value.substr(7); | |
| 47 } else { | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 std::string decoded; | |
| 52 if (!base::Base64Decode(base64_str, &decoded) || | |
| 53 decoded.size() != size()) { | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 memcpy(data(), decoded.data(), size()); | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 std::string HashValue::ToString() const { | |
| 62 std::string base64_str; | |
| 63 base::Base64Encode(base::StringPiece(reinterpret_cast<const char*>(data()), | |
| 64 size()), &base64_str); | |
| 65 switch (tag) { | |
| 66 case HASH_VALUE_SHA1: | |
| 67 return std::string("sha1/") + base64_str; | |
| 68 case HASH_VALUE_SHA256: | |
| 69 return std::string("sha256/") + base64_str; | |
| 70 default: | |
| 71 NOTREACHED() << "Unknown HashValueTag " << tag; | |
| 72 return std::string("unknown/" + base64_str); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 size_t HashValue::size() const { | |
| 77 switch (tag) { | |
| 78 case HASH_VALUE_SHA1: | |
| 79 return sizeof(fingerprint.sha1.data); | |
| 80 case HASH_VALUE_SHA256: | |
| 81 return sizeof(fingerprint.sha256.data); | |
| 82 default: | |
| 83 NOTREACHED() << "Unknown HashValueTag " << tag; | |
| 84 // While an invalid tag should not happen, return a non-zero length | |
| 85 // to avoid compiler warnings when the result of size() is | |
| 86 // used with functions like memset. | |
| 87 return sizeof(fingerprint.sha1.data); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 unsigned char* HashValue::data() { | |
| 92 return const_cast<unsigned char*>(const_cast<const HashValue*>(this)->data()); | |
| 93 } | |
| 94 | |
| 95 const unsigned char* HashValue::data() const { | |
| 96 switch (tag) { | |
| 97 case HASH_VALUE_SHA1: | |
| 98 return fingerprint.sha1.data; | |
| 99 case HASH_VALUE_SHA256: | |
| 100 return fingerprint.sha256.data; | |
| 101 default: | |
| 102 NOTREACHED() << "Unknown HashValueTag " << tag; | |
| 103 return NULL; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, | |
| 108 const uint8* array, | |
| 109 size_t array_byte_len) { | |
| 110 DCHECK_EQ(0u, array_byte_len % base::kSHA1Length); | |
| 111 const size_t arraylen = array_byte_len / base::kSHA1Length; | |
| 112 return NULL != bsearch(hash.data, array, arraylen, base::kSHA1Length, | |
| 113 CompareSHA1Hashes); | |
| 114 } | |
| 115 | |
| 116 } // namespace net | |
| 117 | |
|
agl
2012/12/10 17:13:44
extra blank line at end of file.
unsafe
2012/12/10 20:59:18
Done.
| |
| OLD | NEW |