What's the best way to hash a url in ruby? -
i'm writing web app points external links. i'm looking create non-sequential, non-guessable id each document can use in url. did obvious thing: treating url string , str#crypt on it, seems choke on non-alphanumberic characters, slashes, dots , underscores.
any suggestions on best way solve problem?
thanks!
depending on how long string can use few alternatives:
require 'digest' digest.hexencode('http://foo-bar.com/yay/?foo=bar&a=22') # "687474703a2f2f666f6f2d6261722e636f6d2f7961792f3f666f6f3d62617226613d3232" require 'digest/md5' digest::md5.hexdigest('http://foo-bar.com/yay/?foo=bar&a=22') # "43facc5eb5ce09fd41a6b55dba3fe2fe" require 'digest/sha1' digest::sha1.hexdigest('http://foo-bar.com/yay/?foo=bar&a=22') # "2aba83b05dc9c2d9db7e5d34e69787d0a5e28fc5" require 'digest/sha2' digest::sha2.hexdigest('http://foo-bar.com/yay/?foo=bar&a=22') # "e78f3d17c1c0f8d8c4f6bd91f175287516ecf78a4027d627ebcacfca822574b2"
note won't unguessable, may have combine other (secret static) data salt string:
salt = 'foobar' digest::sha1.hexdigest(salt + 'http://foo-bar.com/yay/?foo=bar&a=22') # "dbf43aff5e808ae471aa1893c6ec992088219bbb"
now becomes harder generate hash doesn't know original content , has no access source.
Comments
Post a Comment