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 <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 fingerprint.sha1 = hash; | 30 fingerprint.sha1 = hash; |
31 } | 31 } |
32 | 32 |
33 HashValue::HashValue(const SHA256HashValue& hash) | 33 HashValue::HashValue(const SHA256HashValue& hash) |
34 : HashValue(HASH_VALUE_SHA256) { | 34 : HashValue(HASH_VALUE_SHA256) { |
35 fingerprint.sha256 = hash; | 35 fingerprint.sha256 = hash; |
36 } | 36 } |
37 | 37 |
38 bool HashValue::FromString(const base::StringPiece value) { | 38 bool HashValue::FromString(const base::StringPiece value) { |
39 base::StringPiece base64_str; | 39 base::StringPiece base64_str; |
40 if (value.starts_with("sha1/")) { | 40 if (value.starts_with("sha256/")) { |
41 tag = HASH_VALUE_SHA1; | |
42 base64_str = value.substr(5); | |
43 } else if (value.starts_with("sha256/")) { | |
44 tag = HASH_VALUE_SHA256; | 41 tag = HASH_VALUE_SHA256; |
45 base64_str = value.substr(7); | 42 base64_str = value.substr(7); |
46 } else { | 43 } else { |
47 return false; | 44 return false; |
48 } | 45 } |
49 | 46 |
50 std::string decoded; | 47 std::string decoded; |
51 if (!base::Base64Decode(base64_str, &decoded) || decoded.size() != size()) | 48 if (!base::Base64Decode(base64_str, &decoded) || decoded.size() != size()) |
52 return false; | 49 return false; |
53 | 50 |
54 memcpy(data(), decoded.data(), size()); | 51 memcpy(data(), decoded.data(), size()); |
55 return true; | 52 return true; |
56 } | 53 } |
57 | 54 |
58 std::string HashValue::ToString() const { | 55 std::string HashValue::ToString() const { |
59 std::string base64_str; | 56 std::string base64_str; |
60 base::Base64Encode(base::StringPiece(reinterpret_cast<const char*>(data()), | 57 base::Base64Encode(base::StringPiece(reinterpret_cast<const char*>(data()), |
61 size()), &base64_str); | 58 size()), &base64_str); |
62 switch (tag) { | 59 switch (tag) { |
63 case HASH_VALUE_SHA1: | 60 case HASH_VALUE_SHA1: |
64 return std::string("sha1/") + base64_str; | 61 return std::string("sha1/") + base64_str; |
davidben
2017/06/26 20:15:55
Out of curiosity, what are the remaining HASH_VALU
davidben
2017/06/26 20:17:00
(Hrm. Codesearch seems to think there actually isn
| |
65 case HASH_VALUE_SHA256: | 62 case HASH_VALUE_SHA256: |
66 return std::string("sha256/") + base64_str; | 63 return std::string("sha256/") + base64_str; |
67 default: | 64 default: |
68 NOTREACHED() << "Unknown HashValueTag " << tag; | 65 NOTREACHED() << "Unknown HashValueTag " << tag; |
69 return std::string("unknown/" + base64_str); | 66 return std::string("unknown/" + base64_str); |
70 } | 67 } |
71 } | 68 } |
72 | 69 |
73 size_t HashValue::size() const { | 70 size_t HashValue::size() const { |
74 switch (tag) { | 71 switch (tag) { |
(...skipping 29 matching lines...) Expand all Loading... | |
104 bool IsSHA256HashInSortedArray(const SHA256HashValue& hash, | 101 bool IsSHA256HashInSortedArray(const SHA256HashValue& hash, |
105 const uint8_t* array, | 102 const uint8_t* array, |
106 size_t array_byte_len) { | 103 size_t array_byte_len) { |
107 DCHECK_EQ(0u, array_byte_len % crypto::kSHA256Length); | 104 DCHECK_EQ(0u, array_byte_len % crypto::kSHA256Length); |
108 const size_t arraylen = array_byte_len / crypto::kSHA256Length; | 105 const size_t arraylen = array_byte_len / crypto::kSHA256Length; |
109 return NULL != bsearch(hash.data, array, arraylen, crypto::kSHA256Length, | 106 return NULL != bsearch(hash.data, array, arraylen, crypto::kSHA256Length, |
110 CompareSHA1Hashes); | 107 CompareSHA1Hashes); |
111 } | 108 } |
112 | 109 |
113 } // namespace net | 110 } // namespace net |
OLD | NEW |