Page 1 of 1

LC and Javascript hash?

Posted: Fri Jan 19, 2024 3:02 pm
by Zax
Hello,

I don't understand much about string hashing but I would like to compare a string hashed in SHA3-256 using the messageDigest() function with a hashed string in Javascript.

On the Javascript side, I found this:
https://coursesweb.net/javascript/jssha-hash-hmac

My problem is that the same string hashed by LC and by Javascript gives a different result.

My LC code:

Code: Select all

put hashSha3Test(fld "OriginalString", true)
put hashSha3Test(fld "OriginalString", false)

function hashSha3Test str, returnHex // SHA3-256
   // if returnHex set to true, returns hex. if false, returns base64
   put messageDigest(textEncode(str, "UTF-8"), "SHA3-256") into hashedStr
   if (returnHex) then
      get binaryDecode("H*", hashedStr, tHex)
      return tHex
   else return base64Encode(hashedStr)
end hashSha3Test
My Javascript code:

Code: Select all

var originalString = 'Test';
var hash;

var sha_ob = new jsSHA('SHA3-256', 'TEXT');
sha_ob.update(originalString);

hash = sha_ob.getHash('HEX');
console.log('HEX=' + hash);
hash = sha_ob.getHash('B64');
console.log('B64=' + hash);
Maybe it would be better if I use another Javascript function?
Thank you.

Re: LC and Javascript hash?

Posted: Sat Jan 20, 2024 10:04 am
by Zax
OK, after several searches, I found a Javascript code which gives results consistent with those of LC:
http://caligatio.github.com/jsSHA/

:)

Re: LC and Javascript hash?

Posted: Sat Jan 20, 2024 5:12 pm
by FourthWorld
The GitHub link is 404.

What did the difference turn out to be?

Re: LC and Javascript hash?

Posted: Sat Jan 20, 2024 5:44 pm
by SparkOut

Re: LC and Javascript hash?

Posted: Sat Jan 20, 2024 6:46 pm
by stam
https://github.com/Caligatio/jsSHA/ works for me... the .io link is a static example page

Re: LC and Javascript hash?

Posted: Sun Jan 21, 2024 10:19 am
by Zax
I don't know why the JS functions in the 1st link (coursesweb.net) didn't work. Maybe I misunderstood their uses.
In any case, the LC hash functions work perfectly, though the trick to return HEX data could be added to the built-in Dictionary documentation.