views:

76

answers:

1

I am trying to hash or encrypt a record's ID for URLs such that people can't view various records simply by guessing different integer IDs.

Essentially, my URLs would be something like this: /plans/0AUTxwoGkOYfiZGd2 instead of /plans/304.

Would the best way to do this just be to use SHA-1 to hash the plan's id and store it in a hashed_id column for plans? Then, overwrite to_param and add a finder to find by hashed_id?

How do you ensure that the characters generated are 0-9, a-z, or A-Z?

Thanks!

A: 

> How do you ensure that the characters generated are 0-9, a-z, or A-Z?

idForURL = URI.escape((Base64.encode64(hash).strip))

I once encrypted the id with Blowfish, it's not super safe but it's kind of convenient and the id gets shorter than for a GUID or a SHA-1.

require 'digest/sha1'
print Digest::SHA1.hexdigest("Let's see how long this gets.")
=> b26316a2db52609f86b540de65282b9d367e085c
Jonas Elfström