%SYSTEM.Encryption MD5Hash function -return value conversion
Hi,
We wanted to use the MD5Hash function,
And had trouble understanding it's return value , as documented:
"Return value: 16-byte MD5 hash."
The value we wanted should have been a simple string, and we did not know how to convert the output to string.
Searching for a solution we found this article:
https://groups.google.com/forum/#!msg/intersystems-public-cache/FdkHIgS9PkQ/P7s-92kN70UJ
That had this solution that works:
"If you know what this gibberish stuff is about, it can be very simple ;)
USER> s h = ##class(%SYSTEM.Encryption).
USER> s h0x = ""
USER> f i=1:1:$l(h) {s chr = $zhex($ascii($e(h,i))) s h0x = h0x _
$s($l(chr) = 1: "0"_chr, 1:chr)}
USER> w $zcvt(h0x,"l")
1d2fd35d57d574b807a807b507528b
And also a simpler solution:
" s pwd="12345678"
s md5=$system.Encryption.
s md5hex=##class(%xsd.hexBinary)
w md5hex,! "
Is this issue addressed in Cache documentation? and is there a more standard way to get the string result?
Regards,
Nael
The value we wanted should have been a simple string, and we did not
know how to convert the output to string
##class(%SYSTEM.Encryption).MD5Hash() does return a string, what do you mean by 'simple'? If you mean 'displayable' that will depend on what you're trying to display on.
Take a look at ##class(%SYSTEM.Encryption).Base64Encode() that will return a string with only non-control ASCII characters.
Hi,
What I mean by a simple string, is a value that you can send Via a web service for example.
Thanks.
What value are you expecting?
Base64 is often used to encode hashes:
I understand.
Thanks, all comments have been helpful!
Regards,
Nael
That looks better than the WRC supplied code when I fist noticed the binary rather than hex values returned.
FWIW, you can also replace the call to $extract with two-argument $ascii:
Whereas the digest methods in the %SYSTEM .Encryption class return binary strings, which are documented in terms of their byte length, it is indeed conventional to display them using hexadecimal. Instead of $select, I more usually see $translate and $justify:
If %xsd.hexBinary is covenient, though, I'd say use that.
For a sanity check, you can compare whatever implementation you choose with the zzdump command:
To encrypt to MD5 (as javascript, unix and others) I writed this classmethod, using information found here, and works very well.
Just need to use " d Encripta("word")"
I hope could be useful!
Joe!
Social networks
InterSystems resources
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue