Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 696
Vitali Zakharoff Avatar asked Mar 28 '11 07:03

Vitali Zakharoff


1 Answers

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}")
like image 134
Jonas Elfström Avatar answered Oct 05 '22 07:10

Jonas Elfström