Browse Source

Add stream support

master
Emil Bay 7 years ago
parent
commit
f32c4fcda9
No known key found for this signature in database GPG Key ID: AF1CF37B90FBF638
  1. 17
      README.md
  2. 2
      example.js
  3. 38
      index.js
  4. 58
      test.js

17
README.md

@ -48,6 +48,23 @@ API. All parameters must also fulfill the following constraints, or an
use this parameter as a kind of app id, or global versioning scheme. This
value is not required to be secret.
### `var instance = blake2b.instance(outlen, [key], [salt], [personal], [noAssert = false])`
Like the above method, but allows your to update the hash as you can access more
data. `noAssert` will also disable asserts in `.update` and `.final` methods.
Note that `outlen` should be a number, and that you pass the `Buffer` in the
`.final` method
### `instance.update(input)`
Update the hash with new `input`. Calling this method after `.final` will throw
an error.
### `instance.final(out)`
Finalise the the hash and write the digest to `out`. `out` must be exactly equal
to `outlen` given in the `.instance` method.
### Constants
* `blake2b.BYTES_MIN` Minimum length of `out`

2
example.js

@ -1,7 +1,7 @@
var blake2b = require('./index.js')
var output = new Uint8Array(64)
var input = Buffer.from('hello world')
var input = Buffer.allocUnsafe(2048)
blake2b(output, input)

38
index.js

@ -227,7 +227,7 @@ function blake2bUpdate (ctx, input) {
// Completes a BLAKE2b streaming hash
// Returns a Uint8Array containing the message digest
function blake2bFinal (out, ctx) {
function blake2bFinal (ctx, out) {
ctx.t += ctx.c // mark last block offset
while (ctx.c < 128) { // fill up with zeros
@ -256,7 +256,41 @@ module.exports = function blake2b (out, input, key, salt, personal, noAssert) {
// do the math
var ctx = blake2bInit(out.length, key, salt, personal)
blake2bUpdate(ctx, input)
return blake2bFinal(out, ctx)
return blake2bFinal(ctx, out)
}
module.exports.instance = function instance (outlen, key, salt, personal, noAssert) {
if (noAssert !== true) {
assert(outlen >= BYTES_MIN)
assert(outlen <= BYTES_MAX)
assert(key == null ? true : key.length >= KEYBYTES_MIN)
assert(key == null ? true : key.length <= KEYBYTES_MAX)
assert(salt == null ? true : salt.length === SALTBYTES)
assert(personal == null ? true : personal.length === PERSONALBYTES)
}
var ctx = blake2bInit(outlen, key, salt, personal)
var finalised = false
return {
update: function (input) {
if (finalised === true) throw new Error('hash has been finalised')
if (noAssert !== true) {
assert(input != null)
}
blake2bUpdate(ctx, input)
},
final: function (out) {
if (noAssert !== true) {
assert(out != null)
assert(out.length === outlen)
}
finalised = true
return blake2bFinal(ctx, out)
}
}
}
var BYTES_MIN = module.exports.BYTES_MIN = 16

58
test.js

@ -34,6 +34,64 @@ test('works with buffers', function (assert) {
assert.end()
})
test('streaming', function (t) {
var isntance = blake2b.instance(blake2b.BYTES)
var buf = new Buffer('Hej, Verden')
for (var i = 0; i < 10; i++) isntance.update(buf)
var out = Buffer.alloc(blake2b.BYTES)
isntance.final(out)
t.same(out.toString('hex'), 'cbc20f347f5dfe37dc13231cbf7eaa4ec48e585ec055a96839b213f62bd8ce00', 'streaming hash')
t.end()
})
test('streaming with key', function (t) {
var key = Buffer.alloc(blake2b.KEYBYTES)
key.fill('lo')
var instance = blake2b.instance(blake2b.BYTES, key)
var buf = new Buffer('Hej, Verden')
for (var i = 0; i < 10; i++) instance.update(buf)
var out = Buffer.alloc(blake2b.BYTES)
instance.final(out)
t.same(out.toString('hex'), '405f14acbeeb30396b8030f78e6a84bab0acf08cb1376aa200a500f669f675dc', 'streaming keyed hash')
t.end()
})
test('streaming with hash length', function (t) {
var isntance = blake2b.instance(blake2b.BYTES_MIN)
var buf = new Buffer('Hej, Verden')
for (var i = 0; i < 10; i++) isntance.update(buf)
var out = Buffer.alloc(blake2b.BYTES_MIN)
isntance.final(out)
t.same(out.toString('hex'), 'decacdcc3c61948c79d9f8dee5b6aa99', 'streaming short hash')
t.end()
})
test('streaming with key and hash length', function (t) {
var key = Buffer.alloc(blake2b.KEYBYTES)
key.fill('lo')
var instance = blake2b.instance(blake2b.BYTES_MIN, key)
var buf = new Buffer('Hej, Verden')
for (var i = 0; i < 10; i++) instance.update(buf)
var out = Buffer.alloc(blake2b.BYTES_MIN)
instance.final(out)
t.same(out.toString('hex'), 'fb43f0ab6872cbfd39ec4f8a1bc6fb37', 'streaming short keyed hash')
t.end()
})
function hexWrite (buf, string) {
// must be an even number of digits
var strLen = string.length

Loading…
Cancel
Save