pmt: reformat code
This commit is contained in:
51
srclib/libhelper/src/Sha256.cpp
Executable file → Normal file
51
srclib/libhelper/src/Sha256.cpp
Executable file → Normal file
@@ -15,36 +15,43 @@
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <optional>
|
||||
#include <picosha2.h>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Helper {
|
||||
std::optional<std::string> sha256Of(const std::string_view path) {
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path <<
|
||||
"\" request. Getting full path (if input is link and exists)." << std::endl;
|
||||
std::string fp = (isLink(path)) ? readSymlink(path) : std::string(path);
|
||||
std::optional<std::string> sha256Of(const std::string_view path) {
|
||||
LOGN(HELPER, INFO)
|
||||
<< "get sha256 of \"" << path
|
||||
<< "\" request. Getting full path (if input is link and exists)."
|
||||
<< std::endl;
|
||||
std::string fp = (isLink(path)) ? readSymlink(path) : std::string(path);
|
||||
|
||||
if (!fileIsExists(fp)) throw Error("Is not exists or not file: %s", fp.data());
|
||||
if (!fileIsExists(fp))
|
||||
throw Error("Is not exists or not file: %s", fp.data());
|
||||
|
||||
if (const std::ifstream file(fp, std::ios::binary); !file) throw Error("Cannot open file: %s", fp.data());
|
||||
if (const std::ifstream file(fp, std::ios::binary); !file)
|
||||
throw Error("Cannot open file: %s", fp.data());
|
||||
|
||||
std::vector<unsigned char> hash(picosha2::k_digest_size);
|
||||
picosha2::hash256(fp, hash.begin(), hash.end());
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path << "\" successfully." << std::endl;
|
||||
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
||||
}
|
||||
std::vector<unsigned char> hash(picosha2::k_digest_size);
|
||||
picosha2::hash256(fp, hash.begin(), hash.end());
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path << "\" successfully."
|
||||
<< std::endl;
|
||||
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
||||
}
|
||||
|
||||
bool sha256Compare(const std::string_view file1, const std::string_view file2) {
|
||||
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files." << std::endl;
|
||||
const auto f1 = sha256Of(file1);
|
||||
const auto f2 = sha256Of(file2);
|
||||
if (f1->empty() || f2->empty()) return false;
|
||||
LOGN_IF(HELPER, INFO, *f1 == *f2) << "(): input files is contains same sha256 signature." << std::endl;
|
||||
return (*f1 == *f2);
|
||||
}
|
||||
bool sha256Compare(const std::string_view file1, const std::string_view file2) {
|
||||
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files."
|
||||
<< std::endl;
|
||||
const auto f1 = sha256Of(file1);
|
||||
const auto f2 = sha256Of(file2);
|
||||
if (f1->empty() || f2->empty()) return false;
|
||||
LOGN_IF(HELPER, INFO, *f1 == *f2)
|
||||
<< "(): input files is contains same sha256 signature." << std::endl;
|
||||
return (*f1 == *f2);
|
||||
}
|
||||
} // namespace Helper
|
||||
|
||||
Reference in New Issue
Block a user