SUI-Components / sui

Monorepo for SUI (Simple User Interface) packages.
169 stars 33 forks source link

feat(packages/sui-js): using a pure js function to make this utility hashing function isomorphic #1743

Closed jordevo closed 5 months ago

jordevo commented 6 months ago

Description

These js utilities should work reliably on both server and client. Using the WebCrypto API makes it difficult to make it work with both NodeJS versions under v19 and the browser runtime.

Original PR approach:

The hashing function of choice was coming from: https://geraintluff.github.io/sha256/ It's small-sized but it only supports ASCII. It fits our needs for now but further research should be done to find a better and still small-sized solution.

New approach:

The hashing function code comes from: https://www.movable-type.co.uk/scripts/sha256.html#src-code It is not optimised but it's Unicode compatible. This script is oriented toward hashing text messages rather than binary data. The standard considers hashing byte-stream (or bit-stream) messages only. Text which contains (multi-byte) characters outside ISO 8859-1 (i.e. accented characters outside Latin-1 or non-European character sets – anything with Unicode code-point above U+FF), can’t be encoded 4-per-word, so the script defaults to encoding the text as UTF-8 before hashing it.

A beta release 2.30.0-beta.native-hash has been published to test this behaviour on preproduction environments and it's working as desired.

image

Related Issue

Since we started using it, we've been seeing an increase in error logs coming from the server. This happens when this hashing function, as it currently is, is called from a NodeJS environment.

image

Example

N/A