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 <openssl/bio.h> | 5 #include <openssl/bio.h> |
6 #include <openssl/buffer.h> | 6 #include <openssl/buffer.h> |
7 #include <openssl/evp.h> | 7 #include <openssl/evp.h> |
8 #include "base/logging.h" | 8 #include "chromeos/obsolete_logging.h" |
9 #include "update_engine/omaha_hash_calculator.h" | 9 #include "update_engine/omaha_hash_calculator.h" |
10 | 10 |
11 namespace chromeos_update_engine { | 11 namespace chromeos_update_engine { |
12 | 12 |
13 OmahaHashCalculator::OmahaHashCalculator() { | 13 OmahaHashCalculator::OmahaHashCalculator() { |
14 CHECK_EQ(1, SHA1_Init(&ctx_)); | 14 CHECK_EQ(SHA1_Init(&ctx_), 1); |
15 } | 15 } |
16 | 16 |
17 // Update is called with all of the data that should be hashed in order. | 17 // Update is called with all of the data that should be hashed in order. |
18 // Mostly just passes the data through to OpenSSL's SHA1_Update() | 18 // Mostly just passes the data through to OpenSSL's SHA1_Update() |
19 void OmahaHashCalculator::Update(const char* data, size_t length) { | 19 void OmahaHashCalculator::Update(const char* data, size_t length) { |
20 CHECK(hash_.empty()) << "Can't Update after hash is finalized"; | 20 CHECK(hash_.empty()) << "Can't Update after hash is finalized"; |
21 COMPILE_ASSERT(sizeof(size_t) <= sizeof(unsigned long), | 21 COMPILE_ASSERT(sizeof(size_t) <= sizeof(unsigned long), |
22 length_param_may_be_truncated_in_SHA1_Update); | 22 length_param_may_be_truncated_in_SHA1_Update); |
23 CHECK_EQ(1, SHA1_Update(&ctx_, data, length)); | 23 CHECK_EQ(SHA1_Update(&ctx_, data, length), 1); |
24 } | 24 } |
25 | 25 |
26 // Call Finalize() when all data has been passed in. This mostly just | 26 // Call Finalize() when all data has been passed in. This mostly just |
27 // calls OpenSSL's SHA1_Final() and then base64 encodes the hash. | 27 // calls OpenSSL's SHA1_Final() and then base64 encodes the hash. |
28 void OmahaHashCalculator::Finalize() { | 28 void OmahaHashCalculator::Finalize() { |
29 CHECK(hash_.empty()) << "Don't call Finalize() twice"; | 29 CHECK(hash_.empty()) << "Don't call Finalize() twice"; |
30 unsigned char md[SHA_DIGEST_LENGTH]; | 30 unsigned char md[SHA_DIGEST_LENGTH]; |
31 CHECK_EQ(1, SHA1_Final(md, &ctx_)); | 31 CHECK_EQ(SHA1_Final(md, &ctx_), 1); |
32 | 32 |
33 // Convert md to base64 encoding and store it in hash_ | 33 // Convert md to base64 encoding and store it in hash_ |
34 BIO *b64 = BIO_new(BIO_f_base64()); | 34 BIO *b64 = BIO_new(BIO_f_base64()); |
35 CHECK(b64); | 35 CHECK(b64); |
36 BIO *bmem = BIO_new(BIO_s_mem()); | 36 BIO *bmem = BIO_new(BIO_s_mem()); |
37 CHECK(bmem); | 37 CHECK(bmem); |
38 b64 = BIO_push(b64, bmem); | 38 b64 = BIO_push(b64, bmem); |
39 CHECK_EQ(sizeof(md), BIO_write(b64, md, sizeof(md))); | 39 CHECK_EQ(BIO_write(b64, md, sizeof(md)), sizeof(md)); |
40 CHECK_EQ(1, BIO_flush(b64)); | 40 CHECK_EQ(BIO_flush(b64), 1); |
41 | 41 |
42 BUF_MEM *bptr = NULL; | 42 BUF_MEM *bptr = NULL; |
43 BIO_get_mem_ptr(b64, &bptr); | 43 BIO_get_mem_ptr(b64, &bptr); |
44 hash_.assign(bptr->data, bptr->length - 1); | 44 hash_.assign(bptr->data, bptr->length - 1); |
45 | 45 |
46 BIO_free_all(b64); | 46 BIO_free_all(b64); |
47 } | 47 } |
48 | 48 |
49 std::string OmahaHashCalculator::OmahaHashOfBytes( | 49 std::string OmahaHashCalculator::OmahaHashOfBytes( |
50 const void* data, size_t length) { | 50 const void* data, size_t length) { |
51 OmahaHashCalculator calc; | 51 OmahaHashCalculator calc; |
52 calc.Update(reinterpret_cast<const char*>(data), length); | 52 calc.Update(reinterpret_cast<const char*>(data), length); |
53 calc.Finalize(); | 53 calc.Finalize(); |
54 return calc.hash(); | 54 return calc.hash(); |
55 } | 55 } |
56 | 56 |
57 std::string OmahaHashCalculator::OmahaHashOfString( | 57 std::string OmahaHashCalculator::OmahaHashOfString( |
58 const std::string& str) { | 58 const std::string& str) { |
59 return OmahaHashOfBytes(str.data(), str.size()); | 59 return OmahaHashOfBytes(str.data(), str.size()); |
60 } | 60 } |
61 | 61 |
62 std::string OmahaHashCalculator::OmahaHashOfData( | 62 std::string OmahaHashCalculator::OmahaHashOfData( |
63 const std::vector<char>& data) { | 63 const std::vector<char>& data) { |
64 return OmahaHashOfBytes(&data[0], data.size()); | 64 return OmahaHashOfBytes(&data[0], data.size()); |
65 } | 65 } |
66 | 66 |
67 } // namespace chromeos_update_engine | 67 } // namespace chromeos_update_engine |
OLD | NEW |