| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |
| 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 #include <openssl/sha.h> | 10 #include <openssl/sha.h> |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 | 13 |
| 14 // Omaha uses base64 encoded SHA-1 as the hash. This class provides a simple | 14 // Omaha uses base64 encoded SHA-256 as the hash. This class provides a simple |
| 15 // wrapper around OpenSSL providing such a formatted hash of data passed in. | 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: | 16 // The methods of this class must be called in a very specific order: First the |
| 17 // First the ctor (of course), then 0 or more calls to Update(), then | 17 // ctor (of course), then 0 or more calls to Update(), then Finalize(), then 0 |
| 18 // Finalize(), then 0 or more calls to hash(). | 18 // or more calls to hash(). |
| 19 | 19 |
| 20 namespace chromeos_update_engine { | 20 namespace chromeos_update_engine { |
| 21 | 21 |
| 22 class OmahaHashCalculator { | 22 class OmahaHashCalculator { |
| 23 public: | 23 public: |
| 24 OmahaHashCalculator(); | 24 OmahaHashCalculator(); |
| 25 | 25 |
| 26 // Update is called with all of the data that should be hashed in order. | 26 // Update is called with all of the data that should be hashed in order. |
| 27 // Update will read |length| bytes of |data|. | 27 // Update will read |length| bytes of |data|. |
| 28 // Returns true on success. | 28 // Returns true on success. |
| 29 bool Update(const char* data, size_t length); | 29 bool Update(const char* data, size_t length); |
| 30 | 30 |
| 31 // Call Finalize() when all data has been passed in. This method tells | 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 | 32 // OpenSSl that no more data will come in and base64 encodes the resulting |
| 33 // hash. | 33 // hash. |
| 34 // Returns true on success. | 34 // Returns true on success. |
| 35 bool Finalize(); | 35 bool Finalize(); |
| 36 | 36 |
| 37 // Gets the hash. Finalize() must have been called. | 37 // Gets the hash. Finalize() must have been called. |
| 38 const std::string& hash() const { | 38 const std::string& hash() const { |
| 39 DCHECK(!hash_.empty()) << "Call Finalize() first"; | 39 DCHECK(!hash_.empty()) << "Call Finalize() first"; |
| 40 return hash_; | 40 return hash_; |
| 41 } | 41 } |
| 42 | 42 |
| 43 const std::vector<char>& raw_hash() const { | 43 const std::vector<char>& raw_hash() const { |
| 44 DCHECK(!raw_hash_.empty()) << "Call Finalize() first"; | 44 DCHECK(!raw_hash_.empty()) << "Call Finalize() first"; |
| 45 return raw_hash_; | 45 return raw_hash_; |
| 46 } | 46 } |
| 47 | 47 |
| 48 // Gets the current hash context. Note that the string will contain binary |
| 49 // data (including \0 characters). |
| 50 std::string GetContext() const; |
| 51 |
| 52 // Sets the current hash context. |context| must the string returned by a |
| 53 // previous OmahaHashCalculator::GetContext method call. Returns true on |
| 54 // success, and false otherwise. |
| 55 bool SetContext(const std::string& context); |
| 56 |
| 48 static bool RawHashOfData(const std::vector<char>& data, | 57 static bool RawHashOfData(const std::vector<char>& data, |
| 49 std::vector<char>* out_hash); | 58 std::vector<char>* out_hash); |
| 50 | 59 |
| 51 // Used by tests | 60 // Used by tests |
| 52 static std::string OmahaHashOfBytes(const void* data, size_t length); | 61 static std::string OmahaHashOfBytes(const void* data, size_t length); |
| 53 static std::string OmahaHashOfString(const std::string& str); | 62 static std::string OmahaHashOfString(const std::string& str); |
| 54 static std::string OmahaHashOfData(const std::vector<char>& data); | 63 static std::string OmahaHashOfData(const std::vector<char>& data); |
| 55 | 64 |
| 56 private: | 65 private: |
| 57 // If non-empty, the final base64 encoded hash and the raw hash. Will only be | 66 // If non-empty, the final base64 encoded hash and the raw hash. Will only be |
| 58 // set to non-empty when Finalize is called. | 67 // set to non-empty when Finalize is called. |
| 59 std::string hash_; | 68 std::string hash_; |
| 60 std::vector<char> raw_hash_; | 69 std::vector<char> raw_hash_; |
| 61 | 70 |
| 62 // Init success | 71 // Init success |
| 63 bool valid_; | 72 bool valid_; |
| 64 | 73 |
| 65 // The hash state used by OpenSSL | 74 // The hash state used by OpenSSL |
| 66 SHA256_CTX ctx_; | 75 SHA256_CTX ctx_; |
| 67 DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator); | 76 DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator); |
| 68 }; | 77 }; |
| 69 | 78 |
| 70 } // namespace chromeos_update_engine | 79 } // namespace chromeos_update_engine |
| 71 | 80 |
| 72 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ | 81 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |
| OLD | NEW |