Nikhil Razdan
Nikhil Razdan

Reputation: 21

How do I convert a Base 64 encoded md5 hash back into its hex representation in Ruby?

I'm trying to convert a Base64 encoded md5 hash into its hex representation using Ruby.

i.e. "+4aRl0iJ84yd/l1oZcT8ww==" becomes "fb8691974889f38c9dfe5d6865c4fcc3"

I've tried using the Base64 library, but Base64.decode doesn't work for my purposes.

I've found a website that works https://base64.guru/converter/decode/hex but I'm not sure how to replicate this in code.

Does anyone know how I can achieve this?

Thank you!

Upvotes: 2

Views: 60

Answers (1)

brc-dd
brc-dd

Reputation: 13044

You'll need to unpack too:

Base64.decode64(str).unpack1('H*')

playground

On ruby < 2.4, unpack1 isn't available, so do this instead - .unpack('H*')[0]

Upvotes: 1

Related Questions