I need to translate some Ruby code to JavaScript and came across the following function:
def sha1_hex(h)
Digest::SHA1.hexdigest([h].pack('H*'))
end
What exactly does [h].pack('H*')
mean in this context? How would it translate to JavaScript?
I need to translate some Ruby code to JavaScript and came across the following function:
def sha1_hex(h)
Digest::SHA1.hexdigest([h].pack('H*'))
end
What exactly does [h].pack('H*')
mean in this context? How would it translate to JavaScript?
It interprets the string as hex numbers, two characters per byte, and converts it to a string with the characters with the corresponding ASCII code:
For the opposite conversion, use
unpack
:It is a little bit more difficult for non-ASCII-8BIT encodings: