blob: 64b83208308bf3373bafc35abbc85a0da01586c1 [file] [log] [blame]
[email protected]abd4cb22014-05-16 05:22:561// 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#ifndef EXTENSIONS_BROWSER_COMPUTED_HASHES_H_
6#define EXTENSIONS_BROWSER_COMPUTED_HASHES_H_
7
avic9cec102015-12-23 00:39:268#include <stddef.h>
9
[email protected]abd4cb22014-05-16 05:22:5610#include <map>
dchengf5d241082016-04-21 03:43:1111#include <memory>
[email protected]abd4cb22014-05-16 05:22:5612#include <string>
13#include <vector>
14
[email protected]abd4cb22014-05-16 05:22:5615
16namespace base {
17class FilePath;
[email protected]de00aeb2014-08-06 09:13:3918class ListValue;
[email protected]abd4cb22014-05-16 05:22:5619}
20
21namespace extensions {
22
23// A pair of classes for serialization of a set of SHA256 block hashes computed
24// over the files inside an extension.
25class ComputedHashes {
26 public:
27 class Reader {
28 public:
29 Reader();
30 ~Reader();
31 bool InitFromFile(const base::FilePath& path);
32
33 // The block size and hashes for |relative_path| will be copied into the
34 // out parameters.
35 bool GetHashes(const base::FilePath& relative_path,
36 int* block_size,
Istiaque Ahmed9bdd9d92017-12-16 04:53:2737 std::vector<std::string>* hashes) const;
[email protected]abd4cb22014-05-16 05:22:5638
39 private:
40 typedef std::pair<int, std::vector<std::string> > HashInfo;
41
42 // This maps a relative path to a pair of (block size, hashes)
43 std::map<base::FilePath, HashInfo> data_;
44 };
45
46 class Writer {
47 public:
48 Writer();
49 ~Writer();
50
51 // Adds hashes for |relative_path|. Should not be called more than once
52 // for a given |relative_path|.
53 void AddHashes(const base::FilePath& relative_path,
54 int block_size,
55 const std::vector<std::string>& hashes);
56
57 bool WriteToFile(const base::FilePath& path);
58
59 private:
[email protected]de00aeb2014-08-06 09:13:3960 // Each element of this list contains the path and block hashes for one
61 // file.
dchengf5d241082016-04-21 03:43:1162 std::unique_ptr<base::ListValue> file_list_;
[email protected]abd4cb22014-05-16 05:22:5663 };
[email protected]de00aeb2014-08-06 09:13:3964
65 // Computes the SHA256 hash of each |block_size| chunk in |contents|, placing
66 // the results into |hashes|.
67 static void ComputeHashesForContent(const std::string& contents,
68 size_t block_size,
69 std::vector<std::string>* hashes);
[email protected]abd4cb22014-05-16 05:22:5670};
71
72} // namespace extensions
73
74#endif // EXTENSIONS_BROWSER_COMPUTED_HASHES_H_