Move file hashing into its own class, Hash.

Test: hidl output remains the same.
Bug: 34178341
Merged-In: Ic8c5de61ebba3bd556d343c4f2c12af4bafb8d6b
Change-Id: Ic8c5de61ebba3bd556d343c4f2c12af4bafb8d6b
diff --git a/Interface.cpp b/Interface.cpp
index d326703..99c13e1 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -20,6 +20,7 @@
 #include "ArrayType.h"
 #include "ConstantExpression.h"
 #include "DeathRecipientType.h"
+#include "Hash.h"
 #include "Method.h"
 #include "ScalarType.h"
 #include "StringType.h"
@@ -27,14 +28,12 @@
 
 #include <unistd.h>
 
-#include <fstream>
 #include <iostream>
 #include <sstream>
 
 #include <android-base/logging.h>
 #include <hidl-util/Formatter.h>
 #include <hidl-util/StringHelper.h>
-#include <openssl/sha.h>
 
 namespace android {
 
@@ -288,36 +287,22 @@
     return true;
 }
 
-static void sha256File(const std::string &path, uint8_t *outDigest) {
-    std::ifstream stream(path);
-    std::stringstream fileStream;
-    fileStream << stream.rdbuf();
-    std::string fileContent = fileStream.str();
-    SHA256(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
-            fileContent.size(), outDigest);
-}
-
 static void emitDigestChain(
         Formatter &out,
         const std::string &prefix,
         const std::vector<const Interface *> &chain,
         std::function<std::string(const ConstantExpression &)> byteToString) {
     out.join(chain.begin(), chain.end(), ",\n", [&] (const auto &iface) {
-        const std::string &filename = iface->location().begin().filename();
-        uint8_t digest[SHA256_DIGEST_LENGTH];
-        sha256File(filename, digest);
+        const Hash &hash = Hash::getHash(iface->location().begin().filename());
         out << prefix;
         out << "{";
-        out.join(digest, digest + SHA256_DIGEST_LENGTH, ",", [&](const auto &e) {
+        out.join(hash.raw().begin(), hash.raw().end(), ",", [&](const auto &e) {
             // Use ConstantExpression::cppValue / javaValue
             // because Java used signed byte for uint8_t.
             out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
         });
         out << "} /* ";
-        out.join(digest, digest + SHA256_DIGEST_LENGTH, "", [&](const auto &e) {
-            static const char hexes[] = "0123456789abcdef";
-            out << hexes[e >> 4] << hexes[e & 0xF];
-        });
+        out << hash.hexString();
         out << " */";
     });
 }