rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 8 | #include <string> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 9 | #include <vector> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 10 | #include <openssl/sha.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 11 | #include "base/basictypes.h" |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 12 | #include "base/logging.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 13 | |
| 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 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | class OmahaHashCalculator { |
| 23 | public: |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 24 | OmahaHashCalculator(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 | |
| 26 | // Update is called with all of the data that should be hashed in order. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 27 | // Update will read |length| bytes of |data|. |
| 28 | // Returns true on success. |
| 29 | bool Update(const char* data, size_t length); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | |
| 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 Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 34 | // Returns true on success. |
| 35 | bool Finalize(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 36 | |
| 37 | // Gets the hash. Finalize() must have been called. |
| 38 | const std::string& hash() const { |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 39 | DCHECK(!hash_.empty()) << "Call Finalize() first"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 40 | return hash_; |
| 41 | } |
| 42 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame^] | 43 | const std::vector<char>& raw_hash() const { |
| 44 | DCHECK(!raw_hash_.empty()) << "Call Finalize() first"; |
| 45 | return raw_hash_; |
| 46 | } |
| 47 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 48 | static bool RawHashOfData(const std::vector<char>& data, |
| 49 | std::vector<char>* out_hash); |
| 50 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 51 | // Used by tests |
| 52 | static std::string OmahaHashOfBytes(const void* data, size_t length); |
| 53 | static std::string OmahaHashOfString(const std::string& str); |
| 54 | static std::string OmahaHashOfData(const std::vector<char>& data); |
| 55 | |
| 56 | private: |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame^] | 57 | // If non-empty, the final base64 encoded hash and the raw hash. Will only be |
| 58 | // set to non-empty when Finalize is called. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 59 | std::string hash_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame^] | 60 | std::vector<char> raw_hash_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 62 | // Init success |
| 63 | bool valid_; |
| 64 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 65 | // The hash state used by OpenSSL |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 66 | SHA256_CTX ctx_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 67 | DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator); |
| 68 | }; |
| 69 | |
| 70 | } // namespace chromeos_update_engine |
| 71 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 72 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |