From aa0305154fbac4a4536471acc9bbd34430e2ccd9 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Thu, 18 Jun 2020 14:09:12 +0200 Subject: [PATCH] move crypto_hash_sha256 to module to uncouple wasm dependencies --- crypto_hash.js | 15 +++------------ crypto_hash_256.js | 18 ++++++++++++++++++ index.js | 1 + 3 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 crypto_hash_256.js diff --git a/crypto_hash.js b/crypto_hash.js index 53c2c2b..77d29d2 100644 --- a/crypto_hash.js +++ b/crypto_hash.js @@ -1,18 +1,11 @@ -const sha256 = require('sha256-wasm') const sha512 = require('sha512-wasm') const assert = require('nanoassert') -var crypto_hash_sha256_BYTES = 32 +if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.') + var crypto_hash_sha512_BYTES = 64 var crypto_hash_BYTES = crypto_hash_sha512_BYTES -function crypto_hash_sha256 (out, m, n) { - assert(out.byteLength === crypto_hash_sha256_BYTES, "out must be 'crypto_hash_sha256_BYTES' bytes long") - - sha256().update(m.subarray(0, n)).digest(out) - return 0 -} - function crypto_hash_sha512 (out, m, n) { assert(out.byteLength === crypto_hash_sha512_BYTES, "out must be 'crypto_hash_sha512_BYTES' bytes long") @@ -26,8 +19,6 @@ function crypto_hash (out, m, n) { module.exports = { crypto_hash, - crypto_hash_sha256, crypto_hash_sha512, - crypto_hash_BYTES, - crypto_hash_sha256_BYTES + crypto_hash_BYTES } diff --git a/crypto_hash_256.js b/crypto_hash_256.js new file mode 100644 index 0000000..cbe0543 --- /dev/null +++ b/crypto_hash_256.js @@ -0,0 +1,18 @@ +const sha256 = require('sha256-wasm') +const assert = require('nanoassert') + +if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.') + +var crypto_hash_sha256_BYTES = 32 + +function crypto_hash_sha256 (out, m, n) { + assert(out.byteLength === crypto_hash_sha256_BYTES, "out must be 'crypto_hash_sha256_BYTES' bytes long") + + sha256().update(m.subarray(0, n)).digest(out) + return 0 +} + +module.exports = { + crypto_hash_sha256, + crypto_hash_sha256_BYTES +} diff --git a/index.js b/index.js index 11292c6..1a9f228 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,7 @@ sodium.sodium_malloc = function (n) { forward(require('./crypto_box')) forward(require('./crypto_generichash')) forward(require('./crypto_hash')) +forward(require('./crypto_hash_sha256')) forward(require('./crypto_kdf')) forward(require('./crypto_kx')) forward(require('./crypto_aead'))