| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/hash_value.h" | 5 #include "net/base/hash_value.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/sha1.h" | 9 #include "base/sha1.h" |
| 10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // CompareSHA1Hashes is a helper function for using bsearch() with an array of | 17 // CompareSHA1Hashes is a helper function for using bsearch() with an array of |
| 18 // SHA1 hashes. | 18 // SHA1 hashes. |
| 19 int CompareSHA1Hashes(const void* a, const void* b) { | 19 int CompareSHA1Hashes(const void* a, const void* b) { |
| 20 return memcmp(a, b, base::kSHA1Length); | 20 return memcmp(a, b, base::kSHA1Length); |
| 21 } | 21 } |
| 22 | 22 |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 | |
| 26 bool SHA1HashValue::Equals(const SHA1HashValue& other) const { | 25 bool SHA1HashValue::Equals(const SHA1HashValue& other) const { |
| 27 return memcmp(data, other.data, sizeof(data)) == 0; | 26 return memcmp(data, other.data, sizeof(data)) == 0; |
| 28 } | 27 } |
| 29 | 28 |
| 30 bool SHA256HashValue::Equals(const SHA256HashValue& other) const { | 29 bool SHA256HashValue::Equals(const SHA256HashValue& other) const { |
| 31 return memcmp(data, other.data, sizeof(data)) == 0; | 30 return memcmp(data, other.data, sizeof(data)) == 0; |
| 32 } | 31 } |
| 33 | 32 |
| 34 bool HashValue::Equals(const HashValue& other) const { | 33 bool HashValue::Equals(const HashValue& other) const { |
| 35 if (tag != other.tag) | 34 if (tag != other.tag) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 60 std::string decoded; | 59 std::string decoded; |
| 61 if (!base::Base64Decode(base64_str, &decoded) || decoded.size() != size()) | 60 if (!base::Base64Decode(base64_str, &decoded) || decoded.size() != size()) |
| 62 return false; | 61 return false; |
| 63 | 62 |
| 64 memcpy(data(), decoded.data(), size()); | 63 memcpy(data(), decoded.data(), size()); |
| 65 return true; | 64 return true; |
| 66 } | 65 } |
| 67 | 66 |
| 68 std::string HashValue::ToString() const { | 67 std::string HashValue::ToString() const { |
| 69 std::string base64_str; | 68 std::string base64_str; |
| 70 base::Base64Encode(base::StringPiece(reinterpret_cast<const char*>(data()), | 69 base::Base64Encode( |
| 71 size()), &base64_str); | 70 base::StringPiece(reinterpret_cast<const char*>(data()), size()), |
| 71 &base64_str); |
| 72 switch (tag) { | 72 switch (tag) { |
| 73 case HASH_VALUE_SHA1: | 73 case HASH_VALUE_SHA1: |
| 74 return std::string("sha1/") + base64_str; | 74 return std::string("sha1/") + base64_str; |
| 75 case HASH_VALUE_SHA256: | 75 case HASH_VALUE_SHA256: |
| 76 return std::string("sha256/") + base64_str; | 76 return std::string("sha256/") + base64_str; |
| 77 default: | 77 default: |
| 78 NOTREACHED() << "Unknown HashValueTag " << tag; | 78 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 79 return std::string("unknown/" + base64_str); | 79 return std::string("unknown/" + base64_str); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 size_t HashValue::size() const { | 83 size_t HashValue::size() const { |
| 84 switch (tag) { | 84 switch (tag) { |
| 85 case HASH_VALUE_SHA1: | 85 case HASH_VALUE_SHA1: |
| 86 return sizeof(fingerprint.sha1.data); | 86 return sizeof(fingerprint.sha1.data); |
| 87 case HASH_VALUE_SHA256: | 87 case HASH_VALUE_SHA256: |
| 88 return sizeof(fingerprint.sha256.data); | 88 return sizeof(fingerprint.sha256.data); |
| 89 default: | 89 default: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 109 NOTREACHED() << "Unknown HashValueTag " << tag; | 109 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 110 return NULL; | 110 return NULL; |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, | 114 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, |
| 115 const uint8* array, | 115 const uint8* array, |
| 116 size_t array_byte_len) { | 116 size_t array_byte_len) { |
| 117 DCHECK_EQ(0u, array_byte_len % base::kSHA1Length); | 117 DCHECK_EQ(0u, array_byte_len % base::kSHA1Length); |
| 118 const size_t arraylen = array_byte_len / base::kSHA1Length; | 118 const size_t arraylen = array_byte_len / base::kSHA1Length; |
| 119 return NULL != bsearch(hash.data, array, arraylen, base::kSHA1Length, | 119 return NULL != |
| 120 CompareSHA1Hashes); | 120 bsearch( |
| 121 hash.data, array, arraylen, base::kSHA1Length, CompareSHA1Hashes); |
| 121 } | 122 } |
| 122 | 123 |
| 123 } // namespace net | 124 } // namespace net |
| OLD | NEW |