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

mumurhash32 collision between ufo and vue #11

Open
danielroe opened this issue Jul 8, 2022 · 4 comments
Open

mumurhash32 collision between ufo and vue #11

danielroe opened this issue Jul 8, 2022 · 4 comments
Assignees

Comments

@danielroe
Copy link
Member

danielroe commented Jul 8, 2022

require('ohash').hash('ufo')
// '2247144487'
require('ohash').hash('vue')
// '2247144487'

related: nuxt/framework#5398

danielroe added a commit to nuxt/framework that referenced this issue Jul 8, 2022
danielroe added a commit to nuxt/framework that referenced this issue Jul 12, 2022
danielroe added a commit to nuxt/framework that referenced this issue Jul 13, 2022
@pi0
Copy link
Member

pi0 commented Jul 14, 2022

Another collision:

/node_modules/vue-toastification/dist/index.css // 944007393
/node_modules/vue-toastification/dist/index.mjs // 944007393

Ref: nuxt/nuxt#14313

@pi0 pi0 changed the title collision between ufo and vue mumurhash32 collision between ufo and vue Jul 14, 2022
@pi0 pi0 self-assigned this Jul 14, 2022
@pi0
Copy link
Member

pi0 commented Jul 14, 2022

Using SHA2 in latest 0.1.x #12

@aleclarson
Copy link

aleclarson commented Sep 1, 2022

Just a shot in the dark, but it seems collisions can be easily avoided with padEnd?

import { murmurHash } from 'ohash'

const fixedHash = (s) => murmurHash(s.padEnd(s.length + (4 - (s.length & 3)) & 3, '\0'))

fixedHash('vue') // => 2175094470
fixedHash('ufo') // => 3195201511

fixedHash('/node_modules/vue-toastification/dist/index.mjs')
// => 1385894279
fixedHash('/node_modules/vue-toastification/dist/index.css')
// => 3153126526

I'm not sure if this really fixes it or not, to be honest. :)

From this article:

No collisions are possible for 4-byte keys

So perhaps making sure that remainder is always zero fixes it? 😅

const remainder = key.length & 3 // key.length % 4

My hypothesis is basically, "No collisions exist for modulus 4 length strings." Again, no idea if it's sound.

@pi0 pi0 reopened this Sep 1, 2022
@pi0
Copy link
Member

pi0 commented Sep 1, 2022

Really nice point @aleclarson thanks for sharing article. Do you mind to open a PR directly adding fix to murmurHash function? (Also maybe few tests)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants