9#include <Poco/DigestStream.h>
10#include <Poco/MD5Engine.h>
11#include <Poco/SHA1Engine.h>
12#include <boost/regex.hpp>
20constexpr auto EOL_LF =
"\n";
22constexpr auto EOL_CRLF =
"\r\n";
29std::string createSHA1(
const std::string &data,
const std::string &header =
"") {
30 using Poco::DigestEngine;
31 using Poco::DigestOutputStream;
32 using Poco::SHA1Engine;
35 DigestOutputStream outstr(sha1);
38 return DigestEngine::digestToHex(sha1.digest());
46std::string createMD5(
const std::string &data,
const std::string &header =
"") {
47 using Poco::DigestEngine;
48 using Poco::DigestOutputStream;
49 using Poco::MD5Engine;
52 DigestOutputStream outstr(sha1);
55 return DigestEngine::digestToHex(sha1.digest());
65std::string
md5FromString(
const std::string &input) {
return createMD5(input); }
71std::string
sha1FromString(
const std::string &input) {
return createSHA1(input); }
78std::string
sha1FromFile(
const std::string &filepath,
const bool unixEOL =
false) {
81 return createSHA1(
loadFile(filepath, unixEOL));
97 const bool unixEOL(
true);
99 std::stringstream header;
100 header <<
"blob " << contents.size() <<
'\0';
101 return createSHA1(contents, header.str());
109std::string
loadFile(
const std::string &filepath,
const bool unixEOL =
false) {
111 std::ifstream filein(filepath.c_str(), std::ios::in | std::ios::binary);
115 std::string contents;
116 filein.seekg(0, std::ios::end);
117 contents.resize(filein.tellg());
118 filein.seekg(0, std::ios::beg);
119 filein.read(&contents[0], contents.size());
126 static boost::regex eol(EOL_CRLF);
127 contents = boost::regex_replace(contents, eol, EOL_LF);
ChecksumHelper : A selection of helper methods for calculating checksums.
MANTID_KERNEL_DLL std::string sha1FromString(const std::string &input)
create a SHA-1 checksum from a string
MANTID_KERNEL_DLL std::string gitSha1FromFile(const std::string &filepath)
create a git checksum from a file (these match the git hash-object command)
MANTID_KERNEL_DLL std::string md5FromString(const std::string &input)
create a md5 checksum from a string
MANTID_KERNEL_DLL std::string loadFile(const std::string &filepath, const bool unixEOL)
Load contents of file into a string.
MANTID_KERNEL_DLL std::string sha1FromFile(const std::string &filepath, const bool unixEOL)
create a SHA-1 checksum from a file