Browse Source

Revert "use sync wasm loading"

This reverts commit a5860244d0.
master
Mathias Buus 7 years ago
parent
commit
a4b32178ee
  1. 21
      README.md
  2. 15
      example.js
  3. 29
      index.js
  4. 94
      test.js

21
README.md

@ -17,13 +17,17 @@ if (!blake2b.SUPPORTED) {
console.log('WebAssembly not supported by your runtime')
}
var hash = blake2b()
.update(Buffer.from('hello')) // pass in a buffer or uint8array
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
blake2b.ready(function (err) {
if (err) throw err
console.log('Blake2b hash of "hello world" is %s', hash)
var hash = blake2b()
.update(Buffer.from('hello')) // pass in a buffer or uint8array
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
console.log('Blake2b hash of "hello world" is %s', hash)
})
```
## API
@ -40,6 +44,11 @@ Update the hash with a new piece of data. `data` should be a buffer or uint8arra
Digest the hash.
#### `var promise = blake2b.ready([cb])`
Wait for the WASM code to load. Returns the WebAssembly instance promise as well for convenience.
You have to call this at least once before instantiating the hash.
## Browser demo
There is a browser example included in [example.html](example.html) and [example.js](example.js).

15
example.js

@ -1,9 +1,12 @@
var blake2b = require('./')
var hash = blake2b()
.update(new Buffer('hello'))
.update(new Buffer(' '))
.update(new Buffer('world'))
.digest('hex')
blake2b.ready(function () {
var hash = blake2b()
.update(new Buffer('hello'))
.update(new Buffer(' '))
.update(new Buffer('world'))
.digest('hex')
console.log('Blake2b hash of "hello world" is %s', hash)
})
console.log('Blake2b hash of "hello world" is %s', hash)

29
index.js

@ -2,15 +2,14 @@ var fs = require('fs')
var assert = require('nanoassert')
var toUint8Array = require('base64-to-uint8array')
var buf = toUint8Array(fs.readFileSync(__dirname + '/blake2b.wasm', 'base64'))
var rdy
var head = 64
var wasm = typeof WebAssembly !== 'undefined' && new WebAssembly.Instance(new WebAssembly.Module(buf))
var mod = wasm && wasm.exports
var memory = wasm && new Uint8Array(wasm.exports.memory.buffer)
var mod = null
var memory = null
var freeList = []
module.exports = Blake2b
var BYTES_MIN = module.exports.BYTES_MIN = 16
var BYTES_MAX = module.exports.BYTES_MAX = 64
var BYTES = module.exports.BYTES = 32
@ -22,7 +21,7 @@ var PERSONALBYTES = module.exports.PERSONALBYTES = 16
function Blake2b (digestLength, key, salt, personal, noAssert) {
if (!(this instanceof Blake2b)) return new Blake2b(digestLength, key, salt, personal, noAssert)
if (!mod) throw new Error('WebAssembly not supported')
if (!mod) throw new Error('WASM not loaded. Wait for Blake2b.ready(cb)')
if (!digestLength) digestLength = 32
if (noAssert !== true) {
@ -61,6 +60,8 @@ function Blake2b (digestLength, key, salt, personal, noAssert) {
}
}
Blake2b.prototype.ready = Blake2b.ready
Blake2b.prototype.update = function (input) {
assert(this.finalized === false, 'Hash instance finalized')
assert(input, 'input must be TypedArray or Buffer')
@ -97,7 +98,18 @@ Blake2b.prototype.digest = function (enc) {
Blake2b.prototype.final = Blake2b.prototype.digest
Blake2b.WASM = buf
Blake2b.SUPPORTED = !!mod
Blake2b.SUPPORTED = typeof WebAssembly !== 'undefined'
Blake2b.ready = function (cb) {
if (!cb) cb = noop
if (!Blake2b.SUPPORTED) return cb(new Error('WebAssembly not supported'))
if (!rdy) {
rdy = WebAssembly.instantiate(buf).then(setup)
}
return rdy.then(cb).catch(cb)
}
function noop () {}
@ -111,3 +123,8 @@ function toHex (n) {
if (n < 16) return '0' + n.toString(16)
return n.toString(16)
}
function setup (w) {
mod = w.instance.exports
memory = new Uint8Array(w.instance.exports.memory.buffer)
}

94
test.js

@ -2,61 +2,63 @@ var tape = require('tape')
var blake2b = require('./')
var vectors = require('blake2b/test-vectors.json')
tape('hello world', function (t) {
var hash = blake2b()
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
t.same(hash, '256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610')
t.end()
})
tape('hello world', function (t) {
var hash = blake2b(64)
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
t.same(hash, '021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbcc05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0')
t.end()
})
blake2b.ready(function () {
tape('hello world', function (t) {
var hash = blake2b()
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
tape('both at the same time', function (t) {
var a = blake2b()
var b = blake2b(64)
t.same(hash, '256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610')
t.end()
})
var hash = a
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
tape('hello world', function (t) {
var hash = blake2b(64)
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
t.same(hash, '256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610')
t.same(hash, '021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbcc05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0')
t.end()
})
var hash = b
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
tape('both at the same time', function (t) {
var a = blake2b()
var b = blake2b(64)
t.same(hash, '021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbcc05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0')
t.end()
})
var hash = a
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
vectors.forEach(function (vector, i) {
tape('test-vectors.json #' + i, function (t) {
var key = vector.key && Buffer.from(vector.key, 'hex')
var salt = vector.salt && Buffer.from(vector.salt, 'hex')
var personal = vector.personal && Buffer.from(vector.personal, 'hex')
t.same(hash, '256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610')
var hash = blake2b(vector.outlen, key, salt, personal, true)
.update(Buffer.from(vector.input, 'hex'))
var hash = b
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
t.same(hash, vector.out)
t.same(hash, '021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbcc05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0')
t.end()
})
vectors.forEach(function (vector, i) {
tape('test-vectors.json #' + i, function (t) {
var key = vector.key && Buffer.from(vector.key, 'hex')
var salt = vector.salt && Buffer.from(vector.salt, 'hex')
var personal = vector.personal && Buffer.from(vector.personal, 'hex')
var hash = blake2b(vector.outlen, key, salt, personal, true)
.update(Buffer.from(vector.input, 'hex'))
.digest('hex')
t.same(hash, vector.out)
t.end()
})
})
})

Loading…
Cancel
Save