1

What i want to do is to make an obfuscate id when i show a user url, i want to have an obfuscate id with exactly 16 characters in length, and without collision, so for example instead of

http://localhost:3000/users/2

i want something like this :

http://localhost:3000/users/3a5643f943cc3a44

i already find the same question in other posts here and here and also others... but can't find an answer of what i want exactly.

=========== Update: ===========

It's been some years now since I posted this question, I just want to mention that there is a super helpful gem called hashids which I use in all my recent projects, it's straight forward and easy to use :)

medBouzid
  • 7,484
  • 10
  • 56
  • 86

1 Answers1

1

I would recommend using SecureRandom.urlsafe_base64. This following related question can also help: Best way to create unique token in Rails?

16 characters is the default length (but the length is variable, as can be seen in the documentation: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html#method-c-urlsafe_base64)

Community
  • 1
  • 1
CamilleLDN
  • 850
  • 6
  • 11
  • i add the first argument for SecureRandom.urlsafe_base64 like this SecureRandom.urlsafe_base64(12, false) which gives me exactly 16 characters in length, but i'm not sure what is the number of possibility when take juste 16 in length – medBouzid Sep 23 '13 at 02:17