LC and Javascript hash?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Zax
Posts: 473
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

LC and Javascript hash?

Post by Zax » Fri Jan 19, 2024 3:02 pm

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.

Zax
Posts: 473
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: LC and Javascript hash?

Post by Zax » Sat Jan 20, 2024 10:04 am

OK, after several searches, I found a Javascript code which gives results consistent with those of LC:
http://caligatio.github.com/jsSHA/

:)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: LC and Javascript hash?

Post by FourthWorld » Sat Jan 20, 2024 5:12 pm

The GitHub link is 404.

What did the difference turn out to be?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: LC and Javascript hash?

Post by SparkOut » Sat Jan 20, 2024 5:44 pm


stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: LC and Javascript hash?

Post by stam » Sat Jan 20, 2024 6:46 pm

https://github.com/Caligatio/jsSHA/ works for me... the .io link is a static example page

Zax
Posts: 473
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: LC and Javascript hash?

Post by Zax » Sun Jan 21, 2024 10:19 am

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.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”