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

crypto-hash

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

crypto-hash - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

thread.js

8

index.d.ts

@@ -12,4 +12,2 @@ export interface OptionsHexOutput {

_Note that even though it returns a promise, [in Node.js, the operation is synchronous 💩](https://github.com/nodejs/node/issues/678)._
@returns The hex-encoded hash.

@@ -27,4 +25,2 @@ */

/**
_Note that even though it returns a promise, [in Node.js, the operation is synchronous 💩](https://github.com/nodejs/node/issues/678)._
@returns The hex-encoded hash.

@@ -52,4 +48,2 @@

/**
_Note that even though it returns a promise, [in Node.js, the operation is synchronous 💩](https://github.com/nodejs/node/issues/678)._
@returns The hex-encoded hash.

@@ -67,4 +61,2 @@ */

/**
_Note that even though it returns a promise, [in Node.js, the operation is synchronous 💩](https://github.com/nodejs/node/issues/678)._
@returns The hex-encoded hash.

@@ -71,0 +63,0 @@ */

71

index.js
'use strict';
const crypto = require('crypto');
const create = algorithm => async (buffer, options) => {
const requireOptional = (name, defaultValue) => {
try {
return require(name);
} catch (_) {
return defaultValue;
}
};
const {Worker} = requireOptional('worker_threads', {});
let worker; // Lazy
let taskIdCounter = 0;
const tasks = new Map();
const createWorker = () => {
worker = new Worker('./thread.js');
worker.on('message', message => {
const task = tasks.get(message.id);
tasks.delete(message.id);
if (tasks.size === 0) {
worker.unref();
}
task(message.value);
});
worker.on('error', error => {
// Any error here is effectively an equivalent of segfault and have no scope, so we just throw it on callback level
throw error;
});
};
const taskWorker = (value, transferList) => new Promise(resolve => {
const id = taskIdCounter++;
tasks.set(id, resolve);
if (worker === undefined) {
createWorker();
}
worker.postMessage({id, value}, transferList);
});
let create = algorithm => async (buffer, options) => {
options = {

@@ -20,2 +63,28 @@ outputFormat: 'hex',

if (Worker !== undefined) {
create = algorithm => async (source, options) => {
options = {
outputFormat: 'hex',
...options
};
let buffer;
if (typeof source === 'string') {
// Saving one copy operation by writing string to buffer right away and then transfering buffer
buffer = new ArrayBuffer(Buffer.byteLength(source, 'utf8'));
Buffer.from(buffer).write(source, 'utf8');
} else {
// Creating a copy of buffer at call time, will be transfered later
buffer = source.buffer.slice(0);
}
const result = await taskWorker({algorithm, buffer}, [buffer]);
if (options.outputFormat === 'hex') {
return Buffer.from(result).toString('hex');
}
return result;
};
}
exports.sha1 = create('sha1');

@@ -22,0 +91,0 @@ exports.sha256 = create('sha256');

{
"name": "crypto-hash",
"version": "1.1.0",
"version": "1.2.0",
"description": "Tiny hashing module that uses the native crypto API in Node.js and the browser",

@@ -21,3 +21,4 @@ "license": "MIT",

"index.d.ts",
"browser.js"
"browser.js",
"thread.js"
],

@@ -45,3 +46,8 @@ "keywords": [

},
"browser": "browser.js"
"browser": "browser.js",
"xo": {
"rules": {
"import/no-unresolved": "off"
}
}
}

@@ -44,3 +44,3 @@ # crypto-hash [![Build Status](https://travis-ci.org/sindresorhus/crypto-hash.svg?branch=master)](https://travis-ci.org/sindresorhus/crypto-hash)

*Note that even though it returns a promise, [in Node.js, the operation is synchronous 💩](https://github.com/nodejs/node/issues/678).*
*In Node.js 12 or later, the operation is executed using [`worker_threads`](https://nodejs.org/api/worker_threads.html). A thread is lazily spawned on the first operation and lives until the end of the program execution. It's `unref`ed, so it won't keep the process alive.*

@@ -47,0 +47,0 @@ [SHA-1 is insecure](https://stackoverflow.com/a/38045085/64949) and should not be used for anything sensitive.

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