In our web application, we want to randomize record identifiers. The reason is that we want to hide how many records are already in the database, and we have unregistered things. In case the identifiers would be prime incremental numbers, it would be easy to guess the identifiers of unregistered things.
As I can see, there are three ways to do this:
Algorithm:
"#{id}--#{page_title}
:
nextval
@emboss
@viktor tron
, URL-, . ( ..).
, . ? ? Ruby on Rails 3.x PostgreSQL 9.x.
: ! , YouTube , . , . , ( ID), , URL, . , , - - , , (URL- ).
. . ( , , , "", .)
, , "" .
: .
, , , .
DB
, , , . , 100000 N :)
: . . URL.
, URL-, -/, https://github.com/norman/friendly_id
friendly_id slug- slug , , , .
, ID:)
- " ". , SHA-256 X H ; , () X H.
Ruby :
@hashed_id = Digest::SHA2.new << SHA_SALT << @foo.id
, , , . , .
, .
, , : , , , . , , . : -, . , 0 , .
, :
SecureRandom . rand , , "" , . , "" , . / , , , , .
SecureRandom
rand
, , . , .
. , , , , . , , , , , . , . -, . , : . , ID 1-100 , , , .
, , . , , , .
, ? " " , URL-, .
: https://github.com/dividedmind/pg_random_id
, readme, . , . .
, , , . , . , , . , , -: . - , , , , .
To achieve this, you must create a new column called "number" and implement one of these strategies on it. Then, if you need to switch to a new strategy, it will be much easier for you: instead of doing it Model.find(id), you just do it Model.find_by_number(number).
Model.find(id)
Model.find_by_number(number)