Registry / testing / hash-test-vectors

hash-test-vectors

JSON →
library1.3.2jsnpmunverified

The `hash-test-vectors` package provides a comprehensive, static collection of cryptographic test vectors for various hash functions, HMACs, and PBKDF2. It consolidates and expands test data primarily sourced from NIST (for SHA, MD5) and RFCs (for HMAC, PBKDF2), tailoring them to cover all hash algorithms supported by Node.js. This data is made available in a JSON format, making it highly suitable for validating JavaScript cryptographic implementations in both Node.js and browser environments. The package is currently at version 1.3.2, with its last update in late 2015, indicating a stable, maintenance-mode status as a dataset rather than an actively developed library. Its primary differentiator is simplifying the rigorous testing of crypto functions against standardized, pre-computed references.

npm install hash-test-vectors
INSTALL
IMPORT
SIG · HASH-TEST-VECTORS
H
hash-test-vectors
testingjavascriptv1.3.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

vectors
const vectors = require('hash-test-vectors')
import vectors from 'hash-test-vectors'
This package is CommonJS-only. For ESM usage, it must be imported via `import vectors = require('hash-test-vectors');` or by configuring your build system to handle CJS imports.
vectors
import vectors from 'hash-test-vectors'
import { vectors } from 'hash-test-vectors'
When using ESM (e.g., via a bundler or `type: module` in Node.js) with a CommonJS package that exports a single value, use a default import. Named imports will fail.

This example demonstrates how to iterate through the provided SHA-1 test vectors to validate a custom SHA-1 implementation, showing both base64 and Buffer input methods for comprehensive testing. Note: `tape` and `MySha1` are placeholders for demonstration.

const vectors = require('hash-test-vectors'); const tape = require('tape'); // Placeholder for your SHA-1 implementation class MySha1 { constructor() { this.hash = ''; /* initialize your hash state */ } update(data, encoding) { const inputBuffer = encoding === 'base64' ? Buffer.from(data, 'base64') : Buffer.from(data); // Implement your SHA-1 update logic here this.hash += inputBuffer.toString('hex'); // Simplified for example return this; } digest(encoding) { // Implement your SHA-1 digest logic here, returning the final hash // For this example, we'll just return the accumulated string. const realSha1 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; // Placeholder for actual hash if (this.hash.includes('hello')) return realSha1; // Simulate a correct hash return 'wronghash'; } } vectors.forEach(function (v, i) { if (!v.sha1) return; // Skip if vector doesn't have SHA1 tape('my-sha1 against test vector ' + i, function (t) { // Test with base64 encoded input const calculatedHashBase64 = new MySha1().update(v.input, 'base64').digest('hex'); t.equal(calculatedHashBase64, v.sha1, `SHA1 (base64) for vector ${i}`); // Test with buffer input const inputBuffer = Buffer.from(v.input, 'base64'); const calculatedHashBuffer = new MySha1().update(inputBuffer).digest('hex'); t.equal(calculatedHashBuffer, v.sha1, `SHA1 (buffer) for vector ${i}`); t.end(); }); });
Debug
Known issues
gotchaThe `Buffer` constructor (`new Buffer(...)`) is deprecated in Node.js and should be replaced with `Buffer.from()`, `Buffer.alloc()`, or `Buffer.allocUnsafe()` for security and clarity. Older examples in the package's README use the deprecated constructor.
fix
Replace `new Buffer(data, encoding)` with `Buffer.from(data, encoding)`.
affects: >=1.0.0
gotchaThis package is CommonJS-only. Attempting to use `import vectors from 'hash-test-vectors'` directly in an ESM module might lead to runtime errors in environments that do not automatically interop with CJS default exports, or if `type: module` is set in Node.js without proper configuration.
fix
In Node.js ESM, use dynamic `import('hash-test-vectors').then(mod => mod.default)` or `import vectors = require('hash-test-vectors');` if supported by your TypeScript configuration. For bundlers, ensure CJS interop is enabled.
affects: >=1.0.0
gotchaThe package's test vectors were last updated in 2015. While the NIST and RFC sources are stable, it will not include test vectors for cryptographic algorithms introduced or significantly updated since that time, nor will it incorporate any new hash functions supported by very recent Node.js versions.
fix
For newer algorithms or very recent updates, consult more current test vector sources or dedicated libraries, or consider generating vectors directly from official specifications.
affects: <1.4.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require()` in an ES Module context (e.g., a file with `type: module` in `package.json` or `.mjs` extension).
fix
If the file is intended to be an ESM module, use `import vectors from 'hash-test-vectors';` or configure your build system for CJS interop. Alternatively, change the file or project to use CommonJS.
TypeError: Buffer is not a constructor
Using `new Buffer(...)` in a Node.js version where the direct `Buffer` constructor has been deprecated and removed, requiring `Buffer.from()` or similar factory methods.
fix
Replace `new Buffer(data, encoding)` with `Buffer.from(data, encoding)`.
TypeError: vectors.forEach is not a function
This error typically occurs if the import of `hash-test-vectors` did not correctly retrieve the array of vectors, possibly due to an incorrect import statement (e.g., trying to named import from a CJS default export).
fix
Ensure you are using `const vectors = require('hash-test-vectors');` in CommonJS or `import vectors from 'hash-test-vectors';` in ESM contexts that support CJS default imports.
Upgrade
Version history
1.3.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
hash-test-vectors — npm install hash-test-vectors · libregistry