Giter Club home page Giter Club logo

hashids's Introduction

hashids

Build Status Monthly Downloads Latest Version

Hashids is a small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database numeric ids to users: https://hashids.org/php

Getting started

Require this package, with Composer, in the root directory of your project.

composer require hashids/hashids

Then you can import the class into your application:

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1);

Note Hashids require either bcmath or gmp extension in order to work.

Quick Example

use Hashids\Hashids;

$hashids = new Hashids();

$id = $hashids->encode(1, 2, 3); // o2fXhV
$numbers = $hashids->decode($id); // [1, 2, 3]

More Options

A few more ways to pass input ids to the encode() function:

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1, 2, 3); // o2fXhV
$hashids->encode([1, 2, 3]); // o2fXhV
$hashids->encode('1', '2', '3'); // o2fXhV
$hashids->encode(['1', '2', '3']); // o2fXhV

Making your output ids unique

Pass a project name to make your output ids unique:

use Hashids\Hashids;

$hashids = new Hashids('My Project');
$hashids->encode(1, 2, 3); // Z4UrtW

$hashids = new Hashids('My Other Project');
$hashids->encode(1, 2, 3); // gPUasb

Use padding to make your output ids longer

Note that output ids are only padded to fit at least a certain length. It doesn't mean that they will be exactly that length.

use Hashids\Hashids;

$hashids = new Hashids(); // no padding
$hashids->encode(1); // jR

$hashids = new Hashids('', 10); // pad to length 10
$hashids->encode(1); // VolejRejNm

Using a custom alphabet

use Hashids\Hashids;

$hashids = new Hashids('', 0, 'abcdefghijklmnopqrstuvwxyz'); // all lowercase
$hashids->encode(1, 2, 3); // mdfphx

Encode hex instead of numbers

Useful if you want to encode Mongo's ObjectIds. Note that there is no limit on how large of a hex number you can pass (it does not have to be Mongo's ObjectId).

use Hashids\Hashids;

$hashids = new Hashids();

$id = $hashids->encodeHex('507f1f77bcf86cd799439011'); // y42LW46J9luq3Xq9XMly
$hex = $hashids->decodeHex($id); // 507f1f77bcf86cd799439011

Pitfalls

  1. When decoding, output is always an array of numbers (even if you encoded only one number):

    use Hashids\Hashids;
    
    $hashids = new Hashids();
    
    $id = $hashids->encode(1);
    
    $hashids->decode($id); // [1]
  2. Encoding negative numbers is not supported.

  3. If you pass bogus input to encode(), an empty string will be returned:

    use Hashids\Hashids;
    
    $hashids = new Hashids();
    
    $id = $hashids->encode('123a');
    
    $id === ''; // true
  4. Do not use this library as a security measure. Do not encode sensitive data with it. Hashids is not an encryption library.

Randomness

The primary purpose of Hashids is to obfuscate numeric ids. It's not meant or tested to be used as a security or compression tool. Having said that, this algorithm does try to make these ids random and unpredictable:

There is no pattern shown when encoding multiple identical numbers (3 shown in the following example):

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(5, 5, 5); // A6t1tQ

The same is true when encoding a series of numbers vs. encoding them separately:

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // wpfLh9iwsqt0uyCEFjHM

$hashids->encode(1); // jR
$hashids->encode(2); // k5
$hashids->encode(3); // l5
$hashids->encode(4); // mO
$hashids->encode(5); // nR

Curse words! #$%@

This code was written with the intent of placing the output ids in visible places, like the URL. Therefore, the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other:

c, f, h, i, s, t, u

hashids's People

Contributors

4kimov avatar arzeth avatar bonfante avatar dependabot-preview[bot] avatar grahamcampbell avatar ignaciogc avatar ivanakimov avatar jamband avatar jwpage avatar leihog avatar leunggamciu avatar m1guelpf avatar mcmillanthomas avatar mtillmann avatar nyholm avatar pablofmorales avatar peter279k avatar philetaylor avatar ptondereau avatar sokool avatar stof avatar szepeviktor avatar tothimre avatar trismegiste avatar vinkla avatar w33ble avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hashids's Issues

Typo in pitfall example?

Hi there!

I guess there is a typo in the pitfall example that can be found here: https://github.com/ivanakimov/hashids.php#pitfalls

This is the example:

use Hashids\Hashids;
$hashids = new Hashids();
$id = $hashids->encode(1);
$hashids->encode($id); // [1]

But instead of $hashids->encode($id) shouldn't it be $hashids->decode($id)?

Best regards,
Rafael Pacheco.

Mac issues

Having this issue on 4 macs, but not on Ubuntu 14.04:

All math libs are on, but when trying to use a number that has more than 10 digits I get a blank screen in the browser, without any errors (php error reporting is on, error logs remain empty).

To fix it I have to make this change manually:
private $_max_int_value = PHP_INT_MAX;

or

$this->_lower_max_int_value = PHP_INT_MAX;

probably this variable
$this->_lower_max_int_value
must go after this block:
if ($this->_math_functions) { $this->_max_int_value = PHP_INT_MAX; }

and not before

convert id sequence id to random unique id

hi thank you for hashids

i need convert sequence number form 1 to 10000000 to random and unique code (just number 8 digit)

can the hashids do this convert ??

very very thanks.

PHP Warning: gmp_add(): Unable to convert variable to GMP

hello i'm using the wordpress plugin based on hashids php, i used it since 2 years, and everything was working good... i had a dedicated server with php5.6 and everything it was need to make the plugin work, but since 2 weeks i switch to a new host.

it come with php7 but since then i got this error into my error_log
PHP Warning: gmp_add(): Unable to convert variable to GMP at line 314 of hashids.php and when i took a look it's this...

$number = $this->_math_functions[โ€˜strโ€™]($this->_math_functions[โ€˜addโ€™]($number, $pos * pow($alphabet_length, ($input_length โ€“ $i โ€“ 1))));

the wordpress plugin is based on hashids 1.0.6 and i know that a lot's of version have been release since then, i contact ivan and he told me to check if GMP and BCMath was install on my machine and then to try here that maybe someone could help me... i contact fastcomet and they told me both extension are install and working so how can i fix this up if anyone have an idea.

Removing Uppercase Characters?

I would like my hashids to only contain numbers and lowercase characters, and I see the following character set for generating the ids:

private $_alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';

Can I override this with a lowercase alphabet only:

private $_lower_alphabet = 'abcdefghijklmnopqrstuvwxyz1234567890';

What are the chances of a collision and what is the max limit in the generated ids?

Thanks

It might be useful to keep old versions

I was using 0.3.@dev and now I try to pull this down I get issues as the latest is now 1.0.0, this could potentially break a website since there is no copy of 0.3.@dev available anywhere. It would have probably been better to keep a copy of that so that users can upgrade to the latest by choice and not by force.

Can't decode Hash

Hi, i just downloaded your library, awesome work y'all got going on here. But the problem is the after i encode, i'm unable to decode back to the number and can't really see what i'm doing wrong here. i get an array to string error.

$hashids = new Hashids\Hashids('mysalt',6,'abefghijABCDIJ12789');

    $encoded = $hashids->encode(12);

    echo 'Encoded: '.$encoded.'</br>';

encoding works fine, but to decode back is where the problem lies and i can't say for sure if its a bug.

$decoded = $hashids->decode($encoded);
but this throws back an error - Notice: Array to string conversion in C:\xampp....oop.php on line 21

What can i do to get this working and all...

Minimum Alphabet Length

What is the consequence of changing the minimum alphabet length to 10.

I want my alphabet to be "0123456789"?

I changed your code. I only want to encode numbers and it seems to work.

Any way to increase get_max_int_value() on 32bit systems?

I do the following, and I receive this:

echo $hashids->get_max_int_value(); //2147483647

Is there any alternative means to increase the max int value on a 32bit system? I have fairly large numbers I need to decode and encode.

Saving hashes?

Hi.
Thank you, it's working like a charm!
I have a little question for best practice.
What do you think is the most accurate?

  • To decrypt hashes for each request then fetch the corresponding item within the database?
    Or
  • To store the hash within the database?

With best regards.

Generate hashs with just one charactere

Hey!

First of all congrats to the amazing package and thanks a lot for making this open source :-)

Currently is there any way to generate a hash with length of 1? Like integer 1 converting into m, for example? I think the smallest hash is from 2 chars length, right?

Thanks for helping.

Guessing the salt with a number of hashids

I read somewhere that it could be possible to figure out the salt, with a good number of hashids. Like 30. Is this true?

I am actually not really totally concerned, because we'll have other precautions set up for a proper API, but knowing this is true is simply good to know, in order add more precautionary steps to the API.

Thanks for any insight.

Scott

A lot of collisions. What's wrong with my code?

I use version 1.0.1
My input data:

salt: randomsalt
min_hash_length: 10

First I created an SQL table: id (int), hash (varchar)

Then tried to generate hashids:

for ($i = 1; $i < 10000; $i++) {
   $hid = $hashIds->encode($i);
   $conn->executeQuery("insert into t(id, hash) values ($i, '$hid')");
}

and what I have:

select count(id) cnt, group_concat(id) ids, `hash` from t group by `hash` having cnt > 1 order by cnt desc 
cnt ids hash
2 231,1375 0GwrjWYBmN
2 2278,3334 1kn24qyBpw
2 8378,9378 1kn2MDWKpw
2 8078,9134 1kn2MqYKpw
2 6134,7178 1kn2RqWnpw
2 634,1634 21kn2DWBpw
2 378,1134 21kn2eYKpw
2 534,678 21kn2pwBpw
2 3313,8813 3DnGdM9r6N
2 8957,9313 3DnGDoar6N
2 2357,8213 3DnGdPar6N
2 4257,5313 3DnGjqvK6N
2 6213,6257 3DnGVPPK6N
2 8926,8970 3JK12XGnAa
2 5026,5070 3JK1JXqnAa
2 6303,7203 53na1ddn6q
2 6803,6891 53na1Emn6q
2 6391,7291 53na1gdn6q
2 5491,5791 53na3aMB6q
2 3903,5291 53na3WDB6q
2 4203,5391 53na3yoB6q
2 4691,5747 53na3ZmB6q
2 8547,9603 53na8ZdK6q
2 2747,3803 53naQZJn6q
2 6481,7137 5PrxpJYrGV
2 5981,6937 5PrxppvrGV
2 9422,9466 68KA2mbnGJ
2 8222,9366 68KA2XmnGJ
2 9522,9566 68KA2YvnGJ
2 3622,3666 68KAgm3rGJ
2 3722,3766 68KAgYJrGJ
2 7522,7566 68KAjmDnGJ
2 4322,5466 68KAVXXnGJ
2 632,1776 7ROBzzvBAX
2 2205,2249 8zBM0der0q
2 2449,3505 8zBM0V9r0q
2 9949,9993 8zBMedeB0q
2 8049,8093 8zBMPdXr0q
2 8249,9305 8zBMPV7r0q
2 4149,4193 8zBMQdMB0q
2 4393,5449 8zBMQVYB0q
2 4705,4793 8zBMQzXB0q
2 6105,6149 8zBMyd1B0q
2 6293,7349 8zBMyV0B0q
2 6605,6693 8zBMyzeB0q
2 5214,5258 9RrO1gRK5O
2 2714,2758 9RrO4zgB5O
2 8514,8558 9RrODzRB5O
2 3340,3640 a0n08GPnGL
2 3040,3140 a0n08MVnGL
2 7440,6484 a0n0EdVrGL
2 53,97 Aq2nwlMrQb
2 1309,1753 Aq2nwVPKQb
2 4515,5659 b7nQDjmKZR
2 8015,8059 b7nQPlLKZR
2 7959,9315 b7nQPPqKZR
2 2215,2259 b7nQVlZrZR
2 2715,3559 b7nQVXQrZR
2 6115,6159 b7nQylonZR
2 4077,5177 Dqno3vVnwL
2 7933,9033 DqnovvJKwL
2 2133,3233 DqnoWvpnwL
2 350,1406 dZOKyPOB1J
2 150,194 dZOKyxdB1J
2 1800,1888 EPAnDAArdJ
2 6448,6492 GqnL0ganEy
2 7148,7192 GqnL0wZnEy
2 7404,7448 GqnL0x1nEy
2 9104,9148 GqnL7weKEy
2 9348,9392 GqnL7xZKEy
2 4504,4548 GqnLMgyrEy
2 5204,5248 GqnLMw1rEy
2 3548,3592 GqnLVxpBEy
2 6435,7679 GVrJ1Wgn1v
2 2135,3179 GVrJ4aGK1v
2 4079,5135 GVrJda6K1v
2 8431,8475 Gwrjal7nmN
2 8031,9175 GwrjaxonmN
2 4131,5275 GwrjdWEKmN
2 2631,2675 GwrjMlOnmN
2 2231,3375 GwrjMxwnmN
2 6031,7175 GwrjVWVrmN
2 7874,7930 gXKedDZBLx
2 5130,9530 gXKeDqXBLx
2 648,692 jGqnLgvKEy
2 1348,1392 jGqnLwpKEy
2 1604,1648 jGqnLxZnEy
2 760,1860 JWen8OpKvk
2 9511,9599 kLn52EgrPw
2 9055,9455 kLn52LZrPw
2 3711,3799 kLn56EMrPw
2 3911,5099 kLn5DOzKPw
2 7611,7699 kLn5REvrPw
2 5811,6999 kLn5RORrPw
2 6199,6855 kLn5RqzrPw
2 6899,7211 kLn5RxZrPw
2 6256,9556 LDKgdA8nyx
2 7912,7956 LDKgDdwnyx
2 6656,8612 LDKgdJqnyx
2 7512,8656 LDKgdmQnyx
2 6756,7812 LDKgdpenyx
2 8312,7256 LDKgDqlnyx
2 8912,8956 LDKgDvenyx
2 4012,4056 LDKggd8Kyx
2 5612,5656 LDKggLeKyx
2 4412,5312 LDKggqqKyx
2 9812,9856 LDKgOdXKyx
2 3112,3156 LDKgPvQnyx
2 5846,6902 lGBP4eyBLe
2 7646,7690 lGBP4qPBLe
2 6446,7546 lGBP4VRBLe
2 7746,8802 lGBP7eMrLe
2 8390,9490 lGBP7VGrLe
2 8402,9502 lGBP7VQrLe
2 1946,3002 lGBP9e3KLe
2 3746,3790 lGBP9qlKLe
2 2590,3690 lGBP9VyKLe
2 3890,4946 lGBPMeNKLe
2 5302,5390 lGBPMpRKLe
2 5702,5746 lGBPMqJKLe
2 4490,5590 lGBPMVMKLe
2 4502,5602 lGBPMVVKLe
2 8923,8967 LlBkdpQKOq
2 5023,5067 LlBkLpxKOq
2 3523,3567 LlBkNGyrOq
2 3123,3167 LlBkNpVrOq
2 7423,7467 LlBkyGWnOq
2 6124,6768 N2npkZxKAJ
2 1623,1667 NLlBkG6BOq
2 4910,4954 NRrW1oLKeM
2 6854,6898 NRrWgowreM
2 6654,7710 NRrWgpVreM
2 2254,3110 NRrWNlVneM
2 2954,2998 NRrWNoMneM
2 2798,3854 NRrWNpLneM
2 8598,9654 NRrWypzKeM
2 1003,1091 O53naEMn6q
2 7762,9162 o6BX1dZKRv
2 2518,3318 o6BXlazBRv
2 7718,5862 o6BXyLZKRv
2 2538,3238 OgB4EAQKVG
2 2782,3838 OgB4ExJKVG
2 6582,6938 OgB4MWqKVG
2 6682,7738 OgB4Mx8KVG
2 8582,9638 OgB4RxYnVG
2 1846,1890 olGBPqQKLe
2 646,1746 olGBPVGnLe
2 2839,2883 P8Kd5Pony2
2 8839,8883 P8Kdde7Ky2
2 8639,8683 P8KddP4Ky2
2 4939,4983 P8KdwexBy2
2 7744,8888 PAnD4jjndJ
2 7600,7688 PAnDqAgrdJ
2 5844,6988 PAnDqjxrdJ
2 3900,5044 PAnDVj8BdJ
2 3700,3788 PAnDXA4ndJ
2 1944,3088 PAnDXj1ndJ
2 5919,7019 PRnY1DonwQ
2 3963,5063 PRnYDD1KwQ
2 4419,4463 PRnYDlVKwQ
2 4363,5419 PRnYDZOKwQ
2 7863,8963 PRnYEDknwQ
2 8263,9319 PRnYEZznwQ
2 3263,3719 PRnYgAOBwQ
2 2063,3163 PRnYgDmBwQ
2 2119,3763 PRnYgEOBwQ
2 2263,2963 PRnYgOVBwQ
2 2463,3519 PRnYgZyBwQ
2 4380,4880 Q0BZ7dlK7k
2 4836,5436 Q0BZ7OLK7k
2 5236,5280 Q0BZ7p3K7k
2 3336,3380 Q0BZVpxB7k
2 9136,9180 Q0BZYpWr7k
2 1953,5253 q2nwlawrQb
2 2209,3353 q2nwlJMrQb
2 4153,5297 q2nwLJwrQb
2 3953,3997 q2nwLlprQb
2 2009,2053 q2nwllVrQb
2 3397,5509 q2nwlPMrQb
2 4653,5053 q2nwLwPrQb
2 9753,9797 q2nwQlVBQb
2 8009,9153 q2nwXJlrQb
2 7809,7853 q2nwXlZrQb
2 9053,9497 q2nwXVPrQb
2 6109,7253 q2nwyJpBQb
2 5853,5897 q2nwyllBQb
2 179,1235 qGVrJa1n1v
2 6642,7186 QlnV4VqB1X
2 8886,9342 QlnVdXqB1X
2 4942,5586 QlnVMGQK1X
2 2742,3342 QlnVwVQr1X
2 305,349 R8zBMdXr0q
2 493,1549 R8zBMV6K0q
2 805,893 R8zBMzMK0q
2 177,1277 RDqnovWKwL
2 4621,4665 RoBqDagrGz
2 4165,5221 RoBqDJVrGz
2 2721,2765 RoBqgaXnGz
2 2065,2421 RoBqgDGnGz
2 8521,8565 RoBqNaVKGz
2 5921,6321 RoBqWDvnGz
2 6465,6865 RoBqWEGnGz
2 6821,7565 RoBqWognGz
2 2476,3376 ROBz1pprAX
2 7776,7976 ROBzEdPrAX
2 6432,7576 ROBzxzXnAX
2 1301,1689 RW0n7ygBzA
2 9420,9464 V3Bvayarbl
2 7520,7564 V3BvbyzBbl
2 3620,3664 V3BvOyJKbl
2 413,457 v3DnGPNr6N
2 357,1413 v3DnGqeB6N
2 273,1373 Vw7BmGbnz8
2 4645,4989 W0n7OGgnzA
2 2545,3189 W0n7PAdrzA
2 3501,3801 W0n7PZgrzA
2 6445,6901 W0n7VMDKzA
2 2443,3087 W6B3WVGBYw
2 2229,3329 w7Bm6GVKz8
2 8029,9129 w7BmMGpBz8
2 4173,5273 w7BmyGOBz8
2 4329,4373 w7BmyXoBz8
2 6173,7729 w7BmzAvKz8
2 5829,6973 w7BmzdpKz8
2 6073,7173 w7BmzG6Kz8
2 6573,7473 w7BmzooKz8
2 4660,5760 Wen8aOWrvk
2 8516,9616 Wen8jOmBvk
2 6560,7660 Wen8wOjKvk
2 2716,3816 Wen8xODnvk
2 1311,1711 WkLn5LZKPw
2 564,1320 WV3BvJpBbl
2 7795,8895 WvnE1j8nyP
2 7807,8907 WvnE1jQnyP
2 7907,9051 WvnE1VanyP
2 8407,8451 WvnE1yVnyP
2 3951,5051 WvnEdjgKyP
2 4007,5151 WvnEdVVKyP
2 5707,5795 WvnEdWaKyP
2 4551,4595 WvnEdyQKyP
2 2007,3107 WvnEjj4ryP
2 1995,3095 WvnEjjyryP
2 2107,3251 WvnEjV3ryP
2 2607,2651 WvnEjyEryP
2 5895,6995 WvnEVjwryP
2 5951,7095 WvnEVVEryP
2 6507,6551 WvnEVy3ryP
2 685,1785 WX9KNZWKvD
2 8441,9541 X9KNaZvBvD
2 4541,5641 X9KNeZ0BvD
2 6485,7585 X9KNVZdKvD
2 7008,7508 xOB6Expraz
2 2008,2696 xOB6GGPKaz
2 2308,2396 xOB6Gq1Kaz
2 8108,8196 xOB6wqpKaz
2 119,1219 YPRnYDkKwQ
2 265,1321 yRoBqJgKGz
2 56,956 zLDKgPqnyx
2 1054,1098 ZNRrWoAreM
2 854,1910 ZNRrWpzBeM
2 9850,9894 ZOKylxwB1J
2 3106,3194 ZOKyMggK1J
2 2250,3306 ZOKyMP6K1J
2 2106,2150 ZOKyMxwK1J
2 8806,6606 ZOKyXD6B1J
2 6350,8150 ZOKyxezB1J
2 8906,8994 ZOKyXgdB1J
2 9594,6294 ZOKyXOyB1J
2 6306,9606 ZOKyxozB1J
2 6150,7206 ZOKyxP4B1J
2 8094,9150 ZOKyXPOB1J
2 7506,8606 ZOKyxwgB1J
2 7494,8594 ZOKyxwzB1J
2 7906,7950 ZOKyXxjB1J
2 5950,5994 ZOKyxxZB1J
2 5906,6394 ZOKyxyZB1J
2 4194,5250 ZOKyyPzK1J
2 4006,4050 ZOKyyxyK1J
2 51,1151 ZWvnEj8ryP
2 151,1295 ZWvnEVQryP
2 1807,1895 ZWvnEWVByP

Default alphabet has repeated characters

For most of the versions from which I read the code (I didn't take a look at all of the implementations), the default alphabet contains 2 'f', 2 'C' and 2 'H'.

The consequence is that if you call Encode with the default params you have a value, and if you call it with the default alphabet you have another value. The uniqueness of chars should be ensured even if the default params of Encode are used.

Alphabet

'1234567890abcdefghij' != 'abcdefghij1234567890'. Why?

$hashids = new Hashids\Hashids('this is my salt', 8, '1234567890abcdefghij');
echo $hashids->encode(123);
// Return 428geg85

$hashids = new Hashids\Hashids('this is my salt', 8, 'abcdefghij1234567890');
echo $hashids->encode(123);
// Return d029b657

encode(0xffffffff) returns empty string

To reproduce:

<?php

require_once 'vendor/autoload.php';
$hashids = new Hashids\Hashids();
$id = $hashids->encode(0xffffffff);
var_dump($id);

echo "Hashids.php version is ".Hashids\Hashids::VERSION."\n";

Output:

string(0) ""
Hashids.php version is 1.0.5

PHP version:

$ php -v
PHP 5.6.3 (cli) (built: Nov 16 2014 08:32:30) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies

Public salt

Hi there,

as you didn't declare the variable $_salt as private in the Hashids class the salt is accessible from outside, because a not declared variable is added as a public member when used with $this.

So
$hash = new Hashids('xyz');
echo $hash->_salt;
would print 'xyz'.

This is very critical.

Retrieving only value as variable.

Good day sir.
I am currently trying to integrate your plugin to my project. However i am encountering some difficulty as php newbie.
the var_dump output the result as this
string 'pAnQlkkm' (length=8)

but what i am interested in is only this 'pAnQlkkm'.
I will be glad if you can help do that...

Encode over 1000000000 fails

Hi,

I try to little obfuscate timestamps in seconds, but on Ubuntu PHP 7.0.8-0ubuntu0.16.04.3 (x64) it fails to encode numbers which are larger than 1000000000 (exactly, I tested with loops finding breaking point). Also it is able to multiply, substract etc. these numbers.

Oddly, it works on my Windows localhost too PHP 7.0.13 (x64). Both Ubuntu and localhost are truly x64, checked PHP_INT_MAX && PHP_INT_SIZE.

Have anyone come over this issue?

Here's sample I test:

$hash = new Hashids();

var_dump(	$hash->encode( 1000000000 )	) ; //Works
var_dump(	$hash->encode( 1000000001 )	) ; //Fails

Result from above on Ubuntu:

string(7) "gn5m6r3" string(0) ""

And from Windows:

string(7) "gn5m6r3" string(7) "j02pLxl"

PHP Bug

Hello Ivan,

i am using your api and it is really good but there is only one problem and i need solution urgent. your PHP and OBjECTIVE C api is not giving me same result with same salt. i have tried OBJECTIVE C , JAVA , .NET and they are generating same result but PHP is not generating same result. HELP NEEDED.

PHP :-

  require_once(__DIR__.'/../lib/Hashids/Hashids.php');

  $hashids = new Hashids\Hashids('this is my salt');

  $id = $hashids->encode(1,2,3);

  $numbers = $hashids->decode($id);

  var_dump($id, $numbers);
  exit;

giving result after encoding is "laHquq".

OBJECTIVE C:-

   Hashids *hashids = [Hashids hashidWithSalt:@"this is my salt"];
    NSString *hash = [hashids encrypt:@1, @2, @3, nil];

    NSLog(@"%@", hash);

    NSLog(@"%@", [hashids decrypt:hash]);

giving result after encoding is "eGtrS8".

See both results are not same after encoding . i know PHP api has bug. Please help me out. i waiting for your reply

Thanks

Using the library without composer / on a Paas

I am pretty new to php and I am used to declaring libraries like this

require('config.php');

I was wondering how can I use the hashid library without using composer as composer too is new to me. I have my website hosted on a Platform-as-a-Service (PaaS) and so a little stuck at this point. Appreciate any help / nudge in the right direction. thanks

gmp_div(): Unable to convert variable to GMP - wrong type

Error message: gmp_div(): Unable to convert variable to GMP - wrong type

Serveur config is: ubuntu 14.04, apache 2.4, php7.0.8 (FPM).

This issue may or may not be a bug but could help someone so I'm posting it here. It was solved by adding $hashids->_lower_max_int_value = PHP_INT_MAX;

I first installed the gmp and bcmath math libraries :

apt-get install php-gmp
apt-get install php-bcmath
echo "gmp_add: ". function_exists( 'gmp_add' );
echo '<br/>';
echo "gmp_div: ". function_exists( 'gmp_div' );
echo '<br/>';
echo "gmp_strval: ". function_exists( 'gmp_strval' );
echo '<br/>';
echo "bcadd: ". function_exists( 'bcadd' );
echo '<br/>';
echo "bcdiv: ". function_exists( 'bcdiv' );
echo '<br/>';
echo "strval: ". function_exists( 'strval' );
echo '<br/>';

$diff = 3;
$unique_salt_value = "What a yolo world!";
$minimum_id_length = 8;
$custom_alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$hashids = new Hashids\Hashids($unique_salt_value, $minimum_id_length, $custom_alphabet);
// Uncomment this removes the 500 error.
//$hashids->_lower_max_int_value = PHP_INT_MAX;
echo $hashids->encode( round(microtime(true)) + intval($diff) );
exit;

Tags for previous versions

My composer.json is broken after last update. I use cayetanosoriano/hashids-bundle
@ivanakimov, can you return removed git tags and versions to packagist?

Thanks.

First encrypt very slow

Hello,

I tried multiple times but no matter what, the first time I encrypt an id, it takes like 20-30ms which is a lot if I have to do it often.

In a foreach loop, I encrypt 20 object id, here are the encrypt duration time.

Try 1 :
0: 0.025040864944458
1: 0.00029420852661133
2: 0.00028109550476074
3: 0.00027704238891602
4: 0.00027799606323242
5: 0.00028610229492188
6: 0.00028586387634277
7: 0.00036287307739258
8: 0.00029206275939941
9: 0.00027704238891602
10: 0.00028586387634277
11: 0.00028300285339355
12: 0.00027799606323242
13: 0.00027918815612793
14: 0.00028896331787109
15: 0.00028800964355469
16: 0.00028014183044434
17: 0.00027894973754883
18: 0.00028109550476074
19: 0.00028109550476074

Try 2 :
0: 0.029690990447998
1: 0.00029087066650391
2: 0.00027799606323242
3: 0.00029301643371582
4: 0.00027203559875488
5: 0.00025010108947754
6: 0.00029110908508301
7: 0.00027799606323242
8: 0.00028300285339355
9: 0.00028300285339355
10: 0.0012109279632568
11: 0.0051960945129395
12: 0.00028800964355469
13: 0.00033783912658691
14: 0.00028300285339355
15: 0.00027799606323242
16: 0.00030088424682617
17: 0.00027894973754883
18: 0.00027704238891602
19: 0.00027704238891602

The max int value on my machine is : 9223372036854775807

Those tests have been run on a Vagrant VM so I know that it'll get faster once I'll deploy on a vps but the first encrypt would still be too slow.

Any idea ?

Anyway thanks for the great plugin :)

Exception: alphabet must contain at least 16 unique characters

Hi
First thank you for this awesome script ! i use it for almost all my projects.

I was using this lines of code before and worked perfectly:

$hashids = new Hashids\Hashids(ENCRYPTION, 10, '0123456789');
$numbers = $hashids->encrypt($encryptit);	

Until today when he show me this error message:
Exception: alphabet must contain at least 16 unique characters

How to solve it please ?

I am using this version: 0.3.1

Generate nice ID base on MongoDB ObjectID

I try to find way to replace MongoDB objectID with Nice id that i can use even in URL

$toHash = '55a3f9bdd874595813000029';
$hashids = new \Hashids\Hashids( 'Coll global salt' );
//$toHash = preg_replace('/[a-zA-Z]+/', '', $toHash); //collision or not collision this is question :p
$toHash = str_replace(range('a', 'z'),range(0,9),$toHash); // or better one - avoid dual numbers
$ID = explode('#',chunk_split($toHash,9,'#',-1));
echo $hashids->encode( $ID ) . '<br />';

Results:
55c21840e12f92701400002b => bKlpzGs2R7qvC6
55a3f9bdd874595813000029 => pQR0p6atY1tRrYvqU87RZZi4

Also will be nice if we had function:
$hashids = new \Hashids\Hashids( 'Globaluhashuu' );
$hashids->MongoID_encode('55a3f9bdd874595813000029') => bKlpzGs2R7qvC6
$hashids->MongoID_decode('bKlpzGs2R7qvC6') => 55a3f9bdd874595813000029

No version information in zip file

I have tried using hashids in a new Drupal site that I'm building using the corresponding Drupal module but it was referring to an old version of the library (not sure which one exactly but there is a reference to a lib/Hashids/HashGenerator.php file).

Anyway, I have made it work, fixed a couple of issues and I would like to submit it back to the Drupal community and unfortunately I can't find a way to grab the version number from any of the files in the package (Drupal libraries require that you specify a regex for them to understand what version of the library they are using; old one looked like this:

    'version arguments' => array(
      'file' => 'lib/Hashids/Hashids.php',
      'pattern' => '/const VERSION = \'([0-9\.]+)\';/',
      'lines' => 20,

).

Is this something that can be added back in the library?

Adding it as a comment would work just fine.

Unhandled Exception on Windows 7 64bit

Unhandled Exception
Message: Uninitialized string offset: -12
Location: Hashids\Hashids.php on line 111

Code that errors out:
encrypt( 339585589, 2147483647 );

same output for 2 different input

i hash two different input sample, which results same output for both input.

code 1 :- (input - .testClass3)
$hashids = new Hashids\Hashids('testClass3', 12, 'aAbBcCdDeEfFgGhHijJ');
$id = $hashids->encode(1,2,3);
var_dump($id); ---> Output : string 'jDbfAFBj' (length=8)

code 2 :- (input - .testClass4)
$hashids = new Hashids\Hashids('testClass4', 12, 'aAbBcCdDeEfFgGhHijJ');
$id = $hashids->encode(1,2,3);
var_dump($id); ---> Output : string 'jDbfAFBj' (length=8)

is there any bug with my code or in yours ??

Add StyleCI Support

What about adding https://styleci.io/ support? Then we will get syntax checks on all commits and PRs made to this repository. It can even fix all syntax errors automagically.

This is the guide to add StyleCI support:

  1. Sign up at https://styleci.io/

  2. Go to the repositories page https://styleci.io/account#repos

  3. Enable this repository.

  4. Press Show Analysis and then Settings.

  5. Add the following settings:

    enabled:
    - unalign_double_arrow
    
    disabled:
    - align_double_arrow
    - phpdoc_align
  6. Press Save Configuration.

  7. Press Analyze Now.

This has to be done by the owner of the repository.

url as hash ?

Hi,

This look like a very nice library but I wonder if it's good to use a part of a url, or a uuid as hash.

Can you give your opinion about this ?

Thank you.

v2.0.1 has changed encoded numbers

$salt = 'this is my salt';
$minHashLength = 12;
$hashids = new \Hashids\Hashids($salt, $minHashLength);

v2.0.0 result
var_dump($hashids->encode(1)); // returns DngB0NV05ev1

v2.0.1 result
var_dump($hashids->encode(1); // returns B0N444444Vd5

Is this change intended?

Length of number effects length of hash?

For single numbers I'm only getting a hash length of 2 characters. Is this intended or something to do with my implementation? I was expecting to see consistent length hashes irrelevant of input (like MD5, or SHA1).

Use without composer

Hi. I want to implement this library but I can't use composer. How could I do it?

duplicates of hashes

Hello! I use Hashids 1.0.6 and received duplicates of hashes.

$hashids = new Hashids ('fabrika', 8);
echo $hashids->encode(53); // 3o1pkgy5
echo $hashids->encode(97); // 3o1pkgy5

Decode should return a string

The encode function return's a string when 1 number is passed to it, but the decode function returns an array when 1 hash is passed. Can this be changed to return a string to match how encode works? This is probably the most common use case and I don't understand why it doesn't work this way already. Thanks!

Composer Compatibility Broken

Howdy,

Wonderful package you got here. However, it looks like something changed recently.

Problem 1
  - The requested package hashids/hashids 0.3.*@dev could not be found.

Potential causes:
 - A typo in the package name
 - The package is not available in a stable-enough version according to your minimum-stability setting
   see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.

Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.

I wonder if you recently removed that branch or something. This causes my composer update to fail now.

I'm using "hashids/hashids": "0.3.*@dev", but now it looks like I must change to "hashids/hashids": "dev-master"

The problem with this though is that if you make any other backwards-incompatible changes I don't want to upgrade. Could you please re-add a version tag?

Thanks.

Best way to generate arbitrary hash

(Great library!)

I'm creating hashes to obfuscate sequential integer IDs in URLs. I don't care what the resulting hash is and I will never need to decode it. The only thing that matters is uniqueness. I would just encode() the IDs of the records, but I don't know the ID yet at the time of creating it (and I want this field to be required). My backup plan is to use time(), but I see on the readme.md that big numbers can be slow to encode. Is there a better way to do this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.