Skip to content

Commit

Permalink
Checksum: Add support for SHA256 and SHA3
Browse files Browse the repository at this point in the history
In case, some day, the server also supports it
  • Loading branch information
ogoffart committed Jul 5, 2018
1 parent e2df49d commit 7091ca9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/common/checksums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
* - Adler32 (requires zlib)
* - MD5
* - SHA1
* - SHA2
* - SHA3 (requires Qt 5.9)
*
*/

Expand Down Expand Up @@ -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:"))
|| -1 != (i = checksums.indexOf("SHA2:"))
|| -1 != (i = checksums.indexOf("SHA1:"))
|| -1 != (i = checksums.indexOf("MD5:"))
|| -1 != (i = checksums.indexOf("Adler32:"))) {
// Now i is the start of the best checksum
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/common/checksums.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace OCC {
*/
static const char checkSumMD5C[] = "MD5";
static const char checkSumSHA1C[] = "SHA1";
static const char checkSumSHA2C[] = "SHA2";
static const char checkSumSHA3C[] = "SHA3";
static const char checkSumAdlerC[] = "Adler32";

class SyncJournalDb;
Expand Down
2 changes: 1 addition & 1 deletion src/csync/csync_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:");
}

0 comments on commit 7091ca9

Please sign in to comment.