Avi Drissman | 60039d4 | 2022-09-13 21:49:05 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | dca94c7 | 2014-05-13 08:26:25 | [diff] [blame] | 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 "extensions/browser/content_hash_tree.h" |
| 6 | |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
David Dorwin | 3f503b8 | 2022-04-20 04:07:03 | [diff] [blame] | 9 | #include "base/check_op.h" |
[email protected] | dca94c7 | 2014-05-13 08:26:25 | [diff] [blame] | 10 | #include "crypto/secure_hash.h" |
| 11 | #include "crypto/sha2.h" |
| 12 | |
| 13 | namespace extensions { |
| 14 | |
| 15 | std::string ComputeTreeHashRoot(const std::vector<std::string>& leaf_hashes, |
| 16 | int branch_factor) { |
| 17 | if (leaf_hashes.empty() || branch_factor < 2) |
| 18 | return std::string(); |
| 19 | |
| 20 | // The nodes of the tree we're currently operating on. |
| 21 | std::vector<std::string> current_nodes; |
| 22 | |
| 23 | // We avoid having to copy all of the input leaf nodes into |current_nodes| |
| 24 | // by using a pointer. So the first iteration of the loop this points at |
| 25 | // |leaf_hashes|, but thereafter it points at |current_nodes|. |
| 26 | const std::vector<std::string>* current = &leaf_hashes; |
| 27 | |
| 28 | // Where we're inserting new hashes computed from the current level. |
| 29 | std::vector<std::string> parent_nodes; |
| 30 | |
| 31 | while (current->size() > 1) { |
| 32 | // Iterate over the current level of hashes, computing the hash of up to |
| 33 | // |branch_factor| elements to form the hash of each parent node. |
jdoerrie | a1e1598b | 2018-10-10 09:10:37 | [diff] [blame] | 34 | auto i = current->cbegin(); |
| 35 | while (i != current->cend()) { |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 36 | std::unique_ptr<crypto::SecureHash> hash( |
[email protected] | dca94c7 | 2014-05-13 08:26:25 | [diff] [blame] | 37 | crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 38 | for (int j = 0; j < branch_factor && i != current->end(); j++) { |
| 39 | DCHECK_EQ(i->size(), crypto::kSHA256Length); |
| 40 | hash->Update(i->data(), i->size()); |
| 41 | ++i; |
| 42 | } |
| 43 | parent_nodes.push_back(std::string(crypto::kSHA256Length, 0)); |
Daniel Cheng | d54aee2 | 2022-02-26 09:26:32 | [diff] [blame] | 44 | hash->Finish(std::data(parent_nodes.back()), crypto::kSHA256Length); |
[email protected] | dca94c7 | 2014-05-13 08:26:25 | [diff] [blame] | 45 | } |
| 46 | current_nodes.swap(parent_nodes); |
| 47 | parent_nodes.clear(); |
| 48 | current = ¤t_nodes; |
| 49 | } |
| 50 | DCHECK_EQ(1u, current->size()); |
| 51 | return (*current)[0]; |
| 52 | } |
| 53 | |
| 54 | } // namespace extensions |