twrpDigest refactor

This patch is to refactor twrpDigest using polymorphism
and inheritance to use the same call patterns for creating and
reading a digest. Now a library.
Use SHA2 from libcrypto. SHA2 is default if device has libcrypto.

Change string MD5 everywhere to use digest or Digest instead. Updated
string tags to digest. Translation will be required.

Switch out digest code into a driver class from partitionmanager.

SHA2 is better for digest creation due to decreased collision space
compared to MD5 and SHA1.

See https://en.wikipedia.org/wiki/SHA-2

Change-Id: I74b5546789990b12aa4ce2e389d25f80a3fe213f
diff --git a/adbbu/twrpback.cpp b/adbbu/twrpback.cpp
index 1c98a1b..8652de2 100644
--- a/adbbu/twrpback.cpp
+++ b/adbbu/twrpback.cpp
@@ -34,9 +34,8 @@
 
 #include "twadbstream.h"
 #include "twrpback.hpp"
-#include "../variables.h"
-#include "../twcommon.h"
-#include "../twrpDigest.hpp"
+#include "../twrpDigest/twrpDigest.hpp"
+#include "../twrpDigest/twrpMD5.hpp"
 
 twrpback::twrpback(void) {
 	read_fd = 0;
@@ -94,7 +93,7 @@
 }
 
 int twrpback::backup(std::string command) {
-	twrpDigest adb_md5;
+	twrpMD5 digest;
 	bool breakloop = false;
 	int bytes = 0, errctr = 0;
 	char result[MAX_ADB_READ];
@@ -215,8 +214,7 @@
 				struct twfilehdr twimghdr;
 
 				adblogwrite("Writing TWIMG\n");
-				adb_md5.initMD5();
-
+				digest.init();
 				memset(&twimghdr, 0, sizeof(twimghdr));
 				memcpy(&twimghdr, cmd, sizeof(cmd));
 				md5fnsize = twimghdr.size;
@@ -234,7 +232,7 @@
 				struct twfilehdr twfilehdr;
 
 				adblogwrite("Writing TWFN\n");
-				adb_md5.initMD5();
+				digest.init();
 
 				ADBSTRUCT_STATIC_ASSERT(sizeof(twfilehdr) == MAX_ADB_READ);
 
@@ -269,8 +267,7 @@
 					totalbytes += bytes;
 					char *writeresult = new char [bytes];
 					memcpy(writeresult, result, bytes);
-					if (adb_md5.updateMD5stream((unsigned char *) writeresult, bytes) == -1)
-						adblogwrite("failed to update md5 stream\n");
+					digest.update((unsigned char *) writeresult, bytes);
 					if (fwrite(writeresult, 1, bytes, adbd_fp) != bytes) {
 						adblogwrite("Error writing backup data to adbd\n");
 						close_backup_fds();
@@ -290,8 +287,7 @@
 						close_backup_fds();
 						return -1;
 					}
-					if (adb_md5.updateMD5stream((unsigned char *) padding, sizeof(padding)) == -1)
-						adblogwrite("failed to update md5 stream\n");
+					digest.update((unsigned char *) padding, sizeof(padding));
 					fflush(adbd_fp);
 					totalbytes = 0;
 				}
@@ -299,9 +295,8 @@
 				AdbBackupFileTrailer md5trailer;
 
 				memset(&md5trailer, 0, sizeof(md5trailer));
-				adb_md5.finalizeMD5stream();
 
-				std::string md5string = adb_md5.createMD5string();
+				std::string md5string = digest.return_digest_string();
 
 				strncpy(md5trailer.start_of_trailer, TWRP, sizeof(md5trailer.start_of_trailer));
 				strncpy(md5trailer.type, MD5TRAILER, sizeof(md5trailer.type));
@@ -350,8 +345,7 @@
 				char *writeresult = new char [bytes];
 				memcpy(writeresult, result, bytes);
 
-				if (adb_md5.updateMD5stream((unsigned char *) writeresult, bytes) == -1)
-					adblogwrite("failed to update md5 stream\n");
+				digest.update((unsigned char *) writeresult, bytes);
 
 				totalbytes += bytes;
 				dataChunkBytes += bytes;
@@ -399,7 +393,8 @@
 }
 
 int twrpback::restore(void) {
-	twrpDigest adb_md5;
+	twrpMD5 digest;
+	char cmd[MAX_ADB_READ];
 	char result[MAX_ADB_READ];
 	struct AdbBackupControlType structcmd;
 	int adb_control_twrp_fd, errctr = 0;
@@ -591,8 +586,8 @@
 					struct twfilehdr twimghdr;
 					uint32_t crc, twimghdrcrc;
 
+					digest.init();
 					totalbytes -= sizeof(result);
-					adb_md5.initMD5();
 					adblogwrite("Restoring TWIMG\n");
 					memset(&twimghdr, 0, sizeof(twimghdr));
 					memcpy(&twimghdr, result, sizeof(result));
@@ -623,9 +618,9 @@
 				else if (cmdtype == TWFN) {
 					struct twfilehdr twfilehdr;
 					uint32_t crc, twfilehdrcrc;
+					digest.init();
 
 					totalbytes -= sizeof(result);
-					adb_md5.initMD5();
 					adblogwrite("Restoring TWFN\n");
 					memset(&twfilehdr, 0, sizeof(twfilehdr));
 					memcpy(&twfilehdr, result, sizeof(result));
@@ -703,15 +698,13 @@
 									close_restore_fds();
 									return -1;
 								}
-								adblogwrite("md5 finalize stream\n");
-								adb_md5.finalizeMD5stream();
 
 								AdbBackupFileTrailer md5;
 
 								memset(&md5, 0, sizeof(md5));
 								strncpy(md5.start_of_trailer, TWRP, sizeof(md5.start_of_trailer));
 								strncpy(md5.type, TWMD5, sizeof(md5.type));
-								std::string md5string = adb_md5.createMD5string();
+								std::string md5string = digest.return_digest_string();
 								strncpy(md5.md5, md5string.c_str(), sizeof(md5.md5));
 
 								adblogwrite("Sending MD5Check\n");
@@ -726,8 +719,7 @@
 								break;
 							}
 						}
-						if (adb_md5.updateMD5stream((unsigned char*)result, sizeof(result)) == -1)
-							adblogwrite("failed to update md5 stream\n");
+						digest.update((unsigned char*)result, sizeof(result));
 						dataChunkBytes += readbytes;
 
 						if (write(adb_write_fd, result, sizeof(result)) < 0) {