Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: net/base/hash_value.cc

Issue 11274032: Separate http_security_headers from transport_security_state (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 bool HashValue::Equals(const HashValue& other) const {
19 if (tag != other.tag)
20 return false;
21 switch (tag) {
22 case HASH_VALUE_SHA1:
23 return fingerprint.sha1.Equals(other.fingerprint.sha1);
24 case HASH_VALUE_SHA256:
25 return fingerprint.sha256.Equals(other.fingerprint.sha256);
26 default:
27 NOTREACHED() << "Unknown HashValueTag " << tag;
28 return false;
29 }
30 }
31
32 bool HashValue::FromString(const std::string& value) {
33 std::string base64_str;
Ryan Sleevi 2012/11/13 19:02:32 nit: (minor) You can use a base::StringPiece here,
unsafe 2012/11/13 23:20:18 Leaving std::string's in-place for now as I don't
Ryan Sleevi 2012/11/13 23:32:05 base::StringPiece behaves like a string, except it
34 if (value.substr(0, 5) == "sha1/") {
35 tag = HASH_VALUE_SHA1;
36 base64_str = value.substr(5, 28); // length of base64 string
37 } else if (value.substr(0, 7) == "sha256/") {
38 tag = HASH_VALUE_SHA256;
39 base64_str = value.substr(7, 44); // length of base64 string
Ryan Sleevi 2012/11/13 19:02:32 DESIGN: By taking the fixed length .substr() of a
unsafe 2012/11/13 23:20:18 Done.
40 } else {
41 return false;
42 }
43
44 std::string decoded;
45 if (!base::Base64Decode(base64_str, &decoded) ||
46 decoded.size() != size()) {
47 return false;
48 }
49 memcpy(data(), decoded.data(), size());
50 return true;
51 }
52
53 std::string HashValue::ToString() const {
54 std::string base64_str;
55 base::Base64Encode(base::StringPiece(reinterpret_cast<const char*>(data()),
56 size()), &base64_str);
57 switch (tag) {
58 case HASH_VALUE_SHA1:
59 return std::string("sha1/" + base64_str);
60 case HASH_VALUE_SHA256:
61 return std::string("sha256/" + base64_str);
62 default:
63 NOTREACHED() << "Unknown HashValueTag " << tag;
64 return std::string("unknown/" + base64_str);
65 }
Ryan Sleevi 2012/11/13 19:02:32 nit: the case labels of switch statements get inde
unsafe 2012/11/13 23:20:18 Hm, where does it say that? I see it saying 2-spa
Ryan Sleevi 2012/11/13 23:32:05 Sorry, you're right - one indent, two spaces. So t
66 }
67
68 size_t HashValue::size() const {
69 switch (tag) {
70 case HASH_VALUE_SHA1:
71 return sizeof(fingerprint.sha1.data);
72 case HASH_VALUE_SHA256:
73 return sizeof(fingerprint.sha256.data);
74 default:
75 NOTREACHED() << "Unknown HashValueTag " << tag;
76 // Although this is NOTREACHED, this function might be inlined and its
77 // return value can be passed to memset as the length argument. If we
78 // returned 0 here, it might result in what appears (in some stages of
79 // compilation) to be a call to to memset with a length argument of 0,
80 // which results in a warning. Therefore, we return a dummy value
81 // here.
82 return sizeof(fingerprint.sha1.data);
83 }
84 }
85
86 unsigned char* HashValue::data() {
87 return const_cast<unsigned char*>(const_cast<const HashValue*>(this)->data());
88 }
89
90 const unsigned char* HashValue::data() const {
91 switch (tag) {
92 case HASH_VALUE_SHA1:
93 return fingerprint.sha1.data;
94 case HASH_VALUE_SHA256:
95 return fingerprint.sha256.data;
96 default:
97 NOTREACHED() << "Unknown HashValueTag " << tag;
98 return NULL;
99 }
100 }
101
102 namespace {
Ryan Sleevi 2012/11/13 19:02:32 This should be moved to ~line 17 (unnamed namespac
unsafe 2012/11/13 23:20:18 Done.
103
104 // CompareSHA1Hashes is a helper function for using bsearch() with an array of
105 // SHA1 hashes.
106 int CompareSHA1Hashes(const void* a, const void* b) {
107 return memcmp(a, b, base::kSHA1Length);
108 }
109
110 } // namespace
111
112 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash,
113 const uint8* array,
114 size_t array_byte_len) {
115 DCHECK_EQ(0u, array_byte_len % base::kSHA1Length);
116 const size_t arraylen = array_byte_len / base::kSHA1Length;
117 return NULL != bsearch(hash.data, array, arraylen, base::kSHA1Length,
palmer 2012/11/13 19:35:04 Perhaps it's better to use std::binary_search and
unsafe 2012/11/13 23:20:18 Maybe - I'm leaving this as-is, if that's OK. Thi
118 CompareSHA1Hashes);
119 }
120
121 } // namespace net
122
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698