SHA1 hashing in Rails
I have database with following fields: id
, q_id
, text
, session
etc. and I have already 2 records there. I want to hash each line with SHA1 alghoritm (every line is unique of course). I tried this:
@w = Digest::SHA1.hexdigest(id+q_id+text+session)
but it doesn't work.
SHA is not encryption but rather it creates a cryptographic hash. If that is still what you want to do, my guess is that id
and q_id
are Fixnums and needs to be converted to strings.
@w = Digest::SHA1.hexdigest(ans.id.to_s + ans.q_id.to_s + ans.text + ans.session)
I also kind of like to use String literals because it makes it very obvious that we are dealing with a string
@w = Digest::SHA1.hexdigest("#{id}#{q_id}#{text}#{session}")
精彩评论