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

Side by Side Diff: net/tools/transport_security_state_generator/spki_hash.cc

Issue 2793823002: Add unittests for certificate and file parsing. (Closed)
Patch Set: comments davidben Created 3 years, 8 months 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/tools/transport_security_state_generator/spki_hash.h" 5 #include "net/tools/transport_security_state_generator/spki_hash.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "third_party/boringssl/src/include/openssl/sha.h" 12 #include "third_party/boringssl/src/include/openssl/sha.h"
13 13
14 namespace net { 14 namespace net {
15 15
16 namespace transport_security_state { 16 namespace transport_security_state {
17 17
18 SPKIHash::SPKIHash() {} 18 SPKIHash::SPKIHash() {}
19 19
20 SPKIHash::~SPKIHash() {} 20 SPKIHash::~SPKIHash() {}
21 21
22 bool SPKIHash::FromString(const std::string& hash_string) { 22 bool SPKIHash::FromString(base::StringPiece hash_string) {
23 std::string base64_string; 23 base::StringPiece base64_string;
24 24
25 if (!base::StartsWith(hash_string, "sha256/", 25 if (!base::StartsWith(hash_string, "sha256/",
26 base::CompareCase::INSENSITIVE_ASCII)) { 26 base::CompareCase::INSENSITIVE_ASCII)) {
27 return false; 27 return false;
28 } 28 }
29 base64_string = hash_string.substr(7); 29 base64_string = hash_string.substr(7);
30 30
31 std::string decoded; 31 std::string decoded;
32 if (!base::Base64Decode(base64_string, &decoded)) { 32 if (!base::Base64Decode(base64_string, &decoded)) {
33 return false; 33 return false;
34 } 34 }
35 35
36 if (decoded.size() != size()) { 36 if (decoded.size() != size()) {
37 return false; 37 return false;
38 } 38 }
39 39
40 memcpy(data_, decoded.data(), decoded.size()); 40 memcpy(data_, decoded.data(), decoded.size());
41 return true; 41 return true;
42 } 42 }
43 43
44 void SPKIHash::CalculateFromBytes(const uint8_t* input, size_t input_length) { 44 void SPKIHash::CalculateFromBytes(const uint8_t* input, size_t input_length) {
45 SHA256(input, input_length, data_); 45 SHA256(input, input_length, data_);
46 } 46 }
47 47
48 } // namespace transport_security_state 48 } // namespace transport_security_state
49 49
50 } // namespace net 50 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698