Socket
Socket
Sign inDemoInstall

blake2b

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

blake2b - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

11

example.js
var blake2b = require('./index.js')
var output = new Uint8Array(64)
var input = Buffer.allocUnsafe(2048)
var output = new Uint8Array(32)
var input = Buffer.alloc(2048)
blake2b(output, input)
console.log('hash:', blake2b(output.length).update(input).digest('hex'))
console.log(output)
blake2b.ready(function () {
console.log('has wasm?', blake2b.WASM_LOADED)
console.log('hash again:', blake2b(output.length).update(input).digest('hex'))
})

@@ -1,2 +0,3 @@

var assert = require('assert')
var assert = require('nanoassert')
var b2wasm = require('blake2b-wasm')

@@ -180,14 +181,13 @@ // 64-bit unsigned addition

// Takes an optional Uint8Array key
function blake2bInit (outlen, key, salt, personal) {
function Blake2b (outlen, key, salt, personal) {
// zero out parameter_block before usage
parameter_block.fill(0)
// state, 'param block'
var ctx = {
b: new Uint8Array(128),
h: new Uint32Array(16),
t: 0, // input count
c: 0, // pointer within buffer
outlen: outlen // output length in bytes
}
this.b = new Uint8Array(128)
this.h = new Uint32Array(16)
this.t = 0 // input count
this.c = 0 // pointer within buffer
this.outlen = outlen // output length in bytes
parameter_block[0] = outlen

@@ -203,3 +203,3 @@ if (key) parameter_block[1] = key.length

for (var i = 0; i < 16; i++) {
ctx.h[i] = BLAKE2B_IV32[i] ^ B2B_GET32(parameter_block, i * 4)
this.h[i] = BLAKE2B_IV32[i] ^ B2B_GET32(parameter_block, i * 4)
}

@@ -209,10 +209,26 @@

if (key) {
blake2bUpdate(ctx, key)
blake2bUpdate(this, key)
// at the end
ctx.c = 128
this.c = 128
}
}
return ctx
Blake2b.prototype.update = function (input) {
blake2bUpdate(this, input)
return this
}
Blake2b.prototype.digest = function (out) {
var buf = (!out || out === 'binary' || out === 'hex') ? new Uint8Array(this.outlen) : out
blake2bFinal(this, buf)
if (out === 'hex') return hexSlice(buf)
return buf
}
Blake2b.ready = function (cb) {
b2wasm.ready(function () {
cb() // ignore the error
})
}
// Updates a BLAKE2b streaming hash

@@ -247,55 +263,37 @@ // Requires hash context and Uint8Array (byte array)

module.exports = function blake2b (out, input, key, salt, personal, noAssert) {
if (noAssert !== true) {
assert(out != null)
assert(out.length >= BYTES_MIN)
assert(out.length <= BYTES_MAX)
assert(input != null)
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)
}
function hexSlice (buf) {
var str = ''
for (var i = 0; i < buf.length; i++) str += toHex(buf[i])
return str
}
// do the math
var ctx = blake2bInit(out.length, key, salt, personal)
blake2bUpdate(ctx, input)
return blake2bFinal(ctx, out)
function toHex (n) {
if (n < 16) return '0' + n.toString(16)
return n.toString(16)
}
module.exports.instance = function instance (outlen, key, salt, personal, noAssert) {
var Proto = Blake2b
module.exports = function createHash (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)
assert(outlen >= BYTES_MIN, 'outlen must be at least ' + BYTES_MIN + ', was given ' + outlen)
assert(outlen <= BYTES_MAX, 'outlen must be at most ' + BYTES_MAX + ', was given ' + outlen)
if (key != null) assert(key.length >= KEYBYTES_MIN, 'key must be at least ' + KEYBYTES_MIN + ', was given ' + key.length)
if (key != null) assert(key.length <= KEYBYTES_MAX, 'key must be at least ' + KEYBYTES_MAX + ', was given ' + key.length)
if (salt != null) assert(salt.length === SALTBYTES, 'salt must be exactly ' + SALTBYTES + ', was given ' + salt.length)
if (personal != null) assert(personal.length === PERSONALBYTES, 'personal must be exactly ' + PERSONALBYTES + ', was given ' + personal.length)
}
var ctx = blake2bInit(outlen, key, salt, personal)
var finalised = false
return new Proto(outlen, key, salt, personal)
}
return {
update: function (input) {
if (finalised === true) throw new Error('hash has been finalised')
if (noAssert !== true) {
assert(input != null)
}
module.exports.ready = function (cb) {
b2wasm.ready(function () { // ignore errors
cb()
})
}
blake2bUpdate(ctx, input)
return this
},
final: function (out) {
if (noAssert !== true) {
assert(out != null)
assert(out.length === outlen)
}
module.exports.WASM_SUPPORTED = b2wasm.SUPPORTED
module.exports.WASM_LOADED = false
finalised = true
return blake2bFinal(ctx, out)
}
}
}
var BYTES_MIN = module.exports.BYTES_MIN = 16

@@ -309,1 +307,8 @@ var BYTES_MAX = module.exports.BYTES_MAX = 64

var PERSONALBYTES = module.exports.PERSONALBYTES = 16
b2wasm.ready(function (err) {
if (!err) {
module.exports.WASM_LOADED = true
Proto = b2wasm
}
})
{
"name": "blake2b",
"version": "1.2.0",
"version": "2.0.0",
"description": "Blake2b (64-bit version) in pure Javascript",
"main": "index.js",
"dependencies": {},
"dependencies": {
"blake2b-wasm": "^1.0.0",
"nanoassert": "^1.0.0"
},
"devDependencies": {
"browserify": "^14.4.0",
"coverify": "^1.4.1",
"tape": "^4.6.3"
},
"scripts": {
"test": "tape test"
"test": "tape test.js",
"cov": "browserify -t coverify test.js | node | coverify"
},

@@ -13,0 +19,0 @@ "repository": {

@@ -13,2 +13,3 @@ # `blake2b`

* This module exports constants for the parameters in libsodium style
* Uses a WASM version (where it is supported) for massive performance boosts

@@ -31,5 +32,5 @@ All credit goes to @dcposch for doing the hard work of porting the

### `var out = blake2b(out, input, [key], [salt], [personal], [noAssert = false])`
### `var hash = blake2b(outLength, [key], [salt], [personal], [noAssert = false])`
Hash `input` and write result to `out`, optionally with `key`, `salt` and
Create a new hash instance, optionally with `key`, `salt` and
`personal`. Bypass input assertions by setting `noAssert` to `true`.

@@ -41,4 +42,3 @@

* `out` must within the byte ranges defined by the constants below.
* `input` can be any length, including `0`
* `outLength` must within the byte ranges defined by the constants below.
* `key` is optional, but must within the byte ranges defined by the constants

@@ -54,19 +54,15 @@ below, if given. This value must be kept secret, and can be used to create

### `var instance = blake2b.instance(outlen, [key], [salt], [personal], [noAssert = false])`
### `var hash = hash.update(input)`
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
### `var instance = instance.update(input)`
Update the hash with new `input`. Calling this method after `.final` will throw
Update the hash with new `input`. Calling this method after `.digest` will throw
an error.
### `var out = instance.final(out)`
### `var out = hash.digest(out)`
Finalise the the hash and write the digest to `out`. `out` must be exactly equal
to `outlen` given in the `.instance` method.
to `outLength` given in the `blake2b` method.
Optionally you can pass `hex` to get the hash as a hex string or no arguments
to have the hash return a new Uint8Array with the hash.
### Constants

@@ -73,0 +69,0 @@

@@ -5,91 +5,101 @@ var test = require('tape')

test('vectors', function (assert) {
vectors.forEach(function (v) {
var out = new Uint8Array(v.outlen)
var input = hexWrite(new Uint8Array(v.input.length / 2), v.input)
var key = v.key.length === 0 ? null : hexWrite(new Uint8Array(v.key.length / 2), v.key)
var salt = v.salt.length === 0 ? null : hexWrite(new Uint8Array(v.salt.length / 2), v.salt)
var personal = v.personal.length === 0 ? null : hexWrite(new Uint8Array(v.personal.length / 2), v.personal)
setup()
test('wait for ready', function (assert) {
blake2b.ready(function () {
assert.end()
})
})
setup()
var expected = Buffer.from(hexWrite(new Uint8Array(v.out.length / 2), v.out))
var actual = Buffer.from(blake2b(out, input, key, salt, personal, true))
function setup () {
test('vectors', function (assert) {
vectors.forEach(function (v) {
var out = new Uint8Array(v.outlen)
var input = hexWrite(new Uint8Array(v.input.length / 2), v.input)
var key = v.key.length === 0 ? null : hexWrite(new Uint8Array(v.key.length / 2), v.key)
var salt = v.salt.length === 0 ? null : hexWrite(new Uint8Array(v.salt.length / 2), v.salt)
var personal = v.personal.length === 0 ? null : hexWrite(new Uint8Array(v.personal.length / 2), v.personal)
assert.deepEquals(actual, expected)
var expected = Buffer.from(hexWrite(new Uint8Array(v.out.length / 2), v.out))
var actual = Buffer.from(blake2b(out.length, key, salt, personal, true).update(input).digest(out))
assert.deepEquals(actual, expected)
})
assert.end()
})
assert.end()
})
test('works with buffers', function (assert) {
var vector = vectors.slice(-1)[0]
test('works with buffers', function (assert) {
var vector = vectors.slice(-1)[0]
var out = Buffer.allocUnsafe(vector.outlen)
var input = Buffer.from(vector.input, 'hex')
var key = Buffer.from(vector.key, 'hex')
var salt = Buffer.from(vector.salt, 'hex')
var personal = Buffer.from(vector.personal, 'hex')
var out = Buffer.allocUnsafe(vector.outlen)
var input = Buffer.from(vector.input, 'hex')
var key = Buffer.from(vector.key, 'hex')
var salt = Buffer.from(vector.salt, 'hex')
var personal = Buffer.from(vector.personal, 'hex')
var expected = Buffer.from(vector.out, 'hex')
var actual = blake2b(out, input, key, salt, personal)
var expected = Buffer.from(vector.out, 'hex')
var actual = blake2b(out.length, key, salt, personal).update(input).digest(out)
assert.deepEquals(actual, expected)
assert.end()
})
assert.deepEquals(actual, expected)
assert.end()
})
test('streaming', function (t) {
var isntance = blake2b.instance(blake2b.BYTES)
var buf = new Buffer('Hej, Verden')
test('streaming', function (t) {
var isntance = blake2b(blake2b.BYTES)
var buf = new Buffer('Hej, Verden')
for (var i = 0; i < 10; i++) isntance.update(buf)
for (var i = 0; i < 10; i++) isntance.update(buf)
var out = Buffer.alloc(blake2b.BYTES)
isntance.final(out)
var out = Buffer.alloc(blake2b.BYTES)
isntance.digest(out)
t.same(out.toString('hex'), 'cbc20f347f5dfe37dc13231cbf7eaa4ec48e585ec055a96839b213f62bd8ce00', 'streaming hash')
t.end()
})
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')
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')
var instance = blake2b(blake2b.BYTES, key)
var buf = new Buffer('Hej, Verden')
for (var i = 0; i < 10; i++) instance.update(buf)
for (var i = 0; i < 10; i++) instance.update(buf)
var out = Buffer.alloc(blake2b.BYTES)
instance.final(out)
var out = Buffer.alloc(blake2b.BYTES)
instance.digest(out)
t.same(out.toString('hex'), '405f14acbeeb30396b8030f78e6a84bab0acf08cb1376aa200a500f669f675dc', 'streaming keyed hash')
t.end()
})
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')
test('streaming with hash length', function (t) {
var isntance = blake2b(blake2b.BYTES_MIN)
var buf = new Buffer('Hej, Verden')
for (var i = 0; i < 10; i++) isntance.update(buf)
for (var i = 0; i < 10; i++) isntance.update(buf)
var out = Buffer.alloc(blake2b.BYTES_MIN)
isntance.final(out)
var out = Buffer.alloc(blake2b.BYTES_MIN)
isntance.digest(out)
t.same(out.toString('hex'), 'decacdcc3c61948c79d9f8dee5b6aa99', 'streaming short hash')
t.end()
})
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')
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')
var instance = blake2b(blake2b.BYTES_MIN, key)
var buf = new Buffer('Hej, Verden')
for (var i = 0; i < 10; i++) instance.update(buf)
for (var i = 0; i < 10; i++) instance.update(buf)
var out = Buffer.alloc(blake2b.BYTES_MIN)
instance.final(out)
var out = Buffer.alloc(blake2b.BYTES_MIN)
instance.digest(out)
t.same(out.toString('hex'), 'fb43f0ab6872cbfd39ec4f8a1bc6fb37', 'streaming short keyed hash')
t.end()
})
t.same(out.toString('hex'), 'fb43f0ab6872cbfd39ec4f8a1bc6fb37', 'streaming short keyed hash')
t.end()
})
}

@@ -96,0 +106,0 @@ function hexWrite (buf, string) {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc