Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Add single file hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWilko committed Aug 14, 2017
1 parent 5b104a2 commit a46f20e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 63 deletions.
11 changes: 11 additions & 0 deletions src/InitiateState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
136 changes: 73 additions & 63 deletions src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& paths, std::string directory) {
std::vector<std::string> dirs = pd2hook::Util::GetDirectoryContents(directory, true);
std::vector<std::string> 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<std::string> paths;
RecurseDirectoryPaths(paths, directory);
std::sort(paths.begin(), paths.end());
void RecurseDirectoryPaths(std::vector<std::string>& paths, std::string directory) {
std::vector<std::string> dirs = pd2hook::Util::GetDirectoryContents(directory, true);
std::vector<std::string> 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<std::string> 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);
}

}
}
1 change: 1 addition & 0 deletions src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Util {
void SplitString(const std::string &s, char delim, std::vector<std::string> &elems);
std::vector<std::string> SplitString(const std::string &s, char delim);
std::string GetDirectoryHash(std::string directory);
std::string GetFileHash(std::string filename);

class Exception : public std::exception
{
Expand Down

0 comments on commit a46f20e

Please sign in to comment.