14

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.

Karol Selak
  • 4,248
  • 6
  • 35
  • 65
Vitali Zakharoff
  • 353
  • 1
  • 3
  • 18
  • 8
    1) SHA1 is for hashing, not for encryption. Check, e.g., Wikipedia (http://en.wikipedia.org/wiki/Cryptographic_hash_function) to read up on basics. 2) What *exactly* isn't working? – atzz Mar 28 '11 at 07:58
  • SHA-1 is has been shown to be insecure. Consider using safer alternatives, such as SHA-256, or SHA-3. https://shattered.io/ – Jonas Elfström Mar 23 '17 at 12:53

1 Answers1

31

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}")
Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106