Crypto.cpp 623 B

1234567891011121314151617181920212223
  1. #include <QFile>
  2. #include <QCryptographicHash>
  3. #include "Crypto.h"
  4. using namespace craftlab::fakeraid;
  5. Crypto::Crypto()
  6. {
  7. }
  8. void Crypto::Compute(const std::string& dir, FileAndSum& file)
  9. {
  10. if (file.isDir)
  11. return;
  12. const std::string path = dir + "/" + file.fileName;
  13. QFile f(path.c_str());
  14. if (!f.open(QFile::ReadOnly))
  15. throw std::runtime_error("Cannot open file for reading: " + path);
  16. QCryptographicHash hash(QCryptographicHash::Algorithm::Sha256);
  17. if (!hash.addData(&f))
  18. throw std::runtime_error("Cannot read file: " + path);
  19. file.checksum = hash.result().toHex();
  20. }