Page 1 of 1

Converting MD5 digest

Posted: Thu Oct 12, 2017 4:20 pm
by capellan
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
-- 5EB63BBBE01EEED093CB22BB8F5ACDC3
put 0 into myVar
get binarydecode("H*",md5digest(tContainer),myVar)
return toupper(myVar)
end if
end MD5Var

Have a nice week!

Al