Mantid
Loading...
Searching...
No Matches
ChecksumHelper.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <Poco/DigestStream.h>
10#include <Poco/MD5Engine.h>
11#include <Poco/SHA1Engine.h>
12#include <boost/regex.hpp>
13
14#include <fstream>
15#include <sstream>
16
17namespace {
18
19// Unix line ending character
20constexpr auto EOL_LF = "\n";
21// Windows line ending sequence
22constexpr auto EOL_CRLF = "\r\n";
23
29std::string createSHA1(const std::string &data, const std::string &header = "") {
30 using Poco::DigestEngine;
31 using Poco::DigestOutputStream;
32 using Poco::SHA1Engine;
33
34 SHA1Engine sha1;
35 DigestOutputStream outstr(sha1);
36 outstr << header << data;
37 outstr.flush(); // to pass everything to the digest engine
38 return DigestEngine::digestToHex(sha1.digest());
39}
40
46std::string createMD5(const std::string &data, const std::string &header = "") {
47 using Poco::DigestEngine;
48 using Poco::DigestOutputStream;
49 using Poco::MD5Engine;
50
51 MD5Engine sha1;
52 DigestOutputStream outstr(sha1);
53 outstr << header << data;
54 outstr.flush(); // to pass everything to the digest engine
55 return DigestEngine::digestToHex(sha1.digest());
56}
57} // namespace
58
60
65std::string md5FromString(const std::string &input) { return createMD5(input); }
66
71std::string sha1FromString(const std::string &input) { return createSHA1(input); }
72
78std::string sha1FromFile(const std::string &filepath, const bool unixEOL = false) {
79 if (filepath.empty())
80 return "";
81 return createSHA1(loadFile(filepath, unixEOL));
82}
83
94std::string gitSha1FromFile(const std::string &filepath) {
95 if (filepath.empty())
96 return "";
97 const bool unixEOL(true);
98 std::string contents = ChecksumHelper::loadFile(filepath, unixEOL);
99 std::stringstream header;
100 header << "blob " << contents.size() << '\0';
101 return createSHA1(contents, header.str());
102}
103
109std::string loadFile(const std::string &filepath, const bool unixEOL = false) {
110
111 std::ifstream filein(filepath.c_str(), std::ios::in | std::ios::binary);
112 if (!filein)
113 return "";
114
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());
120 filein.close();
121
122 if (unixEOL) {
123 // convert CRLF to LF
124 // Old-style mac line endings are left alone by git:
125 // https://github.com/git/git/blob/bfdd66e72fffd18235757bedbf355fd4176d6019/convert.c#L1425
126 static boost::regex eol(EOL_CRLF);
127 contents = boost::regex_replace(contents, eol, EOL_LF);
128 }
129 return contents;
130}
131
132} // namespace Mantid::Kernel::ChecksumHelper
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