ahash.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "ahash.h"
  2. #include "src/opl.h"
  3. AHash::AHash(QFile &file)
  4. {
  5. if (file.open(QFile::ReadOnly)) {
  6. QCryptographicHash hash(QCryptographicHash::Md5);
  7. if (hash.addData(&file)) {
  8. checksum = hash.result();
  9. DEB << "File: " << file;
  10. DEB << "Hash: " << hash.result();
  11. } else {
  12. checksum = QByteArray();
  13. }
  14. } else {
  15. checksum = QByteArray();
  16. }
  17. file.close();
  18. }
  19. AHash::AHash(QFileInfo &file_info)
  20. {
  21. QFile f(file_info.absoluteFilePath());
  22. if (f.open(QFile::ReadOnly)) {
  23. QCryptographicHash hash(QCryptographicHash::Md5);
  24. if (hash.addData(&f)) {
  25. checksum = hash.result();
  26. DEB << "File: " << f;
  27. DEB << "Hash: " << hash.result().toHex();
  28. } else {
  29. checksum = QByteArray();
  30. }
  31. } else {
  32. checksum = QByteArray();
  33. }
  34. f.close();
  35. }
  36. bool AHash::compare(QFileInfo &md5_file)
  37. {
  38. // Open the file and read the first 32 characters
  39. QFile f(md5_file.absoluteFilePath());
  40. if (f.open(QFile::ReadOnly)) {
  41. QTextStream in(&f);
  42. const QString hash_string = in.read(32);
  43. DEB << "Checksum:" << hash_string;
  44. // Verify checksum is not empty and compare to md5 read from file
  45. if (checksum == QByteArray())
  46. return false;
  47. else
  48. if (checksum.toHex() == hash_string)
  49. return true;
  50. else
  51. return false;
  52. }
  53. return false;
  54. }