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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 calc.Update(&data[0], data.size()); | 106 calc.Update(&data[0], data.size()); |
107 | 107 |
108 out_hash->resize(out_hash->size() + SHA256_DIGEST_LENGTH); | 108 out_hash->resize(out_hash->size() + SHA256_DIGEST_LENGTH); |
109 TEST_AND_RETURN_FALSE( | 109 TEST_AND_RETURN_FALSE( |
110 SHA256_Final(reinterpret_cast<unsigned char*>(&(*(out_hash->end() - | 110 SHA256_Final(reinterpret_cast<unsigned char*>(&(*(out_hash->end() - |
111 SHA256_DIGEST_LENGTH))), | 111 SHA256_DIGEST_LENGTH))), |
112 &calc.ctx_) == 1); | 112 &calc.ctx_) == 1); |
113 return true; | 113 return true; |
114 } | 114 } |
115 | 115 |
| 116 off_t OmahaHashCalculator::RawHashOfFile(const std::string& name, off_t length, |
| 117 std::vector<char>* out_hash) { |
| 118 OmahaHashCalculator calc; |
| 119 off_t res = calc.UpdateFile(name, length); |
| 120 if (res < 0) { |
| 121 return res; |
| 122 } |
| 123 if (!calc.Finalize()) { |
| 124 return -1; |
| 125 } |
| 126 *out_hash = calc.raw_hash(); |
| 127 return res; |
| 128 } |
| 129 |
116 string OmahaHashCalculator::OmahaHashOfBytes( | 130 string OmahaHashCalculator::OmahaHashOfBytes( |
117 const void* data, size_t length) { | 131 const void* data, size_t length) { |
118 OmahaHashCalculator calc; | 132 OmahaHashCalculator calc; |
119 calc.Update(reinterpret_cast<const char*>(data), length); | 133 calc.Update(reinterpret_cast<const char*>(data), length); |
120 calc.Finalize(); | 134 calc.Finalize(); |
121 return calc.hash(); | 135 return calc.hash(); |
122 } | 136 } |
123 | 137 |
124 string OmahaHashCalculator::OmahaHashOfString(const string& str) { | 138 string OmahaHashCalculator::OmahaHashOfString(const string& str) { |
125 return OmahaHashOfBytes(str.data(), str.size()); | 139 return OmahaHashOfBytes(str.data(), str.size()); |
126 } | 140 } |
127 | 141 |
128 string OmahaHashCalculator::OmahaHashOfData(const vector<char>& data) { | 142 string OmahaHashCalculator::OmahaHashOfData(const vector<char>& data) { |
129 return OmahaHashOfBytes(&data[0], data.size()); | 143 return OmahaHashOfBytes(&data[0], data.size()); |
130 } | 144 } |
131 | 145 |
132 string OmahaHashCalculator::GetContext() const { | 146 string OmahaHashCalculator::GetContext() const { |
133 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_)); | 147 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_)); |
134 } | 148 } |
135 | 149 |
136 bool OmahaHashCalculator::SetContext(const std::string& context) { | 150 bool OmahaHashCalculator::SetContext(const std::string& context) { |
137 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_)); | 151 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_)); |
138 memcpy(&ctx_, context.data(), sizeof(ctx_)); | 152 memcpy(&ctx_, context.data(), sizeof(ctx_)); |
139 return true; | 153 return true; |
140 } | 154 } |
141 | 155 |
142 } // namespace chromeos_update_engine | 156 } // namespace chromeos_update_engine |
OLD | NEW |