Skip to content

Commit eefdda6

Browse files
committed
Correct computation of joaat hash.
Some operations that should've been performed in the finalize function were instead performed on each chunk, leading to incorrect hashes.
1 parent d001009 commit eefdda6

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

ext/hash/hash_joaat.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,22 @@ PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *i
4848

4949
PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX * context)
5050
{
51+
uint32_t hval = context->state;
52+
hval += (hval << 3);
53+
hval ^= (hval >> 11);
54+
hval += (hval << 15);
55+
5156
#ifdef WORDS_BIGENDIAN
52-
memcpy(digest, &context->state, 4);
57+
memcpy(digest, &hval, 4);
5358
#else
5459
int i = 0;
55-
unsigned char *c = (unsigned char *) &context->state;
60+
unsigned char *c = (unsigned char *) &hval;
5661

5762
for (i = 0; i < 4; i++) {
5863
digest[i] = c[3 - i];
5964
}
6065
#endif
61-
context->state = 0;
66+
context->state = 0;
6267
}
6368

6469
/*
@@ -83,9 +88,5 @@ joaat_buf(void *buf, size_t len, uint32_t hval)
8388
hval ^= (hval >> 6);
8489
}
8590

86-
hval += (hval << 3);
87-
hval ^= (hval >> 11);
88-
hval += (hval << 15);
89-
9091
return hval;
9192
}

ext/hash/tests/hash-clone.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ foreach ($algos as $algo) {
2424

2525
hash_update($copy, "Can’t tell if this is true or dream");
2626
var_dump(hash_final($copy));
27+
28+
if ($algo === "joaat") {
29+
var_dump(hash($algo, "I can't remember anythingCan’t tell if this is true or dream"));
30+
}
2731
}
2832

2933
echo "Done\n";
@@ -301,7 +305,8 @@ string(16) "bebc746a33b6ab62"
301305
string(16) "893899e4415a920f"
302306
string(5) "joaat"
303307
string(8) "aaebf370"
304-
string(8) "513479b4"
308+
string(8) "836fb0e5"
309+
string(8) "836fb0e5"
305310
string(10) "haval128,3"
306311
string(32) "86362472c8895e68e223ef8b3711d8d9"
307312
string(32) "ebeeeb05c18af1e53d2d127b561d5e0d"

ext/hash/tests/hash_copy_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ string(16) "bebc746a33b6ab62"
301301
string(16) "893899e4415a920f"
302302
string(5) "joaat"
303303
string(8) "aaebf370"
304-
string(8) "513479b4"
304+
string(8) "836fb0e5"
305305
string(10) "haval128,3"
306306
string(32) "86362472c8895e68e223ef8b3711d8d9"
307307
string(32) "ebeeeb05c18af1e53d2d127b561d5e0d"

ext/hash/tests/hash_serialize_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ string(16) "893899e4415a920f"
320320
string(16) "893899e4415a920f"
321321
string(5) "joaat"
322322
string(8) "836fb0e5"
323-
string(8) "513479b4"
323+
string(8) "836fb0e5"
324324
string(10) "haval128,3"
325325
string(32) "ebeeeb05c18af1e53d2d127b561d5e0d"
326326
string(32) "ebeeeb05c18af1e53d2d127b561d5e0d"

0 commit comments

Comments
 (0)