summaryrefslogtreecommitdiffstats
path: root/twrpDigest
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--twrpDigest.cpp161
-rw-r--r--twrpDigest.hpp52
-rw-r--r--twrpDigest/Android.mk29
-rw-r--r--twrpDigest/digest/md5/md5.c (renamed from digest/md5.c)0
-rw-r--r--twrpDigest/digest/md5/md5.h (renamed from digest/md5.h)0
-rw-r--r--twrpDigest/twrpDigest.cpp33
-rw-r--r--twrpDigest/twrpDigest.hpp35
-rw-r--r--twrpDigest/twrpMD5.cpp47
-rw-r--r--twrpDigest/twrpMD5.hpp42
-rw-r--r--twrpDigest/twrpSHA.cpp69
-rw-r--r--twrpDigest/twrpSHA.hpp54
-rw-r--r--twrpDigestDriver.cpp218
-rw-r--r--twrpDigestDriver.hpp33
13 files changed, 560 insertions, 213 deletions
diff --git a/twrpDigest.cpp b/twrpDigest.cpp
deleted file mode 100644
index a9ba20a38..000000000
--- a/twrpDigest.cpp
+++ /dev/null
@@ -1,161 +0,0 @@
-/*
- Copyright 2012 to 2016 bigbiff/Dees_Troy TeamWin
- This file is part of TWRP/TeamWin Recovery Project.
-
- TWRP is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- TWRP is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with TWRP. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-extern "C"
-{
- #include "digest/md5.h"
- #include "libcrecovery/common.h"
-}
-
-#include <vector>
-#include <string>
-#include <sstream>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <string.h>
-#include <libgen.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <fstream>
-#include <sys/mman.h>
-#include "twcommon.h"
-#include "data.hpp"
-#include "variables.h"
-#include "twrp-functions.hpp"
-#include "twrpDigest.hpp"
-#include "set_metadata.h"
-#include "gui/gui.hpp"
-
-using namespace std;
-
-void twrpDigest::setfn(const string& fn) {
- md5fn = fn;
-}
-
-void twrpDigest::initMD5(void) {
- MD5Init(&md5c);
- md5string = "";
-}
-
-int twrpDigest::updateMD5stream(unsigned char* stream, int len) {
- if (md5fn.empty()) {
- MD5Update(&md5c, stream, len);
- }
- else {
- return -1;
- }
- return 0;
-}
-
-void twrpDigest::finalizeMD5stream() {
- MD5Final(md5sum, &md5c);
-}
-
-string twrpDigest::createMD5string() {
- int i;
- char hex[3];
-
- for (i = 0; i < 16; ++i) {
- snprintf(hex, 3, "%02x", md5sum[i]);
- md5string += hex;
- }
- if (!md5fn.empty()) {
- md5string += " ";
- md5string += basename((char*) md5fn.c_str());
- md5string += + "\n";
- }
- return md5string;
-}
-
-int twrpDigest::computeMD5(void) {
- string line;
- FILE *file;
- int len;
- unsigned char buf[1024];
- initMD5();
- file = fopen(md5fn.c_str(), "rb");
- if (file == NULL)
- return MD5_NOT_FOUND;
- while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
- MD5Update(&md5c, buf, len);
- }
- fclose(file);
- MD5Final(md5sum, &md5c);
- return 0;
-}
-
-int twrpDigest::write_md5digest(void) {
- string md5file, md5str;
- md5file = md5fn + ".md5";
-
- md5str = createMD5string();
- TWFunc::write_file(md5file, md5str);
- tw_set_default_metadata(md5file.c_str());
- LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5str.c_str());
- return 0;
-}
-
-int twrpDigest::read_md5digest(void) {
- size_t i = 0;
- bool foundMd5File = false;
- string md5file = "";
- vector<string> md5ext;
- md5ext.push_back(".md5");
- md5ext.push_back(".md5sum");
-
- while (i < md5ext.size()) {
- md5file = md5fn + md5ext[i];
- if (TWFunc::Path_Exists(md5file)) {
- foundMd5File = true;
- break;
- }
- i++;
- }
-
- if (!foundMd5File)
- return MD5_NOT_FOUND;
- if (TWFunc::read_file(md5file, line) != 0)
- return MD5_FILE_UNREADABLE;
-
- return 0;
-}
-
-int twrpDigest::verify_md5digest(void) {
- string buf;
- char hex[3];
- int i, ret;
- string md5str;
-
- ret = read_md5digest();
- if (ret != 0)
- return ret;
- stringstream ss(line);
- vector<string> tokens;
- while (ss >> buf)
- tokens.push_back(buf);
- computeMD5();
- for (i = 0; i < 16; ++i) {
- snprintf(hex, 3, "%02x", md5sum[i]);
- md5str += hex;
- }
- if (tokens.empty() || tokens.at(0) != md5str)
- return MD5_MATCH_FAIL;
-
- return MD5_OK;
-}
diff --git a/twrpDigest.hpp b/twrpDigest.hpp
deleted file mode 100644
index ec4a65f75..000000000
--- a/twrpDigest.hpp
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- Copyright 2012 to 2016 bigbiff/Dees_Troy TeamWin
- This file is part of TWRP/TeamWin Recovery Project.
-
- TWRP is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- TWRP is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with TWRP. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-extern "C" {
- #include "digest/md5.h"
-}
-
-/* verify_md5digest return codes */
-enum {
- MD5_MATCH_FAIL = -2, // -2: md5 did not match
- MD5_NOT_FOUND, // -1: no md5 file found
- MD5_OK, // 0: md5 matches
- MD5_FILE_UNREADABLE // 1: md5 file unreadable
-};
-
-using namespace std;
-
-class twrpDigest
-{
-public:
- void setfn(const string& fn);
- int computeMD5(void);
- int verify_md5digest(void);
- int write_md5digest(void);
- int updateMD5stream(unsigned char* stream, int len);
- void finalizeMD5stream(void);
- string createMD5string(void);
- void initMD5(void);
-
-private:
- int read_md5digest(void);
- struct MD5Context md5c;
- string md5fn;
- string line;
- unsigned char md5sum[MD5LENGTH];
- string md5string;
-};
diff --git a/twrpDigest/Android.mk b/twrpDigest/Android.mk
new file mode 100644
index 000000000..5e69822d7
--- /dev/null
+++ b/twrpDigest/Android.mk
@@ -0,0 +1,29 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libtwrpdigest
+LOCAL_MODULE_TAGS := optional
+LOCAL_CFLAGS = -fno-strict-aliasing
+LOCAL_C_INCLUDES := external/openssl/include bionic
+
+LOCAL_SRC_FILES = \
+ twrpDigest.cpp \
+ twrpMD5.cpp \
+ digest/md5/md5.c
+
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
+ LOCAL_C_INCLUDES += external/stlport/stlport
+endif
+
+LOCAL_SHARED_LIBRARIES += libc libstdc++
+
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
+ LOCAL_SHARED_LIBRARIES += libstlport
+else
+ LOCAL_SHARED_LIBRARIES += libc++ libcrypto
+ LOCAL_SRC_FILES += \
+ twrpSHA.cpp
+endif
+
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/digest/md5.c b/twrpDigest/digest/md5/md5.c
index 488d16ef6..488d16ef6 100644
--- a/digest/md5.c
+++ b/twrpDigest/digest/md5/md5.c
diff --git a/digest/md5.h b/twrpDigest/digest/md5/md5.h
index cf306881f..cf306881f 100644
--- a/digest/md5.h
+++ b/twrpDigest/digest/md5/md5.h
diff --git a/twrpDigest/twrpDigest.cpp b/twrpDigest/twrpDigest.cpp
new file mode 100644
index 000000000..69c97017e
--- /dev/null
+++ b/twrpDigest/twrpDigest.cpp
@@ -0,0 +1,33 @@
+/*
+ Copyright 2012 to 2017 TeamWin
+ This file is part of TWRP/TeamWin Recovery Project.
+
+ TWRP is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ TWRP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with TWRP. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <vector>
+#include <string>
+#include <fcntl.h>
+#include "twrpDigest.hpp"
+
+std::string twrpDigest::hexify(uint8_t* hash, size_t len) {
+ char hex[3];
+ std::string digest_string;
+
+ for (size_t i = 0; i < len; ++i) {
+ snprintf(hex, 3, "%02x", hash[i]);
+ digest_string += hex;
+ }
+ return digest_string;
+}
diff --git a/twrpDigest/twrpDigest.hpp b/twrpDigest/twrpDigest.hpp
new file mode 100644
index 000000000..1803a280d
--- /dev/null
+++ b/twrpDigest/twrpDigest.hpp
@@ -0,0 +1,35 @@
+/*
+ Copyright 2012 to 2017 TeamWin
+ This file is part of TWRP/TeamWin Recovery Project.
+
+ TWRP is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ TWRP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with TWRP. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __TWRPDIGEST_H
+#define __TWRPDIGEST_H
+
+class twrpDigest {
+public:
+ twrpDigest() {};
+ virtual ~twrpDigest() {};
+ virtual void init() = 0; // Initialize the digest according to the algorithm
+ virtual void update(const unsigned char* stream, size_t len) = 0; // Update the digest with new data
+ virtual std::string return_digest_string() = 0; // Returns the digest of the file as a string.
+
+protected:
+ virtual void finalize() = 0; // Finalize the digest input for creating the final digest
+ std::string hexify(uint8_t* hash, size_t len); // Take an len bytes and turn it into a hex string
+};
+
+#endif //__TWRPDIGEST_H
diff --git a/twrpDigest/twrpMD5.cpp b/twrpDigest/twrpMD5.cpp
new file mode 100644
index 000000000..50ccf74b4
--- /dev/null
+++ b/twrpDigest/twrpMD5.cpp
@@ -0,0 +1,47 @@
+/*
+ Copyright 2012 to 2017 TeamWin
+ This file is part of TWRP/TeamWin Recovery Project.
+
+ TWRP is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ TWRP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with TWRP. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <vector>
+#include <string>
+//#include <sstream>
+#include "twrpDigest.hpp"
+#include "twrpMD5.hpp"
+
+twrpMD5::twrpMD5() {
+ init();
+}
+
+void twrpMD5::init() {
+ MD5Init(&md5c);
+}
+
+void twrpMD5::update(const unsigned char* stream, size_t len) {
+ MD5Update(&md5c, stream, len);
+}
+
+void twrpMD5::finalize() {
+ MD5Final(md5sum, &md5c);
+}
+
+std::string twrpMD5::return_digest_string() {
+ std::string md5string;
+ finalize();
+
+ md5string = hexify(md5sum, sizeof(md5sum));
+ return md5string;
+}
diff --git a/twrpDigest/twrpMD5.hpp b/twrpDigest/twrpMD5.hpp
new file mode 100644
index 000000000..40cac189f
--- /dev/null
+++ b/twrpDigest/twrpMD5.hpp
@@ -0,0 +1,42 @@
+/*
+ Copyright 2012 to 2017 TeamWin
+ This file is part of TWRP/TeamWin Recovery Project.
+
+ TWRP is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ TWRP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with TWRP. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __TWRPMD5_H
+#define __TWRPMD5_H
+
+#include <string>
+#include "twrpDigest.hpp"
+
+extern "C" {
+ #include "digest/md5/md5.h"
+}
+
+class twrpMD5: public twrpDigest {
+public:
+ twrpMD5(); // Stream initializer
+ void init(); // Initialize MD5 structures
+ void update(const unsigned char* stream, size_t len); // Update MD5 stream with data. Return false if failure to update MD5.
+ std::string return_digest_string(); // Return MD5 digest as string to callee
+ void finalize(); // Finalize and compute MD5
+
+private:
+ struct MD5Context md5c; // MD5 control structure
+ unsigned char md5sum[MD5LENGTH]; // Stores the md5sum computation
+};
+
+#endif //__TWRPMD5_H
diff --git a/twrpDigest/twrpSHA.cpp b/twrpDigest/twrpSHA.cpp
new file mode 100644
index 000000000..60262e755
--- /dev/null
+++ b/twrpDigest/twrpSHA.cpp
@@ -0,0 +1,69 @@
+/*
+ Copyright 2012 to 2017 TeamWin
+ This file is part of TWRP/TeamWin Recovery Project.
+
+ TWRP is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ TWRP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with TWRP. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <vector>
+#include <string>
+#include <sstream>
+#include <string>
+#include <openssl/sha.h>
+#include "twrpDigest.hpp"
+#include "twrpSHA.hpp"
+
+twrpSHA256::twrpSHA256() {
+ twrpSHA256::init();
+}
+
+void twrpSHA256::init(void) {
+ SHA256_Init(&sha256_ctx);
+}
+
+void twrpSHA256::update(const unsigned char* stream, size_t len) {
+ SHA256_Update(&sha256_ctx, stream, len);
+}
+
+void twrpSHA256::finalize(void) {
+ SHA256_Final(sha256_store, &sha256_ctx);
+}
+
+std::string twrpSHA256::return_digest_string(void) {
+ twrpSHA256::finalize();
+ std::string digest_str = twrpDigest::hexify(sha256_store, SHA256_DIGEST_LENGTH);
+ return digest_str;
+}
+
+twrpSHA512::twrpSHA512() {
+ twrpSHA512::init();
+}
+
+void twrpSHA512::init(void) {
+ SHA512_Init(&sha512_ctx);
+}
+
+void twrpSHA512::update(const unsigned char* stream, size_t len) {
+ SHA512_Update(&sha512_ctx, stream, len);
+}
+
+void twrpSHA512::finalize(void) {
+ SHA512_Final(sha512_store, &sha512_ctx);
+}
+
+std::string twrpSHA512::return_digest_string(void) {
+ twrpSHA512::finalize();
+ std::string digest_str = twrpDigest::hexify(sha512_store, SHA512_DIGEST_LENGTH);
+ return digest_str;
+}
diff --git a/twrpDigest/twrpSHA.hpp b/twrpDigest/twrpSHA.hpp
new file mode 100644
index 000000000..e3899300b
--- /dev/null
+++ b/twrpDigest/twrpSHA.hpp
@@ -0,0 +1,54 @@
+/*
+ Copyright 2012 to 2017 TeamWin
+ This file is part of TWRP/TeamWin Recovery Project.
+
+ TWRP is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ TWRP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with TWRP. If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef __TWRPSHA_H
+#define __TWRPSHA_H
+
+#include <openssl/sha.h>
+#include "twrpDigest.hpp"
+
+class twrpSHA256: public twrpDigest {
+public:
+ twrpSHA256(); // Initialize the SHA256 digest for streaming activities only
+ void init(); // Initialize the SHA256 digest algorithm
+
+protected:
+ void update(const unsigned char* stream, size_t len); // Update the SHA256 digest stream
+ void finalize(); // Finalize the SHA256 digest for computation
+ std::string return_digest_string(); // Return the digest string computed to the callee
+
+private:
+ uint8_t sha256_store[SHA256_DIGEST_LENGTH]; // Initialize the SHA256 digest array that holds the computation
+ SHA256_CTX sha256_ctx; // Initialize the SHA256 control structure
+};
+
+class twrpSHA512: public twrpDigest {
+public:
+ twrpSHA512(); // Initialize the SHA512 digest for streaming activities only
+ void init(); // Initialize the SHA512 digest algorithm
+
+protected:
+ void update(const unsigned char* stream, size_t len); // Update the SHA512 digest stream
+ void finalize(); // Finalize the SHA512 digest for computation
+ std::string return_digest_string(); // Return the digest string computed to the callee
+
+private:
+ uint8_t sha512_store[SHA512_DIGEST_LENGTH]; // Initialize the SHA512 digest array that holds the computation
+ SHA512_CTX sha512_ctx; // Initialize the SHA512 control structure
+};
+
+#endif //__TWRPSHA_H
diff --git a/twrpDigestDriver.cpp b/twrpDigestDriver.cpp
new file mode 100644
index 000000000..71ec9840b
--- /dev/null
+++ b/twrpDigestDriver.cpp
@@ -0,0 +1,218 @@
+/*
+ Copyright 2013 to 2017 TeamWin
+ This file is part of TWRP/TeamWin Recovery Project.
+
+ TWRP is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ TWRP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with TWRP. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#include <fcntl.h>
+#include <string>
+#include <unistd.h>
+#include "data.hpp"
+#include "partitions.hpp"
+#include "set_metadata.h"
+#include "twrpDigestDriver.hpp"
+#include "twrp-functions.hpp"
+#include "twcommon.h"
+#include "variables.h"
+#include "gui/gui.hpp"
+#include "twrpDigest/twrpDigest.hpp"
+#include "twrpDigest/twrpMD5.hpp"
+#include "twrpDigest/twrpSHA.hpp"
+
+
+bool twrpDigestDriver::Check_Restore_File_Digest(const string& Filename) {
+ twrpDigest *digest;
+ string digestfile = Filename, file_name = Filename;
+ string digest_str;
+ bool use_sha2 = false;
+
+#ifndef TW_NO_SHA2_LIBRARY
+
+ digestfile += ".sha2";
+ if (TWFunc::Path_Exists(digestfile)) {
+ digest = new twrpSHA256();
+ use_sha2 = true;
+ }
+ else {
+ digest = new twrpMD5();
+ digestfile = Filename + ".md5";
+
+ }
+#else
+ digest = new twrpMD5();
+ digestfile = Filename + ".md5";
+
+#endif
+
+ if (!TWFunc::Path_Exists(digestfile)) {
+ gui_msg(Msg(msg::kError, "no_digest_found=No digest file found for '{1}'. Please unselect Enable Digest verification to restore.")(Filename));
+ delete digest;
+ return false;
+ }
+
+
+ if (TWFunc::read_file(digestfile, digest_str) != 0) {
+ gui_msg("digest_error=Digest Error!");
+ delete digest;
+ return false;
+ }
+
+ if (!stream_file_to_digest(file_name, digest)) {
+ delete digest;
+ return false;
+ }
+ string digest_check = digest->return_digest_string();
+ if (digest_check == digest_str) {
+ if (use_sha2)
+ LOGINFO("SHA2 Digest: %s %s\n", digest_str.c_str(), TWFunc::Get_Filename(Filename).c_str());
+ else
+ LOGINFO("MD5 Digest: %s %s\n", digest_str.c_str(), TWFunc::Get_Filename(Filename).c_str());
+ delete digest;
+ return true;
+ }
+
+ gui_msg(Msg(msg::kError, "digest_fail_match=Digest failed to match on '{1}'.")(Filename));
+ delete digest;
+ return false;
+
+}
+
+bool twrpDigestDriver::Check_Digest(string Full_Filename) {
+ char split_filename[512];
+ int index = 0;
+
+ sync();
+ if (!TWFunc::Path_Exists(Full_Filename)) {
+ // This is a split archive, we presume
+ memset(split_filename, 0, sizeof(split_filename));
+ while (index < 1000) {
+ sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
+ if (!TWFunc::Path_Exists(split_filename))
+ break;
+ LOGINFO("split_filename: %s\n", split_filename);
+ if (!Check_Restore_File_Digest(split_filename))
+ return false;
+ index++;
+ }
+ return true;
+ }
+ return Check_Restore_File_Digest(Full_Filename); // Single file archive
+}
+
+bool twrpDigestDriver::Write_Digest(string Full_Filename) {
+ twrpDigest *digest;
+ string digest_filename, digest_str;
+ int use_sha2;
+
+#ifdef TW_NO_SHA2_LIBRARY
+ use_sha2 = 0;
+#else
+ DataManager::GetValue(TW_USE_SHA2, use_sha2);
+#endif
+
+ if (use_sha2) {
+#ifndef TW_NO_SHA2_LIBRARY
+ digest = new twrpSHA256();
+ digest_filename = Full_Filename + ".sha2";
+ if (!stream_file_to_digest(Full_Filename, digest)) {
+ delete digest;
+ return false;
+ }
+ digest_str = digest->return_digest_string();
+ if (digest_str.empty()) {
+ delete digest;
+ return false;
+ }
+ LOGINFO("SHA2 Digest: %s %s\n", digest_str.c_str(), TWFunc::Get_Filename(Full_Filename).c_str());
+#endif
+ }
+ else {
+ digest = new twrpMD5();
+ digest_filename = Full_Filename + ".md5";
+ if (!stream_file_to_digest(Full_Filename, digest)) {
+ delete digest;
+ return false;
+ }
+ digest_str = digest->return_digest_string();
+ if (digest_str.empty()) {
+ delete digest;
+ return false;
+ }
+ LOGINFO("MD5 Digest: %s %s\n", digest_str.c_str(), TWFunc::Get_Filename(Full_Filename).c_str());
+ }
+
+ digest_str = digest_str + " " + TWFunc::Get_Filename(Full_Filename) + "\n";
+ LOGINFO("digest_filename: %s\n", digest_filename.c_str());
+
+ if (TWFunc::write_file(digest_filename, digest_str) == 0) {
+ tw_set_default_metadata(digest_filename.c_str());
+ gui_msg("digest_created= * Digest Created.");
+ }
+ else {
+ gui_err("digest_error= * Digest Error!");
+ delete digest;
+ return false;
+ }
+ delete digest;
+ return true;
+}
+
+bool twrpDigestDriver::Make_Digest(string Full_Filename) {
+ string command, result;
+
+ TWFunc::GUI_Operation_Text(TW_GENERATE_DIGEST_TEXT, gui_parse_text("{@generating_digest1}"));
+ gui_msg("generating_digest2= * Generating digest...");
+ if (TWFunc::Path_Exists(Full_Filename)) {
+ if (!Write_Digest(Full_Filename))
+ return false;
+ } else {
+ char filename[512];
+ int index = 0;
+ sprintf(filename, "%s%03i", Full_Filename.c_str(), index);
+ while (index < 1000) {
+ string digest_src(filename);
+ if (TWFunc::Path_Exists(filename)) {
+ if (!Write_Digest(filename))
+ return false;
+ }
+ else
+ break;
+ index++;
+ sprintf(filename, "%s%03i", Full_Filename.c_str(), index);
+ }
+ if (index == 0) {
+ LOGERR("Backup file: '%s' not found!\n", filename);
+ return false;
+ }
+ gui_msg("digest_created= * Digest Created.");
+ }
+ return true;
+}
+
+bool twrpDigestDriver::stream_file_to_digest(string filename, twrpDigest* digest) {
+ char buf[4096];
+ int bytes;
+
+ int fd = open(filename.c_str(), O_RDONLY);
+ if (fd < 0) {
+ return false;
+ }
+ while ((bytes = read(fd, &buf, sizeof(buf))) != 0) {
+ digest->update((unsigned char*)buf, bytes);
+ }
+ close(fd);
+ return true;
+}
diff --git a/twrpDigestDriver.hpp b/twrpDigestDriver.hpp
new file mode 100644
index 000000000..6902c8ec6
--- /dev/null
+++ b/twrpDigestDriver.hpp
@@ -0,0 +1,33 @@
+/*
+ Copyright 2013 to 2017 TeamWin
+ This file is part of TWRP/TeamWin Recovery Project.
+
+ TWRP is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ TWRP is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with TWRP. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __TWRP_DIGEST_DRIVER
+#define __TWRP_DIGEST_DRIVER
+#include <string>
+#include "twrpDigest/twrpDigest.hpp"
+
+class twrpDigestDriver {
+public:
+
+ static bool Check_Restore_File_Digest(const string& Filename); //Check the digest of a TWRP partition backup
+ static bool Check_Digest(string Full_Filename); //Check to make sure the digest is correct
+ static bool Write_Digest(string Full_Filename); //Write the digest to a file
+ static bool Make_Digest(string Full_Filename); //Create the digest for a partition backup
+ static bool stream_file_to_digest(string filename, twrpDigest* digest); //Stream the file to twrpDigest
+};
+#endif //__TWRP_DIGEST_DRIVER