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'))