| 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 #include "update_engine/omaha_hash_calculator.h" | 5 #include "update_engine/omaha_hash_calculator.h" |
| 6 | 6 |
| 7 #include <openssl/bio.h> | 7 #include <openssl/bio.h> |
| 8 #include <openssl/buffer.h> | 8 #include <openssl/buffer.h> |
| 9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 length_param_may_be_truncated_in_SHA256_Update); | 29 length_param_may_be_truncated_in_SHA256_Update); |
| 30 TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1); | 30 TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1); |
| 31 return true; | 31 return true; |
| 32 } | 32 } |
| 33 | 33 |
| 34 // Call Finalize() when all data has been passed in. This mostly just | 34 // Call Finalize() when all data has been passed in. This mostly just |
| 35 // calls OpenSSL's SHA256_Final() and then base64 encodes the hash. | 35 // calls OpenSSL's SHA256_Final() and then base64 encodes the hash. |
| 36 bool OmahaHashCalculator::Finalize() { | 36 bool OmahaHashCalculator::Finalize() { |
| 37 bool success = true; | 37 bool success = true; |
| 38 TEST_AND_RETURN_FALSE(hash_.empty()); | 38 TEST_AND_RETURN_FALSE(hash_.empty()); |
| 39 unsigned char md[SHA256_DIGEST_LENGTH]; | 39 TEST_AND_RETURN_FALSE(raw_hash_.empty()); |
| 40 TEST_AND_RETURN_FALSE(SHA256_Final(md, &ctx_) == 1); | 40 raw_hash_.resize(SHA256_DIGEST_LENGTH); |
| 41 TEST_AND_RETURN_FALSE( |
| 42 SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]), |
| 43 &ctx_) == 1); |
| 41 | 44 |
| 42 // Convert md to base64 encoding and store it in hash_ | 45 // Convert raw_hash_ to base64 encoding and store it in hash_. |
| 43 BIO *b64 = BIO_new(BIO_f_base64()); | 46 BIO *b64 = BIO_new(BIO_f_base64()); |
| 44 if (!b64) | 47 if (!b64) |
| 45 LOG(INFO) << "BIO_new(BIO_f_base64()) failed"; | 48 LOG(INFO) << "BIO_new(BIO_f_base64()) failed"; |
| 46 BIO *bmem = BIO_new(BIO_s_mem()); | 49 BIO *bmem = BIO_new(BIO_s_mem()); |
| 47 if (!bmem) | 50 if (!bmem) |
| 48 LOG(INFO) << "BIO_new(BIO_s_mem()) failed"; | 51 LOG(INFO) << "BIO_new(BIO_s_mem()) failed"; |
| 49 if (b64 && bmem) { | 52 if (b64 && bmem) { |
| 50 b64 = BIO_push(b64, bmem); | 53 b64 = BIO_push(b64, bmem); |
| 51 success = (BIO_write(b64, md, sizeof(md)) == sizeof(md)); | 54 success = |
| 55 (BIO_write(b64, &raw_hash_[0], raw_hash_.size()) == |
| 56 static_cast<int>(raw_hash_.size())); |
| 52 if (success) | 57 if (success) |
| 53 success = (BIO_flush(b64) == 1); | 58 success = (BIO_flush(b64) == 1); |
| 54 | 59 |
| 55 BUF_MEM *bptr = NULL; | 60 BUF_MEM *bptr = NULL; |
| 56 BIO_get_mem_ptr(b64, &bptr); | 61 BIO_get_mem_ptr(b64, &bptr); |
| 57 hash_.assign(bptr->data, bptr->length - 1); | 62 hash_.assign(bptr->data, bptr->length - 1); |
| 58 } | 63 } |
| 59 if (b64) { | 64 if (b64) { |
| 60 BIO_free_all(b64); | 65 BIO_free_all(b64); |
| 61 b64 = NULL; | 66 b64 = NULL; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 86 | 91 |
| 87 string OmahaHashCalculator::OmahaHashOfString(const string& str) { | 92 string OmahaHashCalculator::OmahaHashOfString(const string& str) { |
| 88 return OmahaHashOfBytes(str.data(), str.size()); | 93 return OmahaHashOfBytes(str.data(), str.size()); |
| 89 } | 94 } |
| 90 | 95 |
| 91 string OmahaHashCalculator::OmahaHashOfData(const vector<char>& data) { | 96 string OmahaHashCalculator::OmahaHashOfData(const vector<char>& data) { |
| 92 return OmahaHashOfBytes(&data[0], data.size()); | 97 return OmahaHashOfBytes(&data[0], data.size()); |
| 93 } | 98 } |
| 94 | 99 |
| 95 } // namespace chromeos_update_engine | 100 } // namespace chromeos_update_engine |
| OLD | NEW |