From 993a674e1387547fd1dafa3d072fa637c6818bea Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Thu, 5 Jul 2018 14:36:10 +0200 Subject: [PATCH] Checksum: Add support for SHA256 and SHA3 In case, some day, the server also supports it --- src/common/checksums.cpp | 13 ++++++++++++- src/common/checksums.h | 2 ++ src/csync/csync_util.cpp | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/common/checksums.cpp b/src/common/checksums.cpp index ba638c3b4f6..bf4f83982f6 100644 --- a/src/common/checksums.cpp +++ b/src/common/checksums.cpp @@ -78,6 +78,8 @@ * - Adler32 (requires zlib) * - MD5 * - SHA1 + * - SHA256 + * - SHA3-256 (requires Qt 5.9) * */ @@ -146,7 +148,9 @@ QByteArray findBestChecksum(const QByteArray &checksums) { int i = 0; // The order of the searches here defines the preference ordering. - if (-1 != (i = checksums.indexOf("SHA1:")) + if (-1 != (i = checksums.indexOf("SHA3-256:")) + || -1 != (i = checksums.indexOf("SHA256:")) + || -1 != (i = checksums.indexOf("SHA1:")) || -1 != (i = checksums.indexOf("MD5:")) || -1 != (i = checksums.indexOf("Adler32:"))) { // Now i is the start of the best checksum @@ -243,7 +247,14 @@ QByteArray ComputeChecksum::computeNow(const QString &filePath, const QByteArray return calcMd5(filePath); } else if (checksumType == checkSumSHA1C) { return calcSha1(filePath); + } else if (checksumType == checkSumSHA2C) { + return calcCryptoHash(filePath, QCryptographicHash::Sha256); } +#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0) + else if (checksumType == checkSumSHA3C) { + return calcCryptoHash(filePath, QCryptographicHash::Sha3_256); + } +#endif #ifdef ZLIB_FOUND else if (checksumType == checkSumAdlerC) { return calcAdler32(filePath); diff --git a/src/common/checksums.h b/src/common/checksums.h index a37c879092d..6e98e0af56d 100644 --- a/src/common/checksums.h +++ b/src/common/checksums.h @@ -33,6 +33,8 @@ namespace OCC { */ static const char checkSumMD5C[] = "MD5"; static const char checkSumSHA1C[] = "SHA1"; +static const char checkSumSHA2C[] = "SHA256"; +static const char checkSumSHA3C[] = "SHA3-256"; static const char checkSumAdlerC[] = "Adler32"; class SyncJournalDb; diff --git a/src/csync/csync_util.cpp b/src/csync/csync_util.cpp index 761537bb8dd..53c6e00eedf 100644 --- a/src/csync/csync_util.cpp +++ b/src/csync/csync_util.cpp @@ -194,6 +194,6 @@ time_t oc_httpdate_parse( const char *date ) { bool csync_is_collision_safe_hash(const QByteArray &checksum_header) { - return checksum_header.startsWith("SHA1:") + return checksum_header.startsWith("SHA") || checksum_header.startsWith("MD5:"); }