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