crypto-hash
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -1,14 +0,3 @@ | ||
/* eslint-env browser */ | ||
import {bufferToHex} from './utilities.js'; | ||
const bufferToHex = buffer => { | ||
const view = new DataView(buffer); | ||
let hexCodes = ''; | ||
for (let index = 0; index < view.byteLength; index += 4) { | ||
hexCodes += view.getUint32(index).toString(16).padStart(8, '0'); | ||
} | ||
return hexCodes; | ||
}; | ||
const create = algorithm => async (buffer, {outputFormat = 'hex'} = {}) => { | ||
@@ -15,0 +4,0 @@ if (typeof buffer === 'string') { |
21
index.js
@@ -1,4 +0,4 @@ | ||
import {Buffer} from 'node:buffer'; | ||
import {Worker} from 'node:worker_threads'; | ||
import crypto from 'node:crypto'; | ||
import {bufferToHex} from './utilities.js'; | ||
@@ -55,20 +55,7 @@ let create = algorithm => async (buffer, {outputFormat = 'hex'} = {}) => { | ||
create = algorithm => async (source, {outputFormat = 'hex'} = {}) => { | ||
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 { | ||
const finalSource = source instanceof ArrayBuffer ? new Uint8Array(source) : source; | ||
const {buffer} = typeof source === 'string' ? new TextEncoder().encode(source) : (source instanceof ArrayBuffer ? new Uint8Array(source) : source); | ||
// Creating a copy of buffer at call time, will be transfered later | ||
buffer = finalSource.buffer.slice(0); // eslint-disable-line unicorn/prefer-spread | ||
} | ||
const hash = await taskWorker({algorithm, buffer}, [buffer]); | ||
const result = await taskWorker({algorithm, buffer}, [buffer]); | ||
if (outputFormat === 'hex') { | ||
return Buffer.from(result).toString('hex'); | ||
} | ||
return result; | ||
return outputFormat === 'hex' ? bufferToHex(hash) : hash; | ||
}; | ||
@@ -75,0 +62,0 @@ } |
{ | ||
"name": "crypto-hash", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Tiny hashing module that uses the native crypto API in Node.js and the browser", | ||
@@ -19,2 +19,3 @@ "license": "MIT", | ||
}, | ||
"sideEffects": false, | ||
"engines": { | ||
@@ -31,3 +32,4 @@ "node": ">=18" | ||
"browser.js", | ||
"thread.js" | ||
"thread.js", | ||
"utilities.js" | ||
], | ||
@@ -48,8 +50,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"@sindresorhus/is": "^6.0.1", | ||
"ava": "^5.3.1", | ||
"@sindresorhus/is": "^7.0.0", | ||
"ava": "^6.1.3", | ||
"hash.js": "^1.1.7", | ||
"tsd": "^0.29.0", | ||
"xo": "^0.56.0" | ||
"tsd": "^0.31.1", | ||
"xo": "^0.59.2" | ||
} | ||
} |
@@ -1,12 +0,10 @@ | ||
import {Buffer} from 'node:buffer'; | ||
import crypto from 'node:crypto'; | ||
import {parentPort} from 'node:worker_threads'; | ||
parentPort.on('message', message => { | ||
const {algorithm, buffer} = message.value; | ||
parentPort.on('message', ({id, value: {algorithm, buffer}}) => { | ||
const hash = crypto.createHash(algorithm); | ||
hash.update(Buffer.from(buffer)); | ||
const arrayBuffer = hash.digest().buffer; | ||
hash.update(new DataView(buffer)); | ||
const value = hash.digest().buffer; | ||
// Transfering buffer here for consistency, but considering buffer size it might be faster to just leave it for copying, needs perf test | ||
parentPort.postMessage({id: message.id, value: arrayBuffer}, [arrayBuffer]); | ||
parentPort.postMessage({id, value}, [value]); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8
9201
154