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

Side by Side Diff: extensions/browser/content_hash_tree.cc

Issue 274243004: Add code to compute the root hash for a hash tree (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed bugs and added tests Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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 "extensions/browser/content_hash_tree.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/stl_util.h"
9 #include "crypto/secure_hash.h"
10 #include "crypto/sha2.h"
11
12 namespace extensions {
13
14 std::string ComputeTreeHashRoot(const std::vector<std::string>& leaf_hashes,
15 int block_size) {
16 if (leaf_hashes.empty() || block_size % crypto::kSHA256Length != 0)
17 return std::string();
18
19 int children_per_parent = block_size / crypto::kSHA256Length;
20
21 // The nodes of the tree we're currently operating on.
22 std::vector<std::string> current_nodes;
23
24 // We avoid having to copy all of the input leaf nodes into |current_nodes|
25 // by using a pointer. So the first iteration of the loop this points at
Marijn Kruisselbrink 2014/05/12 17:53:45 You could just use a pair of iterators instead of
asargent_no_longer_on_chrome 2014/05/12 21:58:58 I switched to using an iterator instead of an inde
26 // |leaf_hashes|, but thereafter it points at |current_nodes|.
27 const std::vector<std::string>* current = &leaf_hashes;
28
29 // Where we're inserting new hashes computed from the current level.
30 std::vector<std::string> parent_nodes;
31
32 while (current->size() > 1) {
33 // Iterate over the current level of hashes, computing the hash of up to
34 // |children_per_parent| elements to form the hash of each parent node.
35 size_t i = 0;
36 while (i < current->size()) {
37 scoped_ptr<crypto::SecureHash> hash(
38 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
39 int count = 0;
Marijn Kruisselbrink 2014/05/12 17:53:45 What is count for? You don't seem to ever read its
asargent_no_longer_on_chrome 2014/05/12 21:58:58 Good catch - probably left over from some printf-s
40 for (int j = 0; j < children_per_parent && i < current->size(); j++) {
41 hash->Update((*current)[i].data(), (*current)[i].size());
42 i++;
43 count++;
44 }
45 parent_nodes.push_back(std::string(crypto::kSHA256Length, 0));
46 std::string* output = &(parent_nodes.back());
47 hash->Finish(string_as_array(output), output->size());
48 }
49 current_nodes.swap(parent_nodes);
50 parent_nodes.clear();
51 current = &current_nodes;
52 }
53 DCHECK_EQ(1u, current->size());
54 return (*current)[0];
55 }
56
57 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698