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 <cstdlib> |
| 8 #include <cstring> |
| 9 |
| 10 #include "base/base64.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/sha1.h" |
| 13 #include "base/string_split.h" |
| 14 #include "base/string_util.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // CompareSHA1Hashes is a helper function for using bsearch() with an array of |
| 21 // SHA1 hashes. |
| 22 int CompareSHA1Hashes(const void* a, const void* b) { |
| 23 return memcmp(a, b, base::kSHA1Length); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 bool HashValue::Equals(const HashValue& other) const { |
| 29 if (tag != other.tag) |
| 30 return false; |
| 31 switch (tag) { |
| 32 case HASH_VALUE_SHA1: |
| 33 return fingerprint.sha1.Equals(other.fingerprint.sha1); |
| 34 case HASH_VALUE_SHA256: |
| 35 return fingerprint.sha256.Equals(other.fingerprint.sha256); |
| 36 default: |
| 37 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 38 return false; |
| 39 } |
| 40 } |
| 41 |
| 42 bool HashValue::FromString(const base::StringPiece value) { |
| 43 base::StringPiece base64_str; |
| 44 if (value.substr(0, 5) == "sha1/") { |
| 45 tag = HASH_VALUE_SHA1; |
| 46 base64_str = value.substr(5); |
| 47 } else if (value.substr(0, 7) == "sha256/") { |
| 48 tag = HASH_VALUE_SHA256; |
| 49 base64_str = value.substr(7); |
| 50 } else { |
| 51 return false; |
| 52 } |
| 53 |
| 54 std::string decoded; |
| 55 if (!base::Base64Decode(base64_str, &decoded) || |
| 56 decoded.size() != size()) { |
| 57 return false; |
| 58 } |
| 59 |
| 60 memcpy(data(), decoded.data(), size()); |
| 61 return true; |
| 62 } |
| 63 |
| 64 std::string HashValue::ToString() const { |
| 65 std::string base64_str; |
| 66 base::Base64Encode(base::StringPiece(reinterpret_cast<const char*>(data()), |
| 67 size()), &base64_str); |
| 68 switch (tag) { |
| 69 case HASH_VALUE_SHA1: |
| 70 return std::string("sha1/" + base64_str); |
| 71 case HASH_VALUE_SHA256: |
| 72 return std::string("sha256/" + base64_str); |
| 73 default: |
| 74 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 75 return std::string("unknown/" + base64_str); |
| 76 } |
| 77 } |
| 78 |
| 79 size_t HashValue::size() const { |
| 80 switch (tag) { |
| 81 case HASH_VALUE_SHA1: |
| 82 return sizeof(fingerprint.sha1.data); |
| 83 case HASH_VALUE_SHA256: |
| 84 return sizeof(fingerprint.sha256.data); |
| 85 default: |
| 86 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 87 // Although this is NOTREACHED, this function might be inlined and its |
| 88 // return value can be passed to memset as the length argument. If we |
| 89 // returned 0 here, it might result in what appears (in some stages of |
| 90 // compilation) to be a call to to memset with a length argument of 0, |
| 91 // which results in a warning. Therefore, we return a dummy value |
| 92 // here. |
| 93 return sizeof(fingerprint.sha1.data); |
| 94 } |
| 95 } |
| 96 |
| 97 unsigned char* HashValue::data() { |
| 98 return const_cast<unsigned char*>(const_cast<const HashValue*>(this)->data()); |
| 99 } |
| 100 |
| 101 const unsigned char* HashValue::data() const { |
| 102 switch (tag) { |
| 103 case HASH_VALUE_SHA1: |
| 104 return fingerprint.sha1.data; |
| 105 case HASH_VALUE_SHA256: |
| 106 return fingerprint.sha256.data; |
| 107 default: |
| 108 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 109 return NULL; |
| 110 } |
| 111 } |
| 112 |
| 113 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, |
| 114 const uint8* array, |
| 115 size_t array_byte_len) { |
| 116 DCHECK_EQ(0u, array_byte_len % base::kSHA1Length); |
| 117 const size_t arraylen = array_byte_len / base::kSHA1Length; |
| 118 return NULL != bsearch(hash.data, array, arraylen, base::kSHA1Length, |
| 119 CompareSHA1Hashes); |
| 120 } |
| 121 |
| 122 } // namespace net |
| 123 |
OLD | NEW |