Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(murmurHash): fix murmurHash3 implementation, add tests #83

Merged
merged 6 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Usage:
```js
import { murmurHash } from "ohash";

// "2708020327"
// "427197390"
console.log(murmurHash("Hello World"));
```

Expand Down
6 changes: 3 additions & 3 deletions src/crypto/murmur.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* JS Implementation of MurmurHash3 (r136) (as of May 20, 2011)
*
* @param {Uint8Array | string} key ASCII only
* @param {Uint8Array | string} key
* @param {number} seed Positive integer only
* @return {number} 32-bit positive integer hash
*/
Expand Down Expand Up @@ -50,11 +50,11 @@ export function murmurHash(key: Uint8Array | string, seed = 0) {
switch (remainder) {
case 3: {
k1 ^= (key[i + 2] & 0xff) << 16;
break;
/* falls through */
}
case 2: {
k1 ^= (key[i + 1] & 0xff) << 8;
break;
/* falls through */
}
case 1: {
k1 ^= key[i] & 0xff;
Expand Down
36 changes: 34 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,40 @@ describe("objectHash", () => {
});
});

it("murmurHash", () => {
expect(murmurHash("Hello World")).toMatchInlineSnapshot("2708020327");
describe("murmurHash", () => {
it("Generates correct hash for 0 bytes without seed", () => {
expect(murmurHash("")).toMatchInlineSnapshot("0");
});
it("Generates correct hash for 0 bytes with seed", () => {
expect(murmurHash("", 1)).toMatchInlineSnapshot("1364076727"); // 0x514E28B7
});
it("Generates correct hash for 'Hello World'", () => {
expect(murmurHash("Hello World")).toMatchInlineSnapshot("427197390");
});
it("Generates the correct hash for varios string lengths", () => {
expect(murmurHash("a")).toMatchInlineSnapshot("1009084850");
expect(murmurHash("aa")).toMatchInlineSnapshot("923832745");
expect(murmurHash("aaa")).toMatchInlineSnapshot("3033554871");
expect(murmurHash("aaaa")).toMatchInlineSnapshot("2129582471");
expect(murmurHash("aaaaa")).toMatchInlineSnapshot("3922341931");
expect(murmurHash("aaaaaa")).toMatchInlineSnapshot("1736445713");
expect(murmurHash("aaaaaaa")).toMatchInlineSnapshot("1497565372");
expect(murmurHash("aaaaaaaa")).toMatchInlineSnapshot("3662943087");
expect(murmurHash("aaaaaaaaa")).toMatchInlineSnapshot("2724714153");
});
it("Works with Uint8Arrays", () => {
expect(
murmurHash(new Uint8Array([0x21, 0x43, 0x65, 0x87])),
).toMatchInlineSnapshot("4116402539"); // 0xF55B516B
});
it("Handles UTF-8 high characters correctly", () => {
expect(murmurHash("ππππππππ", 0x97_47_b2_8c)).toMatchInlineSnapshot(
"3581961153",
);
});
it("Gives correct hash with uint32 maximum value as seed", () => {
expect(murmurHash("a", 2_147_483_647)).toMatchInlineSnapshot("3574244913");
});
});

it("sha256", () => {
Expand Down
Loading