Converting MD5 digest

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
Posts: 362
Joined: Wed Aug 15, 2007 11:09 pm

Converting MD5 digest

Post by capellan » Thu Oct 12, 2017 4:20 pm

Hi All,

We could use the Md5digest or any other digest type (like SHA1 or SHA512) as a function that returns it's characters as base64encoded or as hexadecimal numbers. Check this example script:

on mouseup
put "abc" into myContainer
put "MD5 encoded as Base64:" && MD5Var(myContainer,B) & cr into tVariable
put "MD5 as Hexadecimal numbers:" && MD5Var(myContainer) after tVariable
put tVariable
end mouseup

Function MD5Var tContainer tEncoding
-- This function returns an MD5 digest from a variable or container.
-- Because MD5 digest is binary data, we need to convert it
-- into Base64 or Hexadecimal numbers. For this conversion
-- we use the parameter named tEncoding with the value
-- B (for base64 encoding) or simply left tEncoding
-- empty and this function returns it's result as
-- Hexadecimal numbers

if tEncoding = B then
-- returns a string of 24 characters like
-- T+YY8ZhKxjGmYt0XMwDb9g==
return base64encode(md5digest(tContainer))
else -- by default, this function
-- returns a string of 32 characters like
put 0 into myVar
get binarydecode("H*",md5digest(tContainer),myVar)
return toupper(myVar)
end if
end MD5Var

Have a nice week!


Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”