From a46f20e6210c595005dd672313ed7d92b3d474b4 Mon Sep 17 00:00:00 2001 From: James Wilkinson Date: Mon, 14 Aug 2017 11:53:46 +0200 Subject: [PATCH] Add single file hash function --- src/InitiateState.cpp | 11 ++++ src/util/util.cpp | 136 +++++++++++++++++++++++------------------- src/util/util.h | 1 + 3 files changed, 85 insertions(+), 63 deletions(-) diff --git a/src/InitiateState.cpp b/src/InitiateState.cpp index a7268bd..0b18a98 100644 --- a/src/InitiateState.cpp +++ b/src/InitiateState.cpp @@ -472,6 +472,16 @@ namespace pd2hook return 1; } + int luaF_filehash(lua_State* L) + { + int n = lua_gettop(L); + size_t l = 0; + const char * fileName = lua_tolstring(L, 1, &l); + std::string hash = Util::GetFileHash(fileName); + lua_pushlstring(L, hash.c_str(), hash.length()); + return 1; + } + static int HTTPReqIdent = 0; int luaF_dohttpreq(lua_State* L) @@ -617,6 +627,7 @@ namespace pd2hook { "RemoveDirectory", luaF_removeDirectory }, { "DirectoryExists", luaF_directoryExists }, { "DirectoryHash", luaF_directoryhash }, + { "FileHash", luaF_filehash }, { NULL, NULL } }; luaI_openlib(L, "file", fileLib, 0); diff --git a/src/util/util.cpp b/src/util/util.cpp index 00085c5..29b0dd9 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -9,78 +9,88 @@ namespace pd2hook { -namespace Util -{ -Exception::Exception(const char *file, int line) : - mFile(file), mLine(line) -{} + namespace Util + { -Exception::Exception(std::string msg, const char *file, int line) : - mFile(file), mLine(line), mMsg(std::move(msg)) -{} + Exception::Exception(const char *file, int line) : + mFile(file), mLine(line) + {} -const char *Exception::what() const -{ - if (!mMsg.empty()) - { - return mMsg.c_str(); - } + Exception::Exception(std::string msg, const char *file, int line) : + mFile(file), mLine(line), mMsg(std::move(msg)) + {} - return std::exception::what(); -} + const char *Exception::what() const + { + if (!mMsg.empty()) + { + return mMsg.c_str(); + } -const char *Exception::exceptionName() const -{ - return "An exception"; -} + return std::exception::what(); + } -void Exception::writeToStream(std::ostream& os) const -{ - os << exceptionName() << " occurred @ (" << mFile << ':' << mLine << "). " << what(); -} + const char *Exception::exceptionName() const + { + return "An exception"; + } -std::string sha256(const std::string str) -{ - unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256_CTX sha256; - SHA256_Init(&sha256); - SHA256_Update(&sha256, str.c_str(), str.size()); - SHA256_Final(hash, &sha256); - std::stringstream ss; - for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) - { - ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; - } - return ss.str(); -} + void Exception::writeToStream(std::ostream& os) const + { + os << exceptionName() << " occurred @ (" << mFile << ':' << mLine << "). " << what(); + } -void RecurseDirectoryPaths(std::vector& paths, std::string directory) { - std::vector dirs = pd2hook::Util::GetDirectoryContents(directory, true); - std::vector files = pd2hook::Util::GetDirectoryContents(directory); - for (auto it = files.begin(); it < files.end(); it++) { - std::string fpath = directory + *it; - std::transform(fpath.begin(), fpath.end(), fpath.begin(), ::tolower); - paths.push_back(fpath); - } - for (auto it = dirs.begin(); it < dirs.end(); it++) { - if (*it == "." || *it == "..") continue; - RecurseDirectoryPaths(paths, directory + *it + "\\"); - } -} + std::string sha256(const std::string str) + { + unsigned char hash[SHA256_DIGEST_LENGTH]; + SHA256_CTX sha256; + SHA256_Init(&sha256); + SHA256_Update(&sha256, str.c_str(), str.size()); + SHA256_Final(hash, &sha256); + std::stringstream ss; + for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) + { + ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; + } + return ss.str(); + } -std::string GetDirectoryHash(std::string directory) { - std::vector paths; - RecurseDirectoryPaths(paths, directory); - std::sort(paths.begin(), paths.end()); + void RecurseDirectoryPaths(std::vector& paths, std::string directory) { + std::vector dirs = pd2hook::Util::GetDirectoryContents(directory, true); + std::vector files = pd2hook::Util::GetDirectoryContents(directory); + for (auto it = files.begin(); it < files.end(); it++) { + std::string fpath = directory + *it; + std::transform(fpath.begin(), fpath.end(), fpath.begin(), ::tolower); + paths.push_back(fpath); + } + for (auto it = dirs.begin(); it < dirs.end(); it++) { + if (*it == "." || *it == "..") continue; + RecurseDirectoryPaths(paths, directory + *it + "\\"); + } + } - std::string hashconcat; + std::string GetDirectoryHash(std::string directory) + { + std::vector paths; + RecurseDirectoryPaths(paths, directory); + std::sort(paths.begin(), paths.end()); - for (auto it = paths.begin(); it < paths.end(); it++) { - std::string hashstr = sha256(pd2hook::Util::GetFileContents(*it)); - hashconcat += hashstr; - } + std::string hashconcat; - return sha256(hashconcat); -} -} + for (auto it = paths.begin(); it < paths.end(); it++) { + std::string hashstr = sha256(pd2hook::Util::GetFileContents(*it)); + hashconcat += hashstr; + } + + return sha256(hashconcat); + } + + std::string GetFileHash(std::string file) + { + // This has to be hashed twice otherwise it won't be the same hash if we're checking against a file uploaded to the server + std::string hash = sha256(pd2hook::Util::GetFileContents(file)); + return sha256(hash); + } + + } } diff --git a/src/util/util.h b/src/util/util.h index 8d843a2..9fc604a 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -21,6 +21,7 @@ namespace Util { void SplitString(const std::string &s, char delim, std::vector &elems); std::vector SplitString(const std::string &s, char delim); std::string GetDirectoryHash(std::string directory); + std::string GetFileHash(std::string filename); class Exception : public std::exception {