Module: system/crypto-helpers


(async, static) createCryptoHash(s) → {Promise.<SHA256Hash>}

Name Type Description
s string

The input string

Helper function to have one place where the crypto hash of a UTF-8 string is calculated. The implementation depends on the platform. This function is asynchronous because the hash function of the crypto API implemented in browsers is asynchronous.


Returns a promise that resolves with the SHA-256 hash over the input string

Type: Promise.<SHA256Hash>

(static) createRandomString(lengthopt, hexopt) → {Promise.<string>}

Name Type Attributes Default Description
length number <optional>

Desired length of the random string. The maximum allowed is 65,536 (platform limit in browsers, enforced on all our platforms for portability).

hex boolean <optional>

If true the random string will only contain characters 0123456789abcdef (hexadecimal, lowercase). If false the string will contain characters from the set 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-

Helper function that creates a secure random string characters.

It uses crypto-functions of the respective platform to create a secure random string.

node.js: crypto.randomBytes() low.js: crypto.randomBytes() (low.js implementation) moddable: NOT SECURE: There is no native method so we use Math.random() browser: crypto.getRandomValues() rn: package LinusU/react-native-get-random-values, i.e. - iOS: secrandomcopybytes() - Android: class SecureRandom

Different platforms set different limits for our implementation, which, for portability, we enforce on all platforms:

  • Maximum string length of 65,536 characters
  • Returns a promise because on node.js the (preferred way to call the native) method is asynchronous

A random string

Type: Promise.<string>

(static) createRandomSHA256Hash() → {Promise.<SHA256Hash>}

This function is a frontend for createRandomString to correctly type-annotate the special case when a random hex string of exactly 64 characters is created: The result will then be a SHA-256 hash.


Type: Promise.<SHA256Hash>