| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/tools/domain_security_preload_generator/spki_hash.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/base64.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "third_party/boringssl/src/include/openssl/evp.h" |
| 13 |
| 14 namespace net { |
| 15 |
| 16 SPKIHash::SPKIHash() {} |
| 17 |
| 18 SPKIHash::~SPKIHash() {} |
| 19 |
| 20 bool SPKIHash::FromString(const std::string& hash_string) { |
| 21 base::StringPiece 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 |
| 42 bool SPKIHash::CalculateFromBytes(const uint8_t* input, size_t input_length) { |
| 43 bssl::ScopedEVP_MD_CTX context; |
| 44 if (!EVP_DigestInit(context.get(), EVP_sha256())) { |
| 45 return false; |
| 46 } |
| 47 |
| 48 if (!EVP_DigestUpdate(context.get(), input, input_length)) { |
| 49 return false; |
| 50 } |
| 51 |
| 52 uint32_t digest_len; |
| 53 if (!EVP_DigestFinal(context.get(), data_, &digest_len)) { |
| 54 return false; |
| 55 } |
| 56 |
| 57 DCHECK(digest_len == size()); |
| 58 return true; |
| 59 } |
| 60 |
| 61 } // namespace net |
| OLD | NEW |