ahash.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2021 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "ahash.h"
  19. #include "src/opl.h"
  20. AHash::AHash(QFileInfo &file_info)
  21. {
  22. QFile f(file_info.absoluteFilePath());
  23. if (f.open(QFile::ReadOnly)) {
  24. QCryptographicHash hash(QCryptographicHash::Md5);
  25. if (hash.addData(&f)) {
  26. checksum = hash.result();
  27. DEB << "File: " << f;
  28. DEB << "Hash: " << hash.result().toHex();
  29. } else {
  30. checksum = QByteArray();
  31. }
  32. } else {
  33. checksum = QByteArray();
  34. }
  35. f.close();
  36. }
  37. bool AHash::compare(QFileInfo &md5_file)
  38. {
  39. // Open the file and read the first 32 characters
  40. QFile f(md5_file.absoluteFilePath());
  41. if (f.open(QFile::ReadOnly)) {
  42. QTextStream in(&f);
  43. const QString hash_string = in.read(32);
  44. DEB << "Checksum:" << hash_string;
  45. // Verify checksum is not empty and compare to md5 read from file
  46. if (checksum == QByteArray())
  47. return false;
  48. else
  49. if (checksum.toHex() == hash_string)
  50. return true;
  51. else
  52. return false;
  53. }
  54. return false;
  55. }