Question William Vorhees · Jan 9, 2017

SHA-512 seems to be wrong

All,

Totally new to Cache and Encryption.  Have a project requirement for SHA-512.  

I am making the call as this:  s hash=$System.Encryption.SHAHash(512,test)

The return is: Ïƒá5~︽ñT(PÖm€Ö ä W܃ô©!ÓléÎGÐÑ<]…ò°ÿƒÒ‡~ì/c¹1½GAz¥82zù'Ú>

However, online SHA-512 hash generators  are saying that the result should be:

ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff

If someone could point me in the right direction that would be excellent.

Thanks in advance.

Comments

Dmitry Maslennikov · Jan 9, 2017

The difference only in output format. InterSystems returns just string of bytes, while online generator returns hex codes for that bytes, which is much better for reading by human. So,  you may quickly check it with command zzdump. And you may notice, that zzdump shows the same hash you got from online generator.

USER>set hash=$system.Encryption.SHAHash(512,"test")

USER>zzdump hash

0000: EE 26 B0 DD 4A F7 E7 49 AA 1A 8E E3 C1 0A E9 92         î&°ÝJ÷çIª..ãÁ.é.
0010: 3F 61 89 80 77 2E 47 3F 88 19 A5 D4 94 0E 0D B2         ?a..w.G?..¥Ô...²
0020: 7A C1 85 F8 A0 E1 D5 F8 4F 88 BC 88 7F D6 7B 14         zÁ.ø áÕøO.¼..Ö{.
0030: 37 32 C3 04 CC 5F A9 AD 8E 6F 57 F5 00 28 A8 FF         72Ã.Ì_©­.oWõ.(¨ÿ

And this simple line of code, may help you to convert this hash to hex hash.

USER>set hashHex="" for i=1:1:$l(hash) set hashHex=hashHex_$zhex($a(hash,i))

USER>write hashHex
EE26B0DD4AF7E749AA1A8EE3C1AE9923F618980772E473F8819A5D494EDB27AC185F8A0E1D5F84F88BC887FD67B143732C34CC5FA9AD8E6F57F5028A8FF

USER>write $zconvert(hashHex,"L")
ee26b0dd4af7e749aa1a8ee3c1ae9923f618980772e473f8819a5d494edb27ac185f8a0e1d5f84f88bc887fd67b143732c34cc5fa9ad8e6f57f5028a8ff
0
Jon Willeke  Jan 9, 2017 to Dmitry Maslennikov

Careful, you should pad each byte with a leading zero if necessary. Otherwise your output is missing five nibbles.

0
Dmitry Maslennikov  Jan 10, 2017 to Jon Willeke

Sure, leading zero, so, fixed code.

set hashHex="" for i=1:1:$l(hash) set hashHex=hashHex_$translate($justify($zhex($a(hash,i)),2)," ","0")
0
Vitaliy Serdtsev · Jan 10, 2017

Important: pre-need string to convert to UTF-8

USER>w ##class(%xsd.hexBinary).LogicalToXSD($system.Encryption.SHAHash(512,$zcvt("тестtest","O","UTF8")))
83B481B1A58FA416F34BFF575F2DCA866C6AB790CF0941BC0F1E7E91D916D22866CB9575C666021B2F8A796B20A484ABB29403853EDEA6173B9A61758060117E
USER>w ##class(%xsd.hexBinary).LogicalToXSD($system.Encryption.SHAHash(512,$zcvt("test","O","UTF8")))
EE26B0DD4AF7E749AA1A8EE3C10AE9923F618980772E473F8819A5D4940E0DB27AC185F8A0E1D5F84F88BC887FD67B143732C304CC5FA9AD8E6F57F50028A8FF
0