Page 1 of 1

Decoding messageDigest data?

Posted: Fri May 11, 2018 9:12 pm
by ClipArtGuy
I am trying to get a SHA-256 hash of some data using LCs messageDigest function.

This code results in a jumble of weird characters:

Code: Select all

put textEncode("Data To Be Hashed", "UTF-8") into tOriginal
   put messageDigest(tOriginal, "SHA-256")
Everywhere outside of LiveCode, the hash of "Data To Be Hashed" is

Code: Select all

"8eb653d4a388eca7f4f50630867ce02b45c3b175b7075e1f002db58d12cb0209"
What step am I missing to decode the hash to a human readable form as above? Thanks!

Re: Decoding messageDigest data?

Posted: Fri May 11, 2018 9:21 pm
by FourthWorld
The hash algo itself produces a binary value. Most systems convert to hex for easier readability and/or for simpler transport when needed. The binaryDecode function will do that:

Code: Select all

function CleanHash pVal
   get binaryDecode("H*", messageDigest(pVal, "SHA-256"), tOut)
   return tOut
end CleanHash

Re: Decoding messageDigest data?

Posted: Fri May 11, 2018 9:28 pm
by ClipArtGuy
Exactly what I needed. Thanks Richard!

Re: Decoding messageDigest data?

Posted: Fri May 11, 2018 10:41 pm
by bwmilby
Follow on question: if storing the hash in a custom property, is there any need to store it decoded? Code works with binary just fine.

Re: Decoding messageDigest data?

Posted: Tue May 15, 2018 8:42 pm
by ClipArtGuy
bwmilby wrote:
Fri May 11, 2018 10:41 pm
Follow on question: if storing the hash in a custom property, is there any need to store it decoded? Code works with binary just fine.
I'm not 100%, but I'll give my two cents and bump the thread for visibility.

I think if it's working for you already, then you're good to go - As long as your stack is uncorrupted, your data is safe in a custom property. In my case, I was just trying to visually verify a hash against one published elsewhere, so I needed it in a human readable form.