AU: When applying delta locally, get source checksums

When applying a delta on the host, we need to calculate the original
checksums for the rootfs/kernel for the delta performer. This CL does
that.

Also:
- Log old/new checksums in performer
- Add base64 encode function to OmahaHashCalculator

BUG=8062
TEST=unittests; applied delta on host

Review URL: http://codereview.chromium.org/4042004

Change-Id: I953d7dd34cb65269e9b44e22c87a13a7f52e66d4
diff --git a/omaha_hash_calculator.cc b/omaha_hash_calculator.cc
index ac4edf4..cf583a9 100644
--- a/omaha_hash_calculator.cc
+++ b/omaha_hash_calculator.cc
@@ -63,18 +63,10 @@
   return bytes_processed;
 }
 
-// Call Finalize() when all data has been passed in. This mostly just
-// calls OpenSSL's SHA256_Final() and then base64 encodes the hash.
-bool OmahaHashCalculator::Finalize() {
+bool OmahaHashCalculator::Base64Encode(const void* data,
+                                       size_t size,
+                                       string* out) {
   bool success = true;
-  TEST_AND_RETURN_FALSE(hash_.empty());
-  TEST_AND_RETURN_FALSE(raw_hash_.empty());
-  raw_hash_.resize(SHA256_DIGEST_LENGTH);
-  TEST_AND_RETURN_FALSE(
-      SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]),
-                   &ctx_) == 1);
-
-  // Convert raw_hash_ to base64 encoding and store it in hash_.
   BIO *b64 = BIO_new(BIO_f_base64());
   if (!b64)
     LOG(INFO) << "BIO_new(BIO_f_base64()) failed";
@@ -84,14 +76,13 @@
   if (b64 && bmem) {
     b64 = BIO_push(b64, bmem);
     success =
-        (BIO_write(b64, &raw_hash_[0], raw_hash_.size()) ==
-         static_cast<int>(raw_hash_.size()));
+        (BIO_write(b64, data, size) == static_cast<int>(size));
     if (success)
       success = (BIO_flush(b64) == 1);
 
     BUF_MEM *bptr = NULL;
     BIO_get_mem_ptr(b64, &bptr);
-    hash_.assign(bptr->data, bptr->length - 1);
+    out->assign(bptr->data, bptr->length - 1);
   }
   if (b64) {
     BIO_free_all(b64);
@@ -100,6 +91,20 @@
   return success;
 }
 
+// Call Finalize() when all data has been passed in. This mostly just
+// calls OpenSSL's SHA256_Final() and then base64 encodes the hash.
+bool OmahaHashCalculator::Finalize() {
+  TEST_AND_RETURN_FALSE(hash_.empty());
+  TEST_AND_RETURN_FALSE(raw_hash_.empty());
+  raw_hash_.resize(SHA256_DIGEST_LENGTH);
+  TEST_AND_RETURN_FALSE(
+      SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]),
+                   &ctx_) == 1);
+
+  // Convert raw_hash_ to base64 encoding and store it in hash_.
+  return Base64Encode(&raw_hash_[0], raw_hash_.size(), &hash_);;
+}
+
 bool OmahaHashCalculator::RawHashOfData(const vector<char>& data,
                                         vector<char>* out_hash) {
   OmahaHashCalculator calc;