blob: c96f75ecde36dfc789be626b1e985b7324dd77ca [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
8#include "base/basictypes.h"
9#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000010#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000011#include <openssl/sha.h>
12
13// Omaha uses base64 encoded SHA-1 as the hash. This class provides a simple
14// wrapper around OpenSSL providing such a formatted hash of data passed in.
15// The methods of this class must be called in a very specific order:
16// First the ctor (of course), then 0 or more calls to Update(), then
17// Finalize(), then 0 or more calls to hash().
18
19namespace chromeos_update_engine {
20
21class OmahaHashCalculator {
22 public:
23 OmahaHashCalculator();
24
25 // Update is called with all of the data that should be hashed in order.
26 // Update will read |length| bytes of |data|
27 void Update(const char* data, size_t length);
28
29 // Call Finalize() when all data has been passed in. This method tells
30 // OpenSSl that no more data will come in and base64 encodes the resulting
31 // hash.
32 void Finalize();
33
34 // Gets the hash. Finalize() must have been called.
35 const std::string& hash() const {
36 CHECK(!hash_.empty()) << "Call Finalize() first";
37 return hash_;
38 }
39
40 // Used by tests
41 static std::string OmahaHashOfBytes(const void* data, size_t length);
42 static std::string OmahaHashOfString(const std::string& str);
43 static std::string OmahaHashOfData(const std::vector<char>& data);
44
45 private:
46 // If non-empty, the final base64 encoded hash. Will only be set to
47 // non-empty when Finalize is called.
48 std::string hash_;
49
50 // The hash state used by OpenSSL
51 SHA_CTX ctx_;
52 DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator);
53};
54
55} // namespace chromeos_update_engine
56
adlr@google.comc98a7ed2009-12-04 18:54:03 +000057#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__