Skip to content

Commit

Permalink
Add hash constants for zero-length data.
Browse files Browse the repository at this point in the history
No need to calculate a hash when the data length is known to be zero.  Use one of these constants instead.
  • Loading branch information
dwsteele committed Aug 8, 2019
1 parent 56c24b7 commit e9517dc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
6 changes: 6 additions & 0 deletions src/common/crypto/hash.c
Expand Up @@ -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
***********************************************************************************************************************************/
Expand Down
10 changes: 10 additions & 0 deletions src/common/crypto/hash.h
Expand Up @@ -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
***********************************************************************************************************************************/
Expand Down
4 changes: 1 addition & 3 deletions src/storage/s3/storage.c
Expand Up @@ -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);
Expand Down
13 changes: 5 additions & 8 deletions test/src/module/common/cryptoTest.c
Expand Up @@ -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");

// -------------------------------------------------------------------------------------------------------------------------
Expand All @@ -322,21 +320,20 @@ 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");

// -------------------------------------------------------------------------------------------------------------------------
TEST_RESULT_STR(
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(
Expand Down
20 changes: 5 additions & 15 deletions test/src/module/storage/s3Test.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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"))),
Expand All @@ -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"))),
Expand All @@ -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"))),
Expand All @@ -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"))),
Expand Down

0 comments on commit e9517dc

Please sign in to comment.