Checksum: Add support for SHA256 and SHA3
authorOlivier Goffart <ogoffart@woboq.com>
Thu, 5 Jul 2018 12:36:10 +0000 (14:36 +0200)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:05 +0000 (10:58 +0100)
In case, some day, the server also supports it

src/common/checksums.cpp
src/common/checksums.h
src/csync/csync_util.cpp

index a2bfdabc798d3492c41d5df6e12877384d50bf19..49ac2eafb886d79b0d3965e74cbfd19b3b9012e4 100644 (file)
@@ -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);
index 765e4659b6769bcdff95b0baffc8e076816e3677..f00a31cf86184c375081babcd8cb1312d357af00 100644 (file)
@@ -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;
index 57b9923875366ddc264d0c40c05090d2e71331bc..696aa41fef4c4a154e83de73073fc06de863eb29 100644 (file)
@@ -195,6 +195,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:");
 }