Michael Yagudaev
Michael Yagudaev

Reputation: 6129

Convert Between Digest::MD5.digest and Digest::MD5.hexdigest

How do I convert a hash string originally encoded with Digest::MD5.digest('abc') to a hex string in the following format Digest::MD5.hexdigest('abc')? The difference is that the hex string is safe against different encoding issues (regardless of the encoding it is read in).

Upvotes: 3

Views: 1783

Answers (2)

akuhn
akuhn

Reputation: 27793

Try this

string = Digest::MD5.digest('abc')
Digest.hexencode(string)

Upvotes: 0

Michael Yagudaev
Michael Yagudaev

Reputation: 6129

Using then unpack method will do the trick. (Checkout the official documentation)

Digest::MD5.digest('http://www.example.com').unpack('H*').first == Digest::MD5.hexdigest('http://www.example.com')

Upvotes: 3

Related Questions