1 // Copyright 2016 The Chromium Authors 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/tools/transport_security_state_generator/spki_hash.h" 6 7 #include <string> 8 #include <string_view> 9 10 #include "base/base64.h" 11 #include "base/strings/string_util.h" 12 #include "third_party/boringssl/src/include/openssl/sha.h" 13 14 namespace net::transport_security_state { 15 16 SPKIHash::SPKIHash() = default; 17 18 SPKIHash::~SPKIHash() = default; 19 FromString(std::string_view hash_string)20bool SPKIHash::FromString(std::string_view hash_string) { 21 std::string_view base64_string; 22 23 if (!base::StartsWith(hash_string, "sha256/", 24 base::CompareCase::INSENSITIVE_ASCII)) { 25 return false; 26 } 27 base64_string = hash_string.substr(7); 28 29 std::string decoded; 30 if (!base::Base64Decode(base64_string, &decoded)) { 31 return false; 32 } 33 34 if (decoded.size() != size()) { 35 return false; 36 } 37 38 memcpy(data_, decoded.data(), decoded.size()); 39 return true; 40 } 41 CalculateFromBytes(const uint8_t * input,size_t input_length)42void SPKIHash::CalculateFromBytes(const uint8_t* input, size_t input_length) { 43 SHA256(input, input_length, data_); 44 } 45 46 } // namespace net::transport_security_state 47