Vell
Vell

Reputation: 357

How to covert a string to a SHA512 hash

I am working on remote authentication for a rails 4 application. The password hash was written using a cold fusion hash library and I need to try and match what the end result in Rails to allow the user into the application.

I did see that the Digest library has something for coverting strings into SHA512 hexadecimal. But that doesn't seem to be what I need. An example snippet of a hash looks like: ü\rÀÚ힎

Is there a hashing library in Ruby that will allow me to hash a string in SHA512. Does such a thing exist or is there an external gem I could leverage?

Thanks in advance.

Upvotes: 3

Views: 6097

Answers (1)

John Ledbetter
John Ledbetter

Reputation: 14193

Sounds like you're looking for Digest::SHA2#digest.

[3] pry(main)> Digest::SHA2.new(512).digest("test")
=> "\xEE&\xB0\xDDJ\xF7\xE7I\xAA\x1A\x8E\xE3\xC1\n\xE9\x92?a\x89\x80w.G?\x88\x19\xA5\xD4\x94\x0E\r\xB2z\xC1\x85\xF8\xA0\xE1\xD5\xF8O\x88\xBC\x88\x7F\xD6{\x1472\xC3\x04\xCC_\xA9\xAD\x8EoW\xF5\x00(\xA8\xFF"

In contrast to the hexdigest function, which will return a formatted string:

[9] pry(main)> Digest::SHA2.new(512).hexdigest("test")
=> "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"

See Digest Instance Methods.

Upvotes: 10

Related Questions