Decoding messageDigest data?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Decoding messageDigest data?

Post by ClipArtGuy » Fri May 11, 2018 9:12 pm

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!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Decoding messageDigest data?

Post by FourthWorld » Fri May 11, 2018 9:21 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Decoding messageDigest data?

Post by ClipArtGuy » Fri May 11, 2018 9:28 pm

Exactly what I needed. Thanks Richard!

bwmilby
Posts: 438
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: Decoding messageDigest data?

Post by bwmilby » 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.
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Decoding messageDigest data?

Post by ClipArtGuy » Tue May 15, 2018 8:42 pm

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.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”