vinkla/hashids

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

hazem-taha opened this issue · 12 comments

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.

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

j8n commented

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"

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

KQAd7lo8XLogXG69
KQAd7lo8JLWgXG69
j8n commented

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 commented

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

The only change is the case of the "K"

This is the expected behaviour.

j8n commented

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?

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 commented

ok thanks

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

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 ?