New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@stablelib/sha512

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/sha512 - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

typedoc.json

22

lib/sha512.bench.js

@@ -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 sha512_1 = require("./sha512");
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("SHA512 1M", benchmark_1.benchmark(function () { return sha512_1.hash(buf1M); }, buf1M.length));
benchmark_1.report("SHA512 8K", benchmark_1.benchmark(function () { return sha512_1.hash(buf8K); }, buf8K.length));
benchmark_1.report("SHA512 1K", benchmark_1.benchmark(function () { return sha512_1.hash(buf1K); }, buf1K.length));
benchmark_1.report("SHA512 32", benchmark_1.benchmark(function () { return sha512_1.hash(buf32); }, buf32.length));
import { hash } from "./sha512";
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("SHA512 1M", benchmark(() => hash(buf1M), buf1M.length));
report("SHA512 8K", benchmark(() => hash(buf8K), buf8K.length));
report("SHA512 1K", benchmark(() => hash(buf1K), buf1K.length));
report("SHA512 32", benchmark(() => hash(buf32), buf32.length));
//# sourceMappingURL=sha512.bench.js.map
/**
* Package sha512 implements SHA-2-512 cryptographic hash function.
*/
import { SerializableHash } from "@stablelib/hash";
import type { SerializableHash } from "@stablelib/hash";
export declare const DIGEST_LENGTH = 64;

@@ -66,3 +66,3 @@ export declare const BLOCK_SIZE = 128;

}
export declare type SavedState = {
export type SavedState = {
stateHi: Int32Array;

@@ -69,0 +69,0 @@ stateLo: Int32Array;

@@ -1,30 +0,28 @@

"use strict";
// Copyright (C) 2016 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.DIGEST_LENGTH = 64;
exports.BLOCK_SIZE = 128;
import { readUint32BE, writeUint32BE } from "@stablelib/binary";
import { wipe } from "@stablelib/wipe";
export const DIGEST_LENGTH = 64;
export const BLOCK_SIZE = 128;
/**
* SHA-2-512 cryptographic hash algorithm.
*/
var SHA512 = /** @class */ (function () {
function SHA512() {
/** Length of hash output */
this.digestLength = exports.DIGEST_LENGTH;
/** Block size */
this.blockSize = exports.BLOCK_SIZE;
// Note: Int32Array is used instead of Uint32Array for performance reasons.
this._stateHi = new Int32Array(8); // hash state, high bytes
this._stateLo = new Int32Array(8); // hash state, low bytes
this._tempHi = new Int32Array(16); // temporary state, high bytes
this._tempLo = new Int32Array(16); // temporary state, low bytes
this._buffer = new Uint8Array(256); // buffer for data to hash
this._bufferLength = 0; // number of bytes in buffer
this._bytesHashed = 0; // number of total bytes hashed
this._finished = false; // indicates whether the hash was finalized
export class SHA512 {
/** Length of hash output */
digestLength = DIGEST_LENGTH;
/** Block size */
blockSize = BLOCK_SIZE;
// Note: Int32Array is used instead of Uint32Array for performance reasons.
_stateHi = new Int32Array(8); // hash state, high bytes
_stateLo = new Int32Array(8); // hash state, low bytes
_tempHi = new Int32Array(16); // temporary state, high bytes
_tempLo = new Int32Array(16); // temporary state, low bytes
_buffer = new Uint8Array(256); // buffer for data to hash
_bufferLength = 0; // number of bytes in buffer
_bytesHashed = 0; // number of total bytes hashed
_finished = false; // indicates whether the hash was finalized
constructor() {
this.reset();
}
SHA512.prototype._initState = function () {
_initState() {
this._stateHi[0] = 0x6a09e667;

@@ -46,3 +44,3 @@ this._stateHi[1] = 0xbb67ae85;

this._stateLo[7] = 0x137e2179;
};
}
/**

@@ -52,3 +50,3 @@ * Resets hash state making it possible

*/
SHA512.prototype.reset = function () {
reset() {
this._initState();

@@ -59,12 +57,12 @@ this._bufferLength = 0;

return this;
};
}
/**
* Cleans internal buffers and resets hash state.
*/
SHA512.prototype.clean = function () {
wipe_1.wipe(this._buffer);
wipe_1.wipe(this._tempHi);
wipe_1.wipe(this._tempLo);
clean() {
wipe(this._buffer);
wipe(this._tempHi);
wipe(this._tempLo);
this.reset();
};
}
/**

@@ -76,11 +74,10 @@ * Updates hash state with the given data.

*/
SHA512.prototype.update = function (data, dataLength) {
if (dataLength === void 0) { dataLength = data.length; }
update(data, dataLength = data.length) {
if (this._finished) {
throw new Error("SHA512: can't update because hash was finished.");
}
var dataPos = 0;
let dataPos = 0;
this._bytesHashed += dataLength;
if (this._bufferLength > 0) {
while (this._bufferLength < exports.BLOCK_SIZE && dataLength > 0) {
while (this._bufferLength < BLOCK_SIZE && dataLength > 0) {
this._buffer[this._bufferLength++] = data[dataPos++];

@@ -103,3 +100,3 @@ dataLength--;

return this;
};
}
/**

@@ -109,32 +106,32 @@ * Finalizes hash state and puts hash into out.

*/
SHA512.prototype.finish = function (out) {
finish(out) {
if (!this._finished) {
var bytesHashed = this._bytesHashed;
var left = this._bufferLength;
var bitLenHi = (bytesHashed / 0x20000000) | 0;
var bitLenLo = bytesHashed << 3;
var padLength = (bytesHashed % 128 < 112) ? 128 : 256;
const bytesHashed = this._bytesHashed;
const left = this._bufferLength;
const bitLenHi = (bytesHashed / 0x20000000) | 0;
const bitLenLo = bytesHashed << 3;
const padLength = (bytesHashed % 128 < 112) ? 128 : 256;
this._buffer[left] = 0x80;
for (var i = left + 1; i < padLength - 8; i++) {
for (let i = left + 1; i < padLength - 8; i++) {
this._buffer[i] = 0;
}
binary_1.writeUint32BE(bitLenHi, this._buffer, padLength - 8);
binary_1.writeUint32BE(bitLenLo, this._buffer, padLength - 4);
writeUint32BE(bitLenHi, this._buffer, padLength - 8);
writeUint32BE(bitLenLo, this._buffer, padLength - 4);
hashBlocks(this._tempHi, this._tempLo, this._stateHi, this._stateLo, this._buffer, 0, padLength);
this._finished = true;
}
for (var i = 0; i < this.digestLength / 8; i++) {
binary_1.writeUint32BE(this._stateHi[i], out, i * 8);
binary_1.writeUint32BE(this._stateLo[i], out, i * 8 + 4);
for (let i = 0; i < this.digestLength / 8; i++) {
writeUint32BE(this._stateHi[i], out, i * 8);
writeUint32BE(this._stateLo[i], out, i * 8 + 4);
}
return this;
};
}
/**
* Returns the final hash digest.
*/
SHA512.prototype.digest = function () {
var out = new Uint8Array(this.digestLength);
digest() {
const out = new Uint8Array(this.digestLength);
this.finish(out);
return out;
};
}
/**

@@ -145,3 +142,3 @@ * Function useful for HMAC/PBKDF2 optimization. Returns hash state to be

*/
SHA512.prototype.saveState = function () {
saveState() {
if (this._finished) {

@@ -157,3 +154,3 @@ throw new Error("SHA256: cannot save finished state");

};
};
}
/**

@@ -163,3 +160,3 @@ * Function useful for HMAC/PBKDF2 optimization. Restores state saved by

*/
SHA512.prototype.restoreState = function (savedState) {
restoreState(savedState) {
this._stateHi.set(savedState.stateHi);

@@ -174,20 +171,18 @@ this._stateLo.set(savedState.stateLo);

return this;
};
}
/**
* Cleans state returned by saveState().
*/
SHA512.prototype.cleanSavedState = function (savedState) {
wipe_1.wipe(savedState.stateHi);
wipe_1.wipe(savedState.stateLo);
cleanSavedState(savedState) {
wipe(savedState.stateHi);
wipe(savedState.stateLo);
if (savedState.buffer) {
wipe_1.wipe(savedState.buffer);
wipe(savedState.buffer);
}
savedState.bufferLength = 0;
savedState.bytesHashed = 0;
};
return SHA512;
}());
exports.SHA512 = SHA512;
}
}
// Constants
var K = new Int32Array([
const K = new Int32Array([
0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,

@@ -235,29 +230,29 @@ 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,

function hashBlocks(wh, wl, hh, hl, m, pos, len) {
var ah0 = hh[0], ah1 = hh[1], ah2 = hh[2], ah3 = hh[3], ah4 = hh[4], ah5 = hh[5], ah6 = hh[6], ah7 = hh[7], al0 = hl[0], al1 = hl[1], al2 = hl[2], al3 = hl[3], al4 = hl[4], al5 = hl[5], al6 = hl[6], al7 = hl[7];
var h, l;
var th, tl;
var a, b, c, d;
let ah0 = hh[0], ah1 = hh[1], ah2 = hh[2], ah3 = hh[3], ah4 = hh[4], ah5 = hh[5], ah6 = hh[6], ah7 = hh[7], al0 = hl[0], al1 = hl[1], al2 = hl[2], al3 = hl[3], al4 = hl[4], al5 = hl[5], al6 = hl[6], al7 = hl[7];
let h, l;
let th, tl;
let a, b, c, d;
while (len >= 128) {
for (var i = 0; i < 16; i++) {
var j = 8 * i + pos;
wh[i] = binary_1.readUint32BE(m, j);
wl[i] = binary_1.readUint32BE(m, j + 4);
for (let i = 0; i < 16; i++) {
const j = 8 * i + pos;
wh[i] = readUint32BE(m, j);
wl[i] = readUint32BE(m, j + 4);
}
for (var i = 0; i < 80; i++) {
var bh0 = ah0;
var bh1 = ah1;
var bh2 = ah2;
var bh3 = ah3;
var bh4 = ah4;
var bh5 = ah5;
var bh6 = ah6;
var bh7 = ah7;
var bl0 = al0;
var bl1 = al1;
var bl2 = al2;
var bl3 = al3;
var bl4 = al4;
var bl5 = al5;
var bl6 = al6;
var bl7 = al7;
for (let i = 0; i < 80; i++) {
let bh0 = ah0;
let bh1 = ah1;
let bh2 = ah2;
let bh3 = ah3;
let bh4 = ah4;
let bh5 = ah5;
let bh6 = ah6;
let bh7 = ah7;
let bl0 = al0;
let bl1 = al1;
let bl2 = al2;
let bl3 = al3;
let bl4 = al4;
let bl5 = al5;
let bl6 = al6;
let bl7 = al7;
// add

@@ -368,3 +363,3 @@ h = ah7;

if (i % 16 === 15) {
for (var j = 0; j < 16; j++) {
for (let j = 0; j < 16; j++) {
// add

@@ -555,10 +550,9 @@ h = wh[j];

}
function hash(data) {
var h = new SHA512();
export function hash(data) {
const h = new SHA512();
h.update(data);
var digest = h.digest();
const digest = h.digest();
h.clean();
return digest;
}
exports.hash = hash;
//# sourceMappingURL=sha512.js.map

@@ -1,8 +0,7 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
var sha512_1 = require("./sha512");
var base64_1 = require("@stablelib/base64");
var vectors = [
import { describe, expect, it } from 'vitest';
import { SHA512, hash } from "./sha512";
import { encode } from "@stablelib/base64";
const vectors = [
"z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==",

@@ -266,68 +265,68 @@ "uCRNAomB1pOve0Vq+O+kytY9KC4Z/xSULCRuUNk1HSJwSoAqccNYC2Nw3kzrKTwySoQjNCVX1OXDhDjw42kQ7g==",

// Test input is [ 0, 1, 2, ..., 255 ].
var input = new Uint8Array(256);
for (var i = 0; i < input.length; i++) {
const input = new Uint8Array(256);
for (let i = 0; i < input.length; i++) {
input[i] = i & 0xff;
}
describe("sha512.SHA512", function () {
it("should produce correct hashes for test vectors", function () {
for (var i = 0; i < input.length; i++) {
var h = new sha512_1.SHA512();
describe("sha512.SHA512", () => {
it("should produce correct hashes for test vectors", () => {
for (let i = 0; i < input.length; i++) {
let h = new SHA512();
h.update(input.subarray(0, i));
expect(base64_1.encode(h.digest())).toBe(vectors[i]);
expect(encode(h.digest())).toBe(vectors[i]);
}
});
it("should correctly update multiple times", function () {
var h1 = new sha512_1.SHA512();
it("should correctly update multiple times", () => {
const h1 = new SHA512();
h1.update(input.subarray(0, 1));
h1.update(input.subarray(1, 120));
h1.update(input.subarray(120, 256));
var h2 = new sha512_1.SHA512();
const h2 = new SHA512();
h2.update(input.subarray(0, 256));
expect(base64_1.encode(h1.digest())).toBe(base64_1.encode(h2.digest()));
expect(encode(h1.digest())).toBe(encode(h2.digest()));
});
it("should return the same digest after finalizing", function () {
var h = new sha512_1.SHA512();
it("should return the same digest after finalizing", () => {
let h = new SHA512();
h.update(input);
var d1 = h.digest();
var d2 = h.digest();
expect(base64_1.encode(d1)).toBe(base64_1.encode(d2));
let d1 = h.digest();
let d2 = h.digest();
expect(encode(d1)).toBe(encode(d2));
});
it("should throw when updating finalized instance", function () {
var h = new sha512_1.SHA512();
it("should throw when updating finalized instance", () => {
let h = new SHA512();
h.update(input);
h.digest();
expect(function () { return h.update(input); }).toThrow();
expect(() => h.update(input)).toThrow();
});
it("should reset instance", function () {
var h = new sha512_1.SHA512();
it("should reset instance", () => {
let h = new SHA512();
h.update(input);
var d1 = h.digest();
let d1 = h.digest();
h.reset();
h.update(input);
var d2 = h.digest();
expect(base64_1.encode(d1)).toBe(base64_1.encode(d2));
let d2 = h.digest();
expect(encode(d1)).toBe(encode(d2));
});
it("should return 64-byte digest", function () {
var h = new sha512_1.SHA512();
it("should return 64-byte digest", () => {
let h = new SHA512();
h.update(input);
expect(h.digest().length).toBe(64);
});
it("should correctly hash 3 GiB", function () {
var h = new sha512_1.SHA512();
var buf = new Uint8Array(256 * 1024 * 1024); // 256 MiB
for (var i = 0; i < buf.length; i++) {
it("should correctly hash 3 GiB", () => {
const h = new SHA512();
const buf = new Uint8Array(256 * 1024 * 1024); // 256 MiB
for (let i = 0; i < buf.length; i++) {
buf[i] = i & 0xff;
}
for (var i = 0; i < 12; i++) { // 3 GiB
for (let i = 0; i < 12; i++) { // 3 GiB
buf[0] = i & 0xff;
h.update(buf);
}
expect(base64_1.encode(h.digest())).toBe("UejeHARrYmiULwwX7A4tSUEkMi8jMl+5xq2AYOMlmK2gs9hYtpRvnD7LLNfxnLl8c8gfSFSeJ2NVjrladi/q7g==");
expect(encode(h.digest())).toBe("UejeHARrYmiULwwX7A4tSUEkMi8jMl+5xq2AYOMlmK2gs9hYtpRvnD7LLNfxnLl8c8gfSFSeJ2NVjrladi/q7g==");
});
});
describe("sha512.hash", function () {
it("should produce correct hashes for test vectors", function () {
for (var i = 0; i < input.length; i++) {
var digest = sha512_1.hash(input.subarray(0, i));
expect(base64_1.encode(digest)).toBe(vectors[i]);
describe("sha512.hash", () => {
it("should produce correct hashes for test vectors", () => {
for (let i = 0; i < input.length; i++) {
const digest = hash(input.subarray(0, i));
expect(encode(digest)).toBe(vectors[i]);
}

@@ -334,0 +333,0 @@ });

{
"name": "@stablelib/sha512",
"version": "1.0.1",
"version": "2.0.0",
"description": "SHA-512 cryptographic hash function",
"main": "./lib/sha512.js",
"type": "module",
"typings": "./lib/sha512.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/sha512.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/base64": "^1.0.1",
"@stablelib/benchmark": "^1.0.1"
"@stablelib/base64": "^2.0.0",
"@stablelib/benchmark": "^2.0.0"
},
"gitHead": "03dadf27703120d54e6be8436525228ee1c4299b"
"gitHead": "ecfe9109b3c05419fd3ffc16da6c8255b08ad64f"
}
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
import { describe, expect, it } from 'vitest';
import { SHA512, hash } from "./sha512";

@@ -5,0 +6,0 @@ import { encode } from "@stablelib/base64";

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

import { SerializableHash } from "@stablelib/hash";
import type { SerializableHash } from "@stablelib/hash";
import { readUint32BE, writeUint32BE } from "@stablelib/binary";

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

{
"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 not supported yet

Sorry, the diff of this file is not supported yet

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