pgbackrest.git / summary / log / commit / refs

commit    e9517dcec05c75a03fa73029250de4f5b21fdfd3
Author:   David Steele <david@pgmasters.net>
Date:     Thu Aug 08 22:47:02 2019 +0000

    Add hash constants for zero-length data.
    
    No need to calculate a hash when the data length is known to be zero.  Use one of these constants instead.


src/common/crypto/hash.c | 6 ++++++ src/common/crypto/hash.h | 10 ++++++++++ src/storage/s3/storage.c | 4 +--- test/src/module/common/cryptoTest.c | 13 +++++-------- test/src/module/storage/s3Test.c | 20 +++++--------------- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/common/crypto/hash.c b/src/common/crypto/hash.c index 4b62623d4..4aa3c8677 100644 --- a/src/common/crypto/hash.c +++ b/src/common/crypto/hash.c @@ -29,6 +29,12 @@ STRING_EXTERN(HASH_TYPE_MD5_STR, HASH_TYPE_MD STRING_EXTERN(HASH_TYPE_SHA1_STR, HASH_TYPE_SHA1); STRING_EXTERN(HASH_TYPE_SHA256_STR, HASH_TYPE_SHA256); +/*********************************************************************************************************************************** +Hashes for zero-length files (i.e., seed value) +***********************************************************************************************************************************/ +STRING_EXTERN(HASH_TYPE_SHA1_ZERO_STR, HASH_TYPE_SHA1_ZERO); +STRING_EXTERN(HASH_TYPE_SHA256_ZERO_STR, HASH_TYPE_SHA256_ZERO); + /*********************************************************************************************************************************** Object type ***********************************************************************************************************************************/ diff --git a/src/common/crypto/hash.h b/src/common/crypto/hash.h index 84ae9a5ea..6d66e69c9 100644 --- a/src/common/crypto/hash.h +++ b/src/common/crypto/hash.h @@ -25,6 +25,16 @@ Hash types #define HASH_TYPE_SHA256 "sha256" STRING_DECLARE(HASH_TYPE_SHA256_STR); +/*********************************************************************************************************************************** +Hashes for zero-length files (i.e., starting hash) +***********************************************************************************************************************************/ +#define HASH_TYPE_MD5_ZERO "d41d8cd98f00b204e9800998ecf8427e" +#define HASH_TYPE_SHA1_ZERO "da39a3ee5e6b4b0d3255bfef95601890afd80709" + STRING_DECLARE(HASH_TYPE_SHA1_ZERO_STR); +#define HASH_TYPE_SHA256_ZERO \ + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + STRING_DECLARE(HASH_TYPE_SHA256_ZERO_STR); + /*********************************************************************************************************************************** Hash type sizes ***********************************************************************************************************************************/ diff --git a/src/storage/s3/storage.c b/src/storage/s3/storage.c index 791b9a611..ef7046f01 100644 --- a/src/storage/s3/storage.c +++ b/src/storage/s3/storage.c @@ -282,9 +282,7 @@ storageS3Request( // Generate authorization header storageS3Auth( this, verb, httpUriEncode(uri, true), query, storageS3DateTime(time(NULL)), requestHeader, - body == NULL || bufUsed(body) == 0 ? - STRDEF("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") : - bufHex(cryptoHashOne(HASH_TYPE_SHA256_STR, body))); + body == NULL || bufUsed(body) == 0 ? HASH_TYPE_SHA256_ZERO_STR : bufHex(cryptoHashOne(HASH_TYPE_SHA256_STR, body))); // Get an http client HttpClient *httpClient = httpClientCacheGet(this->httpClientCache); diff --git a/test/src/module/common/cryptoTest.c b/test/src/module/common/cryptoTest.c index 4555ea7c8..c4e70e24d 100644 --- a/test/src/module/common/cryptoTest.c +++ b/test/src/module/common/cryptoTest.c @@ -301,11 +301,9 @@ testRun(void) // ------------------------------------------------------------------------------------------------------------------------- TEST_ASSIGN(hash, cryptoHashNewVar(varVarLst(jsonToVar(strNewFmt("[\"%s\"]", HASH_TYPE_SHA1)))), "create sha1 hash"); TEST_RESULT_STR( - strPtr(bufHex(cryptoHash((CryptoHash *)ioFilterDriver(hash)))), "da39a3ee5e6b4b0d3255bfef95601890afd80709", - " check empty hash"); + strPtr(bufHex(cryptoHash((CryptoHash *)ioFilterDriver(hash)))), HASH_TYPE_SHA1_ZERO, " check empty hash"); TEST_RESULT_STR( - strPtr(bufHex(cryptoHash((CryptoHash *)ioFilterDriver(hash)))), "da39a3ee5e6b4b0d3255bfef95601890afd80709", - " check empty hash again"); + strPtr(bufHex(cryptoHash((CryptoHash *)ioFilterDriver(hash)))), HASH_TYPE_SHA1_ZERO, " check empty hash again"); TEST_RESULT_VOID(ioFilterFree(hash), " free hash"); // ------------------------------------------------------------------------------------------------------------------------- @@ -322,12 +320,12 @@ testRun(void) // ------------------------------------------------------------------------------------------------------------------------- TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_MD5)), "create md5 hash"); - TEST_RESULT_STR(strPtr(varStr(ioFilterResult(hash))), "d41d8cd98f00b204e9800998ecf8427e", " check empty hash"); + TEST_RESULT_STR(strPtr(varStr(ioFilterResult(hash))), HASH_TYPE_MD5_ZERO, " check empty hash"); // ------------------------------------------------------------------------------------------------------------------------- TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_SHA256)), "create sha256 hash"); TEST_RESULT_STR( - strPtr(varStr(ioFilterResult(hash))), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + strPtr(varStr(ioFilterResult(hash))), HASH_TYPE_SHA256_ZERO, " check empty hash"); // ------------------------------------------------------------------------------------------------------------------------- @@ -335,8 +333,7 @@ testRun(void) strPtr(bufHex(cryptoHashOne(strNew(HASH_TYPE_SHA1), BUFSTRDEF("12345")))), "8cb2237d0679ca88db6464eac60da96345513964", " check small hash"); TEST_RESULT_STR( - strPtr(bufHex(cryptoHashOne(strNew(HASH_TYPE_SHA1), BUFSTRDEF("")))), "da39a3ee5e6b4b0d3255bfef95601890afd80709", - " check empty hash"); + strPtr(bufHex(cryptoHashOne(strNew(HASH_TYPE_SHA1), BUFSTRDEF("")))), HASH_TYPE_SHA1_ZERO, " check empty hash"); // ------------------------------------------------------------------------------------------------------------------------- TEST_RESULT_STR( diff --git a/test/src/module/storage/s3Test.c b/test/src/module/storage/s3Test.c index 2a3bf44ce..c1b2619d8 100644 --- a/test/src/module/storage/s3Test.c +++ b/test/src/module/storage/s3Test.c @@ -46,9 +46,7 @@ testS3ServerRequest(const char *verb, const char *uri, const char *content) "x-amz-content-sha256:%s\r\n" "x-amz-date:" DATETIME_REPLACE "\r\n" "\r\n", - content == NULL ? - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" : - strPtr(bufHex(cryptoHashOne(HASH_TYPE_SHA256_STR, BUFSTRZ(content))))); + content == NULL ? HASH_TYPE_SHA256_ZERO : strPtr(bufHex(cryptoHashOne(HASH_TYPE_SHA256_STR, BUFSTRZ(content))))); if (content != NULL) strCat(request, content); @@ -698,9 +696,7 @@ testRun(void) httpQueryAdd(query, strNew("list-type"), strNew("2")); TEST_RESULT_VOID( - storageS3Auth( - driver, strNew("GET"), strNew("/"), query, strNew("20170606T121212Z"), header, - strNew("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")), + storageS3Auth(driver, strNew("GET"), strNew("/"), query, strNew("20170606T121212Z"), header, HASH_TYPE_SHA256_ZERO_STR), "generate authorization"); TEST_RESULT_STR( strPtr(httpHeaderGet(header, strNew("authorization"))), @@ -713,9 +709,7 @@ testRun(void) const Buffer *lastSigningKey = driver->signingKey; TEST_RESULT_VOID( - storageS3Auth( - driver, strNew("GET"), strNew("/"), query, strNew("20170606T121212Z"), header, - strNew("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")), + storageS3Auth(driver, strNew("GET"), strNew("/"), query, strNew("20170606T121212Z"), header, HASH_TYPE_SHA256_ZERO_STR), "generate authorization"); TEST_RESULT_STR( strPtr(httpHeaderGet(header, strNew("authorization"))), @@ -727,9 +721,7 @@ testRun(void) // Change the date to generate a new signing key TEST_RESULT_VOID( - storageS3Auth( - driver, strNew("GET"), strNew("/"), query, strNew("20180814T080808Z"), header, - strNew("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")), + storageS3Auth(driver, strNew("GET"), strNew("/"), query, strNew("20180814T080808Z"), header, HASH_TYPE_SHA256_ZERO_STR), " generate authorization"); TEST_RESULT_STR( strPtr(httpHeaderGet(header, strNew("authorization"))), @@ -747,9 +739,7 @@ testRun(void) NULL, NULL)); TEST_RESULT_VOID( - storageS3Auth( - driver, strNew("GET"), strNew("/"), query, strNew("20170606T121212Z"), header, - strNew("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")), + storageS3Auth(driver, strNew("GET"), strNew("/"), query, strNew("20170606T121212Z"), header, HASH_TYPE_SHA256_ZERO_STR), "generate authorization"); TEST_RESULT_STR( strPtr(httpHeaderGet(header, strNew("authorization"))), [parent: 56c24b7669f1]