checksumcalculator.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (C) 2023 by Oleksandr Zolotov <alex@nextcloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #pragma once
  15. #include "ocsynclib.h"
  16. #include "config.h"
  17. #include "checksumconsts.h"
  18. #include <QObject>
  19. #include <QByteArray>
  20. #include <QFutureWatcher>
  21. #include <QMutex>
  22. #include <memory>
  23. class QCryptographicHash;
  24. namespace OCC {
  25. class OCSYNC_EXPORT ChecksumCalculator
  26. {
  27. Q_DISABLE_COPY(ChecksumCalculator)
  28. public:
  29. enum class AlgorithmType {
  30. Undefined = -1,
  31. MD5,
  32. SHA1,
  33. SHA256,
  34. SHA3_256,
  35. Adler32,
  36. };
  37. ChecksumCalculator(QSharedPointer<QIODevice> sharedDevice, const QByteArray &checksumTypeName);
  38. ~ChecksumCalculator();
  39. [[nodiscard]] QByteArray calculate();
  40. private:
  41. void initChecksumAlgorithm();
  42. bool addChunk(const QByteArray &chunk, const qint64 size);
  43. QSharedPointer<QIODevice> _device;
  44. QScopedPointer<QCryptographicHash> _cryptographicHash;
  45. unsigned int _adlerHash = 0;
  46. bool _isInitialized = false;
  47. AlgorithmType _algorithmType = AlgorithmType::Undefined;
  48. QMutex _deviceMutex;
  49. };
  50. }