1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

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.
This commit is contained in:
David Steele 2019-08-08 18:47:02 -04:00
parent 56c24b7669
commit e9517dcec0
5 changed files with 27 additions and 26 deletions

View File

@ -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
***********************************************************************************************************************************/

View File

@ -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
***********************************************************************************************************************************/

View File

@ -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);

View File

@ -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(

View File

@ -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"))),