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 <fcntl.h> | 7 #include <fcntl.h> |
8 | 8 |
9 #include <base/eintr_wrapper.h> | 9 #include <base/eintr_wrapper.h> |
10 #include <base/logging.h> | 10 #include <base/logging.h> |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 if (rc < 0 || !Update(buffer.data(), rc)) { | 56 if (rc < 0 || !Update(buffer.data(), rc)) { |
57 bytes_processed = -1; | 57 bytes_processed = -1; |
58 break; | 58 break; |
59 } | 59 } |
60 bytes_processed += rc; | 60 bytes_processed += rc; |
61 } | 61 } |
62 HANDLE_EINTR(close(fd)); | 62 HANDLE_EINTR(close(fd)); |
63 return bytes_processed; | 63 return bytes_processed; |
64 } | 64 } |
65 | 65 |
66 // Call Finalize() when all data has been passed in. This mostly just | 66 bool OmahaHashCalculator::Base64Encode(const void* data, |
67 // calls OpenSSL's SHA256_Final() and then base64 encodes the hash. | 67 size_t size, |
68 bool OmahaHashCalculator::Finalize() { | 68 string* out) { |
69 bool success = true; | 69 bool success = true; |
70 TEST_AND_RETURN_FALSE(hash_.empty()); | |
71 TEST_AND_RETURN_FALSE(raw_hash_.empty()); | |
72 raw_hash_.resize(SHA256_DIGEST_LENGTH); | |
73 TEST_AND_RETURN_FALSE( | |
74 SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]), | |
75 &ctx_) == 1); | |
76 | |
77 // Convert raw_hash_ to base64 encoding and store it in hash_. | |
78 BIO *b64 = BIO_new(BIO_f_base64()); | 70 BIO *b64 = BIO_new(BIO_f_base64()); |
79 if (!b64) | 71 if (!b64) |
80 LOG(INFO) << "BIO_new(BIO_f_base64()) failed"; | 72 LOG(INFO) << "BIO_new(BIO_f_base64()) failed"; |
81 BIO *bmem = BIO_new(BIO_s_mem()); | 73 BIO *bmem = BIO_new(BIO_s_mem()); |
82 if (!bmem) | 74 if (!bmem) |
83 LOG(INFO) << "BIO_new(BIO_s_mem()) failed"; | 75 LOG(INFO) << "BIO_new(BIO_s_mem()) failed"; |
84 if (b64 && bmem) { | 76 if (b64 && bmem) { |
85 b64 = BIO_push(b64, bmem); | 77 b64 = BIO_push(b64, bmem); |
86 success = | 78 success = |
87 (BIO_write(b64, &raw_hash_[0], raw_hash_.size()) == | 79 (BIO_write(b64, data, size) == static_cast<int>(size)); |
88 static_cast<int>(raw_hash_.size())); | |
89 if (success) | 80 if (success) |
90 success = (BIO_flush(b64) == 1); | 81 success = (BIO_flush(b64) == 1); |
91 | 82 |
92 BUF_MEM *bptr = NULL; | 83 BUF_MEM *bptr = NULL; |
93 BIO_get_mem_ptr(b64, &bptr); | 84 BIO_get_mem_ptr(b64, &bptr); |
94 hash_.assign(bptr->data, bptr->length - 1); | 85 out->assign(bptr->data, bptr->length - 1); |
95 } | 86 } |
96 if (b64) { | 87 if (b64) { |
97 BIO_free_all(b64); | 88 BIO_free_all(b64); |
98 b64 = NULL; | 89 b64 = NULL; |
99 } | 90 } |
100 return success; | 91 return success; |
101 } | 92 } |
102 | 93 |
94 // Call Finalize() when all data has been passed in. This mostly just | |
95 // calls OpenSSL's SHA256_Final() and then base64 encodes the hash. | |
96 bool OmahaHashCalculator::Finalize() { | |
97 TEST_AND_RETURN_FALSE(hash_.empty()); | |
98 TEST_AND_RETURN_FALSE(raw_hash_.empty()); | |
99 raw_hash_.resize(SHA256_DIGEST_LENGTH); | |
100 TEST_AND_RETURN_FALSE( | |
101 SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]), | |
102 &ctx_) == 1); | |
103 | |
104 // Convert raw_hash_ to base64 encoding and store it in hash_. | |
105 return OmahaHashCalculator::Base64Encode(&raw_hash_[0], | |
petkov
2010/10/22 20:15:16
You don't need OmahaHashCalculator:: I think.
adlr
2010/10/22 20:40:18
Done.
| |
106 raw_hash_.size(), | |
107 &hash_);; | |
108 } | |
109 | |
103 bool OmahaHashCalculator::RawHashOfData(const vector<char>& data, | 110 bool OmahaHashCalculator::RawHashOfData(const vector<char>& data, |
104 vector<char>* out_hash) { | 111 vector<char>* out_hash) { |
105 OmahaHashCalculator calc; | 112 OmahaHashCalculator calc; |
106 calc.Update(&data[0], data.size()); | 113 calc.Update(&data[0], data.size()); |
107 | 114 |
108 out_hash->resize(out_hash->size() + SHA256_DIGEST_LENGTH); | 115 out_hash->resize(out_hash->size() + SHA256_DIGEST_LENGTH); |
109 TEST_AND_RETURN_FALSE( | 116 TEST_AND_RETURN_FALSE( |
110 SHA256_Final(reinterpret_cast<unsigned char*>(&(*(out_hash->end() - | 117 SHA256_Final(reinterpret_cast<unsigned char*>(&(*(out_hash->end() - |
111 SHA256_DIGEST_LENGTH))), | 118 SHA256_DIGEST_LENGTH))), |
112 &calc.ctx_) == 1); | 119 &calc.ctx_) == 1); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_)); | 154 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_)); |
148 } | 155 } |
149 | 156 |
150 bool OmahaHashCalculator::SetContext(const std::string& context) { | 157 bool OmahaHashCalculator::SetContext(const std::string& context) { |
151 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_)); | 158 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_)); |
152 memcpy(&ctx_, context.data(), sizeof(ctx_)); | 159 memcpy(&ctx_, context.data(), sizeof(ctx_)); |
153 return true; | 160 return true; |
154 } | 161 } |
155 | 162 |
156 } // namespace chromeos_update_engine | 163 } // namespace chromeos_update_engine |
OLD | NEW |