RoR - MD5 generation

Mithun Sreedharan picture Mithun Sreedharan · Nov 22, 2010 · Viewed 58.9k times · Source

How can I encrypt a string with MD5 in Rails 3.0 ? pass = MD5.hexdigest(pass) in a model yields uninitialized constant MyModel::MD5

Answer

joschi picture joschi · Nov 22, 2010

You can use Digest::MD5 from the Ruby standard library for this.

irb(main):001:0> require 'digest/md5'
=> true
irb(main):002:0> Digest::MD5.hexdigest('foobar')
=> "3858f62230ac3c915f300c664312c63f"

And one more thing: MD5 is a hash algorithm. You don't "encrypt" anything with a hash algorithm.