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

Hashing both numbers (123) and (167) gives the same hashed id #165

Closed
hazem-taha opened this issue Mar 24, 2021 · 12 comments
Closed

Hashing both numbers (123) and (167) gives the same hashed id #165

hazem-taha opened this issue Mar 24, 2021 · 12 comments

Comments

@hazem-taha
Copy link

Hello

I'm generating hashed ids using the below:-

(new \Hashids\Hashids('App\Booking' . 's1lt', 10))->encode(SOME_NUMBER)

and i found that it generated two identical hashed ids although the numbers are different (123 and 167)

so both
(new \Hashids\Hashids('App\Booking' . 's1lt', 10))->encode(123)
and
(new \Hashids\Hashids('App\Booking' . 's1lt', 10))->encode(167)

gives the hash Dkq1ZKnyWa

Why is this happening ?
Thanks in advance.

@vinkla
Copy link
Owner

vinkla commented Mar 24, 2021

If you think you've found a bug. Please provide a failing test case in the HashidsTest test class in a pull request.

@vinkla vinkla closed this as completed Mar 24, 2021
@j8n
Copy link

j8n commented May 9, 2021

Something similar happened to me!

Error reproduction
$hash = (new Hashids\Hashids('App\Models\Model'.'_string', 16))->encode(545) --> "KQAd7lo8XLogXG69"
$hash = (new Hashids\Hashids('App\Models\Model'.'_string', 16))->encode(501) --> "KQAd7lo8JLWgXG69"

@vinkla
Copy link
Owner

vinkla commented May 9, 2021

@j8n as you can see, these are not the same hashes:

KQAd7lo8XLogXG69
KQAd7lo8JLWgXG69

@j8n
Copy link

j8n commented May 9, 2021

Yes ok, I cannot reproduce the error again (with test data). I had a "Integrity constraint violation: 1062 Duplicate entry" for a hash id in a record

@j8n
Copy link

j8n commented May 9, 2021

Ok, just to share the original as in my production env:

$hash = (new Hashids\Hashids('App\Models\Subscription'.'_OUTGO', 16))->encode(501); --> "P17zevgVkZExk80a"
$hash = (new Hashids\Hashids('App\Models\Subscription'.'_OUTGO', 16))->encode(545); --> "P17zevgVKZExk80a"
The only change is the case of the "K"

That's how I got the error above

@vinkla
Copy link
Owner

vinkla commented May 9, 2021

The only change is the case of the "K"

This is the expected behaviour.

@j8n
Copy link

j8n commented May 9, 2021

What's the use of Hashids lib, if we get a "Integrity constraint violation" error in the database? The database seems to understand the case incorrectly.
What do I have to do?

@vinkla
Copy link
Owner

vinkla commented May 9, 2021

We don't recommend to store the hashids in the database. Instead encode/decode them on the fly.

Use it when you don't want to expose your database numeric ids to users.

@j8n
Copy link

j8n commented May 9, 2021

ok thanks

@peterchaula
Copy link

So the hashes are unique, and the behaviour is correct, the only issue is with your database being case insensitive.

@hazem-taha
Copy link
Author

I'm saving the hashids to the DB and it is not possible to refactor the system, is there a way to make the DB queries in laravel to be case sensitive ?

@eugene-borovov
Copy link

eugene-borovov commented Jun 9, 2021

Setting up a collation in the database may help you.
https://dev.mysql.com/doc/refman/8.0/en/case-sensitivity.html
https://stackoverflow.com/a/6448861

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

5 participants