vinkla / hashids

A small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.
https://hashids.org/php
MIT License
5.26k stars 417 forks source link

Padding/minimum value not being respected #174

Closed hashimaziz1 closed 2 years ago

hashimaziz1 commented 2 years ago

I just tried generating and saving 50,000 hashids to my database as a test for my Laravel app and it doesn't seem that the padding parameter is being respected. I'm seeing plenty of IDs lower than requested, some consisting of just one digit, as well as a few hundred duplicates.

Here is the model code I used to generate the IDs, which passes a minimum length of 8 digits:


    public static function generatePaymentID($request) {

    ...
    $transaction->save(); // First save to ensure transaction->id is populated 
    ...
    $hashids = new Hashids('projectnamesalt', 8, "ABCDEFGHJKLMNPQRSTUVWXYZ23456789");
    $transaction->payment_id = $hashids->encode($transaction->id);
    ...
    $transaction->save();
}

And here's the quick-and-dirty for loop I'm using to run the above function as part of my migration (as I said, just for testing the library):

for ($i=0; $i < 50000; $i++) {
    $manufacturer->generatePaymentID($request);
}
hashimaziz1 commented 2 years ago

Stupid mistake, I had the record set to bigInt in my migration. Weird that Laravel wasn't reporting anything but not the fault of the library.