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

@stablelib/sha256

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

Comparing version 1.0.1 to 2.0.0

typedoc.json

22

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

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

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

@@ -69,0 +69,0 @@ buffer: Uint8Array | undefined;

@@ -1,28 +0,26 @@

"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 = 32;
exports.BLOCK_SIZE = 64;
import { readUint32BE, writeUint32BE } from "@stablelib/binary";
import { wipe } from "@stablelib/wipe";
export const DIGEST_LENGTH = 32;
export const BLOCK_SIZE = 64;
/**
* SHA2-256 cryptographic hash algorithm.
*/
var SHA256 = /** @class */ (function () {
function SHA256() {
/** 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._state = new Int32Array(8); // hash state
this._temp = new Int32Array(64); // temporary state
this._buffer = new Uint8Array(128); // 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 SHA256 {
/** Length of hash output */
digestLength = DIGEST_LENGTH;
/** Block size */
blockSize = BLOCK_SIZE;
// Note: Int32Array is used instead of Uint32Array for performance reasons.
_state = new Int32Array(8); // hash state
_temp = new Int32Array(64); // temporary state
_buffer = new Uint8Array(128); // 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();
}
SHA256.prototype._initState = function () {
_initState() {
this._state[0] = 0x6a09e667;

@@ -36,3 +34,3 @@ this._state[1] = 0xbb67ae85;

this._state[7] = 0x5be0cd19;
};
}
/**

@@ -42,3 +40,3 @@ * Resets hash state making it possible

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

@@ -49,11 +47,11 @@ this._bufferLength = 0;

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

@@ -65,8 +63,7 @@ * Updates hash state with the given data.

*/
SHA256.prototype.update = function (data, dataLength) {
if (dataLength === void 0) { dataLength = data.length; }
update(data, dataLength = data.length) {
if (this._finished) {
throw new Error("SHA256: can't update because hash was finished.");
}
var dataPos = 0;
let dataPos = 0;
this._bytesHashed += dataLength;

@@ -92,3 +89,3 @@ if (this._bufferLength > 0) {

return this;
};
}
/**

@@ -98,31 +95,31 @@ * Finalizes hash state and puts hash into out.

*/
SHA256.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 % 64 < 56) ? 64 : 128;
const bytesHashed = this._bytesHashed;
const left = this._bufferLength;
const bitLenHi = (bytesHashed / 0x20000000) | 0;
const bitLenLo = bytesHashed << 3;
const padLength = (bytesHashed % 64 < 56) ? 64 : 128;
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._temp, this._state, this._buffer, 0, padLength);
this._finished = true;
}
for (var i = 0; i < this.digestLength / 4; i++) {
binary_1.writeUint32BE(this._state[i], out, i * 4);
for (let i = 0; i < this.digestLength / 4; i++) {
writeUint32BE(this._state[i], out, i * 4);
}
return this;
};
}
/**
* Returns the final hash digest.
*/
SHA256.prototype.digest = function () {
var out = new Uint8Array(this.digestLength);
digest() {
const out = new Uint8Array(this.digestLength);
this.finish(out);
return out;
};
}
/**

@@ -134,3 +131,3 @@ * Function useful for HMAC/PBKDF2 optimization.

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

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

};
};
}
/**

@@ -152,3 +149,3 @@ * Function useful for HMAC/PBKDF2 optimization.

*/
SHA256.prototype.restoreState = function (savedState) {
restoreState(savedState) {
this._state.set(savedState.state);

@@ -162,19 +159,17 @@ this._bufferLength = savedState.bufferLength;

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

@@ -196,26 +191,26 @@ 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01,

while (len >= 64) {
var a = v[0];
var b = v[1];
var c = v[2];
var d = v[3];
var e = v[4];
var f = v[5];
var g = v[6];
var h = v[7];
for (var i = 0; i < 16; i++) {
var j = pos + i * 4;
w[i] = binary_1.readUint32BE(p, j);
let a = v[0];
let b = v[1];
let c = v[2];
let d = v[3];
let e = v[4];
let f = v[5];
let g = v[6];
let h = v[7];
for (let i = 0; i < 16; i++) {
let j = pos + i * 4;
w[i] = readUint32BE(p, j);
}
for (var i = 16; i < 64; i++) {
var u = w[i - 2];
var t1 = (u >>> 17 | u << (32 - 17)) ^ (u >>> 19 | u << (32 - 19)) ^ (u >>> 10);
for (let i = 16; i < 64; i++) {
let u = w[i - 2];
let t1 = (u >>> 17 | u << (32 - 17)) ^ (u >>> 19 | u << (32 - 19)) ^ (u >>> 10);
u = w[i - 15];
var t2 = (u >>> 7 | u << (32 - 7)) ^ (u >>> 18 | u << (32 - 18)) ^ (u >>> 3);
let t2 = (u >>> 7 | u << (32 - 7)) ^ (u >>> 18 | u << (32 - 18)) ^ (u >>> 3);
w[i] = (t1 + w[i - 7] | 0) + (t2 + w[i - 16] | 0);
}
for (var i = 0; i < 64; i++) {
var t1 = (((((e >>> 6 | e << (32 - 6)) ^ (e >>> 11 | e << (32 - 11)) ^
for (let i = 0; i < 64; i++) {
let t1 = (((((e >>> 6 | e << (32 - 6)) ^ (e >>> 11 | e << (32 - 11)) ^
(e >>> 25 | e << (32 - 25))) + ((e & f) ^ (~e & g))) | 0) +
((h + ((K[i] + w[i]) | 0)) | 0)) | 0;
var t2 = (((a >>> 2 | a << (32 - 2)) ^ (a >>> 13 | a << (32 - 13)) ^
let t2 = (((a >>> 2 | a << (32 - 2)) ^ (a >>> 13 | a << (32 - 13)) ^
(a >>> 22 | a << (32 - 22))) + ((a & b) ^ (a & c) ^ (b & c))) | 0;

@@ -244,10 +239,9 @@ h = g;

}
function hash(data) {
var h = new SHA256();
export function hash(data) {
const h = new SHA256();
h.update(data);
var digest = h.digest();
const digest = h.digest();
h.clean();
return digest;
}
exports.hash = hash;
//# sourceMappingURL=sha256.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 sha256_1 = require("./sha256");
var base64_1 = require("@stablelib/base64");
var vectors = [
import { describe, expect, it } from 'vitest';
import { SHA256, hash } from "./sha256";
import { encode } from "@stablelib/base64";
const vectors = [
"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",

@@ -266,68 +265,68 @@ "bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=",

// 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("sha256.SHA256", function () {
it("should produce correct hashes for test vectors", function () {
for (var i = 0; i < input.length; i++) {
var h = new sha256_1.SHA256();
describe("sha256.SHA256", () => {
it("should produce correct hashes for test vectors", () => {
for (let i = 0; i < input.length; i++) {
let h = new SHA256();
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 sha256_1.SHA256();
it("should correctly update multiple times", () => {
const h1 = new SHA256();
h1.update(input.subarray(0, 1));
h1.update(input.subarray(1, 120));
h1.update(input.subarray(120, 256));
var h2 = new sha256_1.SHA256();
const h2 = new SHA256();
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 sha256_1.SHA256();
it("should return the same digest after finalizing", () => {
let h = new SHA256();
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 sha256_1.SHA256();
it("should throw when updating finalized instance", () => {
let h = new SHA256();
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 sha256_1.SHA256();
it("should reset instance", () => {
let h = new SHA256();
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 32-byte digest", function () {
var h = new sha256_1.SHA256();
it("should return 32-byte digest", () => {
let h = new SHA256();
h.update(input);
expect(h.digest().length).toBe(32);
});
it("should correctly hash 3 GiB", function () {
var h = new sha256_1.SHA256();
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 SHA256();
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("BvFiHnDTVyn76NY32YGVu51ZEVq92dl55yjfoHQSQzA=");
expect(encode(h.digest())).toBe("BvFiHnDTVyn76NY32YGVu51ZEVq92dl55yjfoHQSQzA=");
});
});
describe("sha256.hash", function () {
it("should produce correct hashes for test vectors", function () {
for (var i = 0; i < input.length; i++) {
var digest = sha256_1.hash(input.subarray(0, i));
expect(base64_1.encode(digest)).toBe(vectors[i]);
describe("sha256.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/sha256",
"version": "1.0.1",
"version": "2.0.0",
"description": "SHA-256 cryptographic hash function",
"main": "./lib/sha256.js",
"type": "module",
"typings": "./lib/sha256.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/sha256.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 { SHA256, hash } from "./sha256";

@@ -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