Comparing version 1.0.0 to 1.1.0
var blake2b = require('./index.js') | ||
var output = new Uint8Array(64) | ||
var input = Buffer.from('hello world') | ||
var input = Buffer.allocUnsafe(2048) | ||
@@ -6,0 +6,0 @@ blake2b(output, input) |
38
index.js
@@ -230,3 +230,3 @@ var assert = require('assert') | ||
// Returns a Uint8Array containing the message digest | ||
function blake2bFinal (out, ctx) { | ||
function blake2bFinal (ctx, out) { | ||
ctx.t += ctx.c // mark last block offset | ||
@@ -260,5 +260,39 @@ | ||
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 | ||
@@ -265,0 +299,0 @@ var BYTES_MAX = module.exports.BYTES_MAX = 64 |
{ | ||
"name": "blake2b", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Blake2b (64-bit version) in pure Javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -51,2 +51,19 @@ # `blake2b` | ||
### `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 | ||
@@ -53,0 +70,0 @@ |
58
test.js
@@ -37,2 +37,60 @@ var test = require('tape') | ||
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) { | ||
@@ -39,0 +97,0 @@ // must be an even number of digits |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
304049
1739
98
0