blob: 208fd01e6acddf2629c6d96b6795db9ca68cf1e5 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// Copyright (c) 2009 The Chromium OS 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
adlr@google.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +00009#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000010#include <openssl/sha.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070011#include "base/basictypes.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070012#include "base/logging.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000013
14// Omaha uses base64 encoded SHA-1 as the hash. This class provides a simple
15// wrapper around OpenSSL providing such a formatted hash of data passed in.
16// The methods of this class must be called in a very specific order:
17// First the ctor (of course), then 0 or more calls to Update(), then
18// Finalize(), then 0 or more calls to hash().
19
20namespace chromeos_update_engine {
21
22class OmahaHashCalculator {
23 public:
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070024 OmahaHashCalculator();
rspangler@google.com49fdf182009-10-10 00:57:34 +000025
26 // Update is called with all of the data that should be hashed in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070027 // Update will read |length| bytes of |data|.
28 // Returns true on success.
29 bool Update(const char* data, size_t length);
rspangler@google.com49fdf182009-10-10 00:57:34 +000030
31 // Call Finalize() when all data has been passed in. This method tells
32 // OpenSSl that no more data will come in and base64 encodes the resulting
33 // hash.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070034 // Returns true on success.
35 bool Finalize();
rspangler@google.com49fdf182009-10-10 00:57:34 +000036
37 // Gets the hash. Finalize() must have been called.
38 const std::string& hash() const {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070039 DCHECK(!hash_.empty()) << "Call Finalize() first";
rspangler@google.com49fdf182009-10-10 00:57:34 +000040 return hash_;
41 }
42
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070043 static bool RawHashOfData(const std::vector<char>& data,
44 std::vector<char>* out_hash);
45
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 // Used by tests
47 static std::string OmahaHashOfBytes(const void* data, size_t length);
48 static std::string OmahaHashOfString(const std::string& str);
49 static std::string OmahaHashOfData(const std::vector<char>& data);
50
51 private:
52 // If non-empty, the final base64 encoded hash. Will only be set to
53 // non-empty when Finalize is called.
54 std::string hash_;
55
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070056 // Init success
57 bool valid_;
58
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 // The hash state used by OpenSSL
Darin Petkovd22cb292010-09-29 10:02:29 -070060 SHA256_CTX ctx_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator);
62};
63
64} // namespace chromeos_update_engine
65
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070066#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__