Comments (5)
I'm going to guess that you're trying to figure out a better way to compress Mongo's IDs because the current output is kinda lengthy.
There are two problems with your code that I see:
- When you do
preg_replace
with''
, you cannot safely encode/decode "5ab3" and "a53b". And when you dostr_replace
with range, you cannot safely decode because you don't know which integer was a letter (5a
=>50
or50
=>50
?). - Additionally, your
chunk_split
will have an issue when you split a string like this: "200003", because encode will think you're encoding 200 and 3, when you should be encoding 200 and 003.
So, to answer your question -- yes, looks like you'll definitely have collisions with these methods.
from hashids.
55a3f9bdd874595813000029 => 920000318595478ddb9f3a55 => 920000318595478331953055
[0] => 920000318
[1] => 595478331
[2] => 953055
[3] => time(1)
Right now i;m not care about decrypt ID back, need be only uniq, so i think to add mt_rand ord time(1) as additional param. Also MongoDB ids are normal ended with 0000001 so i just strrev base ID. So som kind of hard-fix.
Thank you for your patience
from hashids.
If you need them to be unique, I don't recommend doing what you're proposing, because you will generate duplicate ids (irregardless of what additional parameters you add). I'm not aware of any safer method other than the one provided by encode_hex
and decode_hex
.
from hashids.
$id = new \MongoId($toHash);
echo $h->encode([$id->getTimestamp(),$id->getInc(),$id->getPID(),preg_replace('/[a-zA-Z]+/', '', substr($toHash,8,6))]);
55c1f1f4e12f92d814000031
Timestamp: 1438773748
Hostname2: 1292 //MD5 3 bytes :: -A_Z
ProcesID: 5336
Increment: 49
0DWpDj6ulYUL3ZuR5
Tested on 2milion rows no duplicate, so this method is i think more clean
from hashids.
Hi @Dawjan, yes that's one of the methods listed under http://hashids.org/#alternatives (number 2). That's good that you didn't get duplicates for the first 2m rows.
from hashids.
Related Issues (20)
- Padding/minimum value not being respected HOT 1
- Laravel 9 has been released. Would be awesome if we could add support to this package 🚀 HOT 1
- Ability to encode fractions/decimal places HOT 2
- TypeError: Unsupported operand types: int - string on PHP 8.1 HOT 1
- [PHP 8.1] Deprecated: Implicit conversion from float-string to int loses precision HOT 1
- 2 particulars numbers render the exact same result whatever the salt used ???!!! HOT 1
- Is there any max length for the salt? HOT 2
- How can I get more randomness? HOT 1
- Implicit conversion from float-string HOT 1
- How to use without Composer? HOT 6
- Parsing ERROR? HOT 2
- Error Invalid text representation (hashids not decoding) with route in plural HOT 1
- Different results for large numbers between 32-bit and 64-bit HOT 1
- encode number 1 but got different values in laravel and hyperf project HOT 2
- xdffx dsdxsazazjjk
- Missing math extension for Hashids, install either bcmath or gmp. HOT 6
- HashIds not working with macOS Sonoma 14.2 HOT 3
- Github Actions produces gmp_mod(): Argument #1 ($num1) must be of type GMP|string|int error HOT 1
- gmp_mod Argument #1 ($num1) is not an integer string HOT 3
- Same result for different numbers HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hashids.