MxLDevs
MxLDevs

Reputation: 19546

Convert digest strings into byte strings

Suppose I hash a filename as such

require 'digest'
hashed = Digest::SHA256.digest("test")
path = "/myFile/%s" %hashed
p path

This would give me a path equal to

/myFile/\x9F\x86\xD0\x81\x88L}e\x9A/\xEA\xA0\xC5Z\xD0\x15\xA3\xBFO\e+\v\x82,\xD1]l\x15\xB0\xF0\n\b"

This isn't what I want. Instead, I want the bytes to be literally represented as a string. The hash of "test" is 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08, and my desired path is

/myFile/9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08

How do I achieve this?

Upvotes: 0

Views: 206

Answers (1)

sameers
sameers

Reputation: 5095

The method you're looking for in the module is hexdigest - see the example at the top of that page.

So your code should read:

require 'digest'
hashed = Digest::SHA256.hexdigest("test")
path = "/myFile/%s" %hashed

Upvotes: 1

Related Questions