cross-sha256
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -0,1 +1,8 @@ | ||
# [1.2.0](https://github.com/zone117x/cross-sha256/compare/v1.1.2...v1.2.0) (2021-11-01) | ||
### Features | ||
* cache nodejs `crypto` module, around 4x speed up in hashing ([ff6c33e](https://github.com/zone117x/cross-sha256/commit/ff6c33e0e228f169b74ca68fcce95b5c5be8963c)) | ||
## [1.1.2](https://github.com/zone117x/cross-sha256/compare/v1.1.1...v1.1.2) (2020-08-19) | ||
@@ -2,0 +9,0 @@ |
@@ -45,2 +45,4 @@ /// <reference types="node" /> | ||
hash: Digest; | ||
static nodeCryptoCreateHash: any; | ||
static useCryptoModuleCache: boolean; | ||
constructor(); | ||
@@ -47,0 +49,0 @@ update(data: string | Buffer | Uint8Array, encoding?: string): this; |
16
index.js
@@ -165,3 +165,6 @@ "use strict"; | ||
function sha256nodeCrypto() { | ||
this.hash = require('crypto').createHash('sha256'); | ||
if (!sha256nodeCrypto.useCryptoModuleCache || sha256nodeCrypto.nodeCryptoCreateHash === undefined) { | ||
sha256nodeCrypto.nodeCryptoCreateHash = require('crypto').createHash; | ||
} | ||
this.hash = sha256nodeCrypto.nodeCryptoCreateHash('sha256'); | ||
} | ||
@@ -180,10 +183,16 @@ sha256nodeCrypto.prototype.update = function (data, encoding) { | ||
}; | ||
sha256nodeCrypto.useCryptoModuleCache = true; | ||
return sha256nodeCrypto; | ||
}()); | ||
exports.sha256nodeCrypto = sha256nodeCrypto; | ||
var _nodeCryptoAvailable = undefined; | ||
function isNodeCryptoAvailable() { | ||
if (sha256nodeCrypto.useCryptoModuleCache && _nodeCryptoAvailable !== undefined) { | ||
return _nodeCryptoAvailable; | ||
} | ||
var isAvailable = false; | ||
try { | ||
if (typeof require === 'function') { | ||
var cryptoModule = require('crypto'); | ||
return cryptoModule && typeof cryptoModule.createHash === 'function'; | ||
isAvailable = cryptoModule && typeof cryptoModule.createHash === 'function'; | ||
} | ||
@@ -194,3 +203,4 @@ } | ||
} | ||
return false; | ||
_nodeCryptoAvailable = isAvailable; | ||
return isAvailable; | ||
} | ||
@@ -197,0 +207,0 @@ var sha256 = /** @class */ (function () { |
25
index.ts
@@ -52,3 +52,3 @@ import { Buffer as SafeBuffer } from 'buffer/'; | ||
constructor () { | ||
this._block = SafeBuffer.alloc(this._blockSize) as Buffer | ||
this._block = SafeBuffer.alloc(this._blockSize) as unknown as Buffer | ||
} | ||
@@ -59,3 +59,3 @@ | ||
encoding = encoding || 'utf8' | ||
data = SafeBuffer.from(data, encoding) as Buffer; | ||
data = SafeBuffer.from(data, encoding) as unknown as Buffer; | ||
} | ||
@@ -121,3 +121,3 @@ | ||
const hash = SafeBuffer.alloc(32) as Buffer; | ||
const hash = SafeBuffer.alloc(32) as unknown as Buffer; | ||
hash.writeInt32BE(this._a, 0) | ||
@@ -132,3 +132,3 @@ hash.writeInt32BE(this._b, 4) | ||
return encoding ? hash.toString(encoding) : hash | ||
return encoding ? hash.toString(encoding as BufferEncoding) : hash | ||
} | ||
@@ -203,4 +203,9 @@ | ||
hash: Digest; | ||
static nodeCryptoCreateHash: any; | ||
static useCryptoModuleCache: boolean = true; | ||
constructor() { | ||
this.hash = require('crypto').createHash('sha256'); | ||
if (!sha256nodeCrypto.useCryptoModuleCache || sha256nodeCrypto.nodeCryptoCreateHash === undefined) { | ||
sha256nodeCrypto.nodeCryptoCreateHash = require('crypto').createHash; | ||
} | ||
this.hash = sha256nodeCrypto.nodeCryptoCreateHash('sha256'); | ||
} | ||
@@ -222,7 +227,12 @@ update(data: string | Buffer | Uint8Array, encoding?: string): this { | ||
let _nodeCryptoAvailable: boolean | undefined = undefined; | ||
function isNodeCryptoAvailable(): boolean { | ||
if (sha256nodeCrypto.useCryptoModuleCache && _nodeCryptoAvailable !== undefined) { | ||
return _nodeCryptoAvailable; | ||
} | ||
let isAvailable = false; | ||
try { | ||
if (typeof require === 'function') { | ||
const cryptoModule = require('crypto'); | ||
return cryptoModule && typeof cryptoModule.createHash === 'function'; | ||
isAvailable = cryptoModule && typeof cryptoModule.createHash === 'function'; | ||
} | ||
@@ -232,3 +242,4 @@ } catch (error) { | ||
} | ||
return false; | ||
_nodeCryptoAvailable = isAvailable; | ||
return isAvailable; | ||
} | ||
@@ -235,0 +246,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "SHA-256 hash in pure javascript", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"homepage": "https://github.com/zone117x/cross-sha256", | ||
@@ -16,11 +16,13 @@ "repository": { | ||
"dependencies": { | ||
"@types/node": "^8.0.0", | ||
"buffer": "^5.6.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.17.32", | ||
"@types/source-map-support": "^0.5.4", | ||
"@types/tape": "^4.13.0", | ||
"hash-test-vectors": "^1.3.1", | ||
"source-map-support": "^0.5.20", | ||
"tape": "~2.3.2", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^3.9.7" | ||
"ts-node": "^10.4.0", | ||
"typescript": "^4.4.4" | ||
}, | ||
@@ -30,3 +32,4 @@ "scripts": { | ||
"build": "tsc", | ||
"test": "ts-node-transpile-only test/index.ts" | ||
"test": "ts-node-transpile-only test/index.ts", | ||
"bench": "ts-node-transpile-only test/bench.ts" | ||
}, | ||
@@ -33,0 +36,0 @@ "files": [ |
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
23644
1
534
8
- Removed@types/node@^8.0.0
- Removed@types/node@8.10.66(transitive)