bor1s
bor1s

Reputation: 4113

Generate unique hashes in Ruby/Rails


I am looking for advise about best practices of generating unique hash strings in Ruby/Rails. Usually I use MD5, SHA etc to do this, but it was not quite straightforward to choose source values for hash (timestamps not always prefered to be used).
So my question are:

  1. What values prefered to be used for generating unique hashes? (database column values, timestamps etc)
  2. Are there any gems for this kind of work?

Any advise would be appreciated.

Upvotes: 9

Views: 4784

Answers (1)

Gareth
Gareth

Reputation: 138002

Use UUIDs:

In ruby 1.9

require 'securerandom'
SecureRandom.uuid

In ruby 1.8

$ gem install uuidtools

UUIDTools::UUID.random_create

Upvotes: 19

Related Questions