| 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 | 13 |
| 13 // Omaha uses base64 encoded SHA-1 as the hash. This class provides a simple | 14 // 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 // 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 // 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 // First the ctor (of course), then 0 or more calls to Update(), then |
| 17 // Finalize(), then 0 or more calls to hash(). | 18 // Finalize(), then 0 or more calls to hash(). |
| 18 | 19 |
| 19 namespace chromeos_update_engine { | 20 namespace chromeos_update_engine { |
| 20 | 21 |
| 21 class OmahaHashCalculator { | 22 class OmahaHashCalculator { |
| 22 public: | 23 public: |
| 23 OmahaHashCalculator(); | 24 OmahaHashCalculator(); |
| 24 | 25 |
| 25 // 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. |
| 26 // Update will read |length| bytes of |data| | 27 // Update will read |length| bytes of |data|. |
| 27 void Update(const char* data, size_t length); | 28 // Returns true on success. |
| 29 bool Update(const char* data, size_t length); |
| 28 | 30 |
| 29 // 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 |
| 30 // 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 |
| 31 // hash. | 33 // hash. |
| 32 void Finalize(); | 34 // Returns true on success. |
| 35 bool Finalize(); |
| 33 | 36 |
| 34 // Gets the hash. Finalize() must have been called. | 37 // Gets the hash. Finalize() must have been called. |
| 35 const std::string& hash() const { | 38 const std::string& hash() const { |
| 36 CHECK(!hash_.empty()) << "Call Finalize() first"; | 39 DCHECK(!hash_.empty()) << "Call Finalize() first"; |
| 37 return hash_; | 40 return hash_; |
| 38 } | 41 } |
| 39 | 42 |
| 43 static bool RawHashOfData(const std::vector<char>& data, |
| 44 std::vector<char>* out_hash); |
| 45 |
| 40 // Used by tests | 46 // Used by tests |
| 41 static std::string OmahaHashOfBytes(const void* data, size_t length); | 47 static std::string OmahaHashOfBytes(const void* data, size_t length); |
| 42 static std::string OmahaHashOfString(const std::string& str); | 48 static std::string OmahaHashOfString(const std::string& str); |
| 43 static std::string OmahaHashOfData(const std::vector<char>& data); | 49 static std::string OmahaHashOfData(const std::vector<char>& data); |
| 44 | 50 |
| 45 private: | 51 private: |
| 46 // If non-empty, the final base64 encoded hash. Will only be set to | 52 // If non-empty, the final base64 encoded hash. Will only be set to |
| 47 // non-empty when Finalize is called. | 53 // non-empty when Finalize is called. |
| 48 std::string hash_; | 54 std::string hash_; |
| 49 | 55 |
| 56 // Init success |
| 57 bool valid_; |
| 58 |
| 50 // The hash state used by OpenSSL | 59 // The hash state used by OpenSSL |
| 51 SHA_CTX ctx_; | 60 SHA_CTX ctx_; |
| 52 DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator); | 61 DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator); |
| 53 }; | 62 }; |
| 54 | 63 |
| 55 } // namespace chromeos_update_engine | 64 } // namespace chromeos_update_engine |
| 56 | 65 |
| 57 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ | 66 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |
| OLD | NEW |