Search code examples
algorithmbase64encode

Encode array to fixed length string


I'm trying to implement a similar function to PCPartPicker's list permalink function.

https://au.pcpartpicker.com/list/

basically generate a permalink based on the items in the list. The key part is to generate a string which should be:

  1. unique
  2. persistent
  3. fixed length

I'm thinking about encoding an array contains product id, but can't find the right way to implement it.

Base64 and the similar (like Hashids library) can ensure it's unique and persistent, but it ends up quite long when the array has many items.

Is there other way to encode the array or is there other direction I can implement this function?

Thank you in advance.


Solution

  • One can't generate unique fixed length string for arbitrary length list that will contain all info - there is always some length that can't fit.

    Since your site has database, you can generate UUID and store list in DB along with UUID. To save space and efforts you can save it into DB only when user presses "get permalink" button or something like that.