Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@stablelib/blake2b

Package Overview
Dependencies
Maintainers
0
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stablelib/blake2b - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

typedoc.json

2

blake2b.ts

@@ -8,3 +8,3 @@ // Copyright (C) 2017 Dmitry Chestnykh

import { SerializableHash } from "@stablelib/hash";
import type { SerializableHash } from "@stablelib/hash";
import { readUint32LE, writeUint32LE } from "@stablelib/binary";

@@ -11,0 +11,0 @@ import { wipe } from "@stablelib/wipe";

@@ -1,15 +0,13 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
var blake2b_1 = require("./blake2b");
var benchmark_1 = require("@stablelib/benchmark");
var buf1M = benchmark_1.byteSeq(1024 << 10);
var buf8K = benchmark_1.byteSeq(8 << 10);
var buf1K = benchmark_1.byteSeq(1 << 10);
var buf32 = benchmark_1.byteSeq(32);
benchmark_1.report("BLAKE2b 1M", benchmark_1.benchmark(function () { return blake2b_1.hash(buf1M); }, buf1M.length));
benchmark_1.report("BLAKE2b 8K", benchmark_1.benchmark(function () { return blake2b_1.hash(buf8K); }, buf8K.length));
benchmark_1.report("BLAKE2b 1K", benchmark_1.benchmark(function () { return blake2b_1.hash(buf1K); }, buf1K.length));
benchmark_1.report("BLAKE2b 32", benchmark_1.benchmark(function () { return blake2b_1.hash(buf32); }, buf32.length));
import { hash } from "./blake2b";
import { benchmark, report, byteSeq } from "@stablelib/benchmark";
let buf1M = byteSeq(1024 << 10);
let buf8K = byteSeq(8 << 10);
let buf1K = byteSeq(1 << 10);
let buf32 = byteSeq(32);
report("BLAKE2b 1M", benchmark(() => hash(buf1M), buf1M.length));
report("BLAKE2b 8K", benchmark(() => hash(buf8K), buf8K.length));
report("BLAKE2b 1K", benchmark(() => hash(buf1K), buf1K.length));
report("BLAKE2b 32", benchmark(() => hash(buf32), buf32.length));
//# sourceMappingURL=blake2b.bench.js.map
/**
* Package blake2b implements BLAKE2b cryptographic hash function.
*/
import { SerializableHash } from "@stablelib/hash";
import type { SerializableHash } from "@stablelib/hash";
export declare const BLOCK_SIZE = 128;

@@ -16,3 +16,3 @@ export declare const DIGEST_LENGTH = 64;

*/
export declare type Config = {
export type Config = {
key?: Uint8Array;

@@ -26,3 +26,3 @@ salt?: Uint8Array;

*/
export declare type Tree = {
export type Tree = {
fanout: number;

@@ -68,3 +68,3 @@ maxDepth: number;

}
export declare type SavedState = {
export type SavedState = {
state: Uint32Array;

@@ -71,0 +71,0 @@ buffer: Uint8Array;

@@ -1,16 +0,14 @@

"use strict";
// Copyright (C) 2017 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
var binary_1 = require("@stablelib/binary");
var wipe_1 = require("@stablelib/wipe");
exports.BLOCK_SIZE = 128;
exports.DIGEST_LENGTH = 64;
exports.KEY_LENGTH = 64;
exports.PERSONALIZATION_LENGTH = 16;
exports.SALT_LENGTH = 16;
exports.MAX_LEAF_SIZE = Math.pow(2, 32) - 1;
exports.MAX_FANOUT = 255;
exports.MAX_MAX_DEPTH = 255; // not a typo
var IV = new Uint32Array([
import { readUint32LE, writeUint32LE } from "@stablelib/binary";
import { wipe } from "@stablelib/wipe";
export const BLOCK_SIZE = 128;
export const DIGEST_LENGTH = 64;
export const KEY_LENGTH = 64;
export const PERSONALIZATION_LENGTH = 16;
export const SALT_LENGTH = 16;
export const MAX_LEAF_SIZE = Math.pow(2, 32) - 1;
export const MAX_FANOUT = 255;
export const MAX_MAX_DEPTH = 255; // not a typo
const IV = new Uint32Array([
// low bits // high bits

@@ -28,3 +26,3 @@ 0xf3bcc908, 0x6a09e667,

// 64-bit ints as two 32-bit ints in arrays.
var SIGMA = [
const SIGMA = [
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30],

@@ -46,19 +44,21 @@ [28, 20, 8, 16, 18, 30, 26, 12, 2, 24, 0, 4, 22, 14, 10, 6],

*/
var BLAKE2b = /** @class */ (function () {
function BLAKE2b(digestLength, config) {
if (digestLength === void 0) { digestLength = 64; }
export class BLAKE2b {
digestLength;
blockSize = BLOCK_SIZE;
// Note: Int32Arrays for state and message are used for performance reasons.
_state = new Int32Array(IV); // hash state, initialized with IV
_buffer = new Uint8Array(BLOCK_SIZE); // buffer for data
_bufferLength = 0; // number of bytes in buffer
_ctr = new Uint32Array(4);
_flag = new Uint32Array(4);
_lastNode = false;
_finished = false;
_vtmp = new Uint32Array(32);
_mtmp = new Uint32Array(32);
_paddedKey; // copy of zero-padded key if present
_initialState; // initial state after initialization
constructor(digestLength = 64, config) {
this.digestLength = digestLength;
this.blockSize = exports.BLOCK_SIZE;
// Note: Int32Arrays for state and message are used for performance reasons.
this._state = new Int32Array(IV); // hash state, initialized with IV
this._buffer = new Uint8Array(exports.BLOCK_SIZE); // buffer for data
this._bufferLength = 0; // number of bytes in buffer
this._ctr = new Uint32Array(4);
this._flag = new Uint32Array(4);
this._lastNode = false;
this._finished = false;
this._vtmp = new Uint32Array(32);
this._mtmp = new Uint32Array(32);
// Validate digest length.
if (digestLength < 1 || digestLength > exports.DIGEST_LENGTH) {
if (digestLength < 1 || digestLength > DIGEST_LENGTH) {
throw new Error("blake2b: wrong digest length");

@@ -71,3 +71,3 @@ }

// Get key length from config.
var keyLength = 0;
let keyLength = 0;
if (config && config.key) {

@@ -77,4 +77,4 @@ keyLength = config.key.length;

// Get tree fanout and maxDepth from config.
var fanout = 1;
var maxDepth = 1;
let fanout = 1;
let maxDepth = 1;
if (config && config.tree) {

@@ -96,13 +96,13 @@ fanout = config.tree.fanout;

if (config && config.salt) {
this._state[8] ^= binary_1.readUint32LE(config.salt, 0);
this._state[9] ^= binary_1.readUint32LE(config.salt, 4);
this._state[10] ^= binary_1.readUint32LE(config.salt, 8);
this._state[11] ^= binary_1.readUint32LE(config.salt, 12);
this._state[8] ^= readUint32LE(config.salt, 0);
this._state[9] ^= readUint32LE(config.salt, 4);
this._state[10] ^= readUint32LE(config.salt, 8);
this._state[11] ^= readUint32LE(config.salt, 12);
}
// Xor personalization into state.
if (config && config.personalization) {
this._state[12] ^= binary_1.readUint32LE(config.personalization, 0);
this._state[13] ^= binary_1.readUint32LE(config.personalization, 4);
this._state[14] ^= binary_1.readUint32LE(config.personalization, 8);
this._state[15] ^= binary_1.readUint32LE(config.personalization, 12);
this._state[12] ^= readUint32LE(config.personalization, 0);
this._state[13] ^= readUint32LE(config.personalization, 4);
this._state[14] ^= readUint32LE(config.personalization, 8);
this._state[15] ^= readUint32LE(config.personalization, 12);
}

@@ -113,10 +113,10 @@ // Save a copy of initialized state for reset.

if (config && config.key && keyLength > 0) {
this._paddedKey = new Uint8Array(exports.BLOCK_SIZE);
this._paddedKey = new Uint8Array(BLOCK_SIZE);
this._paddedKey.set(config.key);
// Put padded key into buffer.
this._buffer.set(this._paddedKey);
this._bufferLength = exports.BLOCK_SIZE;
this._bufferLength = BLOCK_SIZE;
}
}
BLAKE2b.prototype.reset = function () {
reset() {
// Restore initial state.

@@ -127,3 +127,3 @@ this._state.set(this._initialState);

this._buffer.set(this._paddedKey);
this._bufferLength = exports.BLOCK_SIZE;
this._bufferLength = BLOCK_SIZE;
}

@@ -134,41 +134,40 @@ else {

// Clear counters and flags.
wipe_1.wipe(this._ctr);
wipe_1.wipe(this._flag);
wipe(this._ctr);
wipe(this._flag);
this._finished = false;
return this;
};
BLAKE2b.prototype.validateConfig = function (config) {
if (config.key && config.key.length > exports.KEY_LENGTH) {
}
validateConfig(config) {
if (config.key && config.key.length > KEY_LENGTH) {
throw new Error("blake2b: wrong key length");
}
if (config.salt && config.salt.length !== exports.SALT_LENGTH) {
if (config.salt && config.salt.length !== SALT_LENGTH) {
throw new Error("blake2b: wrong salt length");
}
if (config.personalization &&
config.personalization.length !== exports.PERSONALIZATION_LENGTH) {
config.personalization.length !== PERSONALIZATION_LENGTH) {
throw new Error("blake2b: wrong personalization length");
}
if (config.tree) {
if (config.tree.fanout < 0 || config.tree.fanout > exports.MAX_FANOUT) {
if (config.tree.fanout < 0 || config.tree.fanout > MAX_FANOUT) {
throw new Error("blake2b: wrong tree fanout");
}
if (config.tree.maxDepth < 0 || config.tree.maxDepth > exports.MAX_MAX_DEPTH) {
if (config.tree.maxDepth < 0 || config.tree.maxDepth > MAX_MAX_DEPTH) {
throw new Error("blake2b: wrong tree depth");
}
if (config.tree.leafSize < 0 || config.tree.leafSize > exports.MAX_LEAF_SIZE) {
if (config.tree.leafSize < 0 || config.tree.leafSize > MAX_LEAF_SIZE) {
throw new Error("blake2b: wrong leaf size");
}
if (config.tree.innerDigestLength < 0 ||
config.tree.innerDigestLength > exports.DIGEST_LENGTH) {
config.tree.innerDigestLength > DIGEST_LENGTH) {
throw new Error("blake2b: wrong tree inner digest length");
}
}
};
BLAKE2b.prototype.update = function (data, dataLength) {
if (dataLength === void 0) { dataLength = data.length; }
}
update(data, dataLength = data.length) {
if (this._finished) {
throw new Error("blake2b: can't update because hash was finished.");
}
var left = exports.BLOCK_SIZE - this._bufferLength;
var dataPos = 0;
const left = BLOCK_SIZE - this._bufferLength;
let dataPos = 0;
if (dataLength === 0) {

@@ -179,6 +178,6 @@ return this;

if (dataLength > left) {
for (var i = 0; i < left; i++) {
for (let i = 0; i < left; i++) {
this._buffer[this._bufferLength + i] = data[dataPos + i];
}
this._processBlock(exports.BLOCK_SIZE);
this._processBlock(BLOCK_SIZE);
dataPos += left;

@@ -189,13 +188,13 @@ dataLength -= left;

// Process data blocks.
while (dataLength > exports.BLOCK_SIZE) {
for (var i = 0; i < exports.BLOCK_SIZE; i++) {
while (dataLength > BLOCK_SIZE) {
for (let i = 0; i < BLOCK_SIZE; i++) {
this._buffer[i] = data[dataPos + i];
}
this._processBlock(exports.BLOCK_SIZE);
dataPos += exports.BLOCK_SIZE;
dataLength -= exports.BLOCK_SIZE;
this._processBlock(BLOCK_SIZE);
dataPos += BLOCK_SIZE;
dataLength -= BLOCK_SIZE;
this._bufferLength = 0;
}
// Copy leftovers to buffer.
for (var i = 0; i < dataLength; i++) {
for (let i = 0; i < dataLength; i++) {
this._buffer[this._bufferLength + i] = data[dataPos + i];

@@ -205,6 +204,6 @@ }

return this;
};
BLAKE2b.prototype.finish = function (out) {
}
finish(out) {
if (!this._finished) {
for (var i = this._bufferLength; i < exports.BLOCK_SIZE; i++) {
for (let i = this._bufferLength; i < BLOCK_SIZE; i++) {
this._buffer[i] = 0;

@@ -224,30 +223,30 @@ }

// Reuse buffer as temporary space for digest.
var tmp = this._buffer.subarray(0, 64);
for (var i = 0; i < 16; i++) {
binary_1.writeUint32LE(this._state[i], tmp, i * 4);
const tmp = this._buffer.subarray(0, 64);
for (let i = 0; i < 16; i++) {
writeUint32LE(this._state[i], tmp, i * 4);
}
out.set(tmp.subarray(0, out.length));
return this;
};
BLAKE2b.prototype.digest = function () {
var out = new Uint8Array(this.digestLength);
}
digest() {
const out = new Uint8Array(this.digestLength);
this.finish(out);
return out;
};
BLAKE2b.prototype.clean = function () {
wipe_1.wipe(this._vtmp);
wipe_1.wipe(this._mtmp);
wipe_1.wipe(this._state);
wipe_1.wipe(this._buffer);
wipe_1.wipe(this._initialState);
}
clean() {
wipe(this._vtmp);
wipe(this._mtmp);
wipe(this._state);
wipe(this._buffer);
wipe(this._initialState);
if (this._paddedKey) {
wipe_1.wipe(this._paddedKey);
wipe(this._paddedKey);
}
this._bufferLength = 0;
wipe_1.wipe(this._ctr);
wipe_1.wipe(this._flag);
wipe(this._ctr);
wipe(this._flag);
this._lastNode = false;
this._finished = false;
};
BLAKE2b.prototype.saveState = function () {
}
saveState() {
if (this._finished) {

@@ -266,4 +265,4 @@ throw new Error("blake2b: cannot save finished state");

};
};
BLAKE2b.prototype.restoreState = function (savedState) {
}
restoreState(savedState) {
this._state.set(savedState.state);

@@ -276,3 +275,3 @@ this._buffer.set(savedState.buffer);

if (this._paddedKey) {
wipe_1.wipe(this._paddedKey);
wipe(this._paddedKey);
}

@@ -282,19 +281,19 @@ this._paddedKey = savedState.paddedKey ? new Uint8Array(savedState.paddedKey) : undefined;

return this;
};
BLAKE2b.prototype.cleanSavedState = function (savedState) {
wipe_1.wipe(savedState.state);
wipe_1.wipe(savedState.buffer);
wipe_1.wipe(savedState.initialState);
}
cleanSavedState(savedState) {
wipe(savedState.state);
wipe(savedState.buffer);
wipe(savedState.initialState);
if (savedState.paddedKey) {
wipe_1.wipe(savedState.paddedKey);
wipe(savedState.paddedKey);
}
savedState.bufferLength = 0;
wipe_1.wipe(savedState.ctr);
wipe_1.wipe(savedState.flag);
wipe(savedState.ctr);
wipe(savedState.flag);
savedState.lastNode = false;
};
BLAKE2b.prototype._G = function (v, al, bl, cl, dl, ah, bh, ch, dh, ml0, mh0, ml1, mh1) {
var vla = v[al], vha = v[ah], vlb = v[bl], vhb = v[bh], vlc = v[cl], vhc = v[ch], vld = v[dl], vhd = v[dh];
}
_G(v, al, bl, cl, dl, ah, bh, ch, dh, ml0, mh0, ml1, mh1) {
let vla = v[al], vha = v[ah], vlb = v[bl], vhb = v[bh], vlc = v[cl], vhc = v[ch], vld = v[dl], vhd = v[dh];
// 64-bit: va += vb
var w = vla & 0xffff, x = vla >>> 16, y = vha & 0xffff, z = vha >>> 16;
let w = vla & 0xffff, x = vla >>> 16, y = vha & 0xffff, z = vha >>> 16;
w += vlb & 0xffff;

@@ -415,6 +414,6 @@ x += vlb >>> 16;

v[dh] = vhd;
};
BLAKE2b.prototype._incrementCounter = function (n) {
for (var i = 0; i < 3; i++) {
var a = this._ctr[i] + n;
}
_incrementCounter(n) {
for (let i = 0; i < 3; i++) {
let a = this._ctr[i] + n;
this._ctr[i] = a >>> 0;

@@ -426,6 +425,6 @@ if (this._ctr[i] === a) {

}
};
BLAKE2b.prototype._processBlock = function (length) {
}
_processBlock(length) {
this._incrementCounter(length);
var v = this._vtmp;
let v = this._vtmp;
v.set(this._state);

@@ -441,7 +440,7 @@ v.set(IV, 16);

v[15 * 2 + 1] ^= this._flag[3];
var m = this._mtmp;
for (var i = 0; i < 32; i++) {
m[i] = binary_1.readUint32LE(this._buffer, i * 4);
let m = this._mtmp;
for (let i = 0; i < 32; i++) {
m[i] = readUint32LE(this._buffer, i * 4);
}
for (var r = 0; r < 12; r++) {
for (let r = 0; r < 12; r++) {
this._G(v, 0, 8, 16, 24, 1, 9, 17, 25, m[SIGMA[r][0]], m[SIGMA[r][0] + 1], m[SIGMA[r][1]], m[SIGMA[r][1] + 1]);

@@ -456,18 +455,14 @@ this._G(v, 2, 10, 18, 26, 3, 11, 19, 27, m[SIGMA[r][2]], m[SIGMA[r][2] + 1], m[SIGMA[r][3]], m[SIGMA[r][3] + 1]);

}
for (var i = 0; i < 16; i++) {
for (let i = 0; i < 16; i++) {
this._state[i] ^= v[i] ^ v[i + 16];
}
};
return BLAKE2b;
}());
exports.BLAKE2b = BLAKE2b;
function hash(data, digestLength, config) {
if (digestLength === void 0) { digestLength = exports.DIGEST_LENGTH; }
var h = new BLAKE2b(digestLength, config);
}
}
export function hash(data, digestLength = DIGEST_LENGTH, config) {
const h = new BLAKE2b(digestLength, config);
h.update(data);
var digest = h.digest();
const digest = h.digest();
h.clean();
return digest;
}
exports.hash = hash;
//# sourceMappingURL=blake2b.js.map
{
"name": "@stablelib/blake2b",
"version": "1.0.1",
"version": "2.0.0",
"description": "BLAKE2b cryptographic hash function",
"main": "./lib/blake2b.js",
"type": "module",
"typings": "./lib/blake2b.d.ts",

@@ -18,15 +19,15 @@ "author": "Dmitry Chestnykh",

"build": "tsc",
"test": "jasmine JASMINE_CONFIG_PATH=../../configs/jasmine.json",
"test": "vitest run",
"bench": "node ./lib/blake2b.bench.js"
},
"dependencies": {
"@stablelib/binary": "^1.0.1",
"@stablelib/hash": "^1.0.1",
"@stablelib/wipe": "^1.0.1"
"@stablelib/binary": "^2.0.0",
"@stablelib/hash": "^2.0.0",
"@stablelib/wipe": "^2.0.0"
},
"devDependencies": {
"@stablelib/benchmark": "^1.0.1",
"@stablelib/hex": "^1.0.1"
"@stablelib/benchmark": "^2.0.0",
"@stablelib/hex": "^2.0.0"
},
"gitHead": "03dadf27703120d54e6be8436525228ee1c4299b"
"gitHead": "ecfe9109b3c05419fd3ffc16da6c8255b08ad64f"
}
{
"extends": "../../configs/tsconfig.json",
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"removeComments": false,
"preserveConstEnums": true,
"moduleResolution": "node",
"newLine": "LF",
"sourceMap": true,
"declaration": true,
"outDir": "lib",
"lib": [
"es5",
"es2015.promise",
"dom",
"scripthost"
]
},

@@ -23,0 +6,0 @@ "exclude": [

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

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