move crypto_hash_sha256 to module to uncouple wasm dependencies

This commit is contained in:
Christophe Diederichs 2020-06-18 14:09:12 +02:00
parent cb1fe07efe
commit aa0305154f
3 changed files with 22 additions and 12 deletions

View File

@ -1,18 +1,11 @@
const sha256 = require('sha256-wasm')
const sha512 = require('sha512-wasm') const sha512 = require('sha512-wasm')
const assert = require('nanoassert') 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_sha512_BYTES = 64
var crypto_hash_BYTES = crypto_hash_sha512_BYTES 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) { function crypto_hash_sha512 (out, m, n) {
assert(out.byteLength === crypto_hash_sha512_BYTES, "out must be 'crypto_hash_sha512_BYTES' bytes long") 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 = { module.exports = {
crypto_hash, crypto_hash,
crypto_hash_sha256,
crypto_hash_sha512, crypto_hash_sha512,
crypto_hash_BYTES, crypto_hash_BYTES
crypto_hash_sha256_BYTES
} }

18
crypto_hash_256.js Normal file
View File

@ -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
}

View File

@ -23,6 +23,7 @@ sodium.sodium_malloc = function (n) {
forward(require('./crypto_box')) forward(require('./crypto_box'))
forward(require('./crypto_generichash')) forward(require('./crypto_generichash'))
forward(require('./crypto_hash')) forward(require('./crypto_hash'))
forward(require('./crypto_hash_sha256'))
forward(require('./crypto_kdf')) forward(require('./crypto_kdf'))
forward(require('./crypto_kx')) forward(require('./crypto_kx'))
forward(require('./crypto_aead')) forward(require('./crypto_aead'))