{"id":13283,"library":"hash-test-vectors","title":"Hash Test Vectors","description":"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.","status":"maintenance","version":"1.3.2","language":"javascript","source_language":"en","source_url":"git://github.com/crypto-browserify/hash-test-vectors","tags":["javascript"],"install":[{"cmd":"npm install hash-test-vectors","lang":"bash","label":"npm"},{"cmd":"yarn add hash-test-vectors","lang":"bash","label":"yarn"},{"cmd":"pnpm add hash-test-vectors","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import vectors from 'hash-test-vectors'","symbol":"vectors","correct":"const vectors = require('hash-test-vectors')"},{"note":"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.","wrong":"import { vectors } from 'hash-test-vectors'","symbol":"vectors","correct":"import vectors from 'hash-test-vectors'"}],"quickstart":{"code":"const vectors = require('hash-test-vectors');\nconst tape = require('tape');\n\n// Placeholder for your SHA-1 implementation\nclass MySha1 {\n  constructor() { this.hash = ''; /* initialize your hash state */ }\n  update(data, encoding) {\n    const inputBuffer = encoding === 'base64' ? Buffer.from(data, 'base64') : Buffer.from(data);\n    // Implement your SHA-1 update logic here\n    this.hash += inputBuffer.toString('hex'); // Simplified for example\n    return this;\n  }\n  digest(encoding) {\n    // Implement your SHA-1 digest logic here, returning the final hash\n    // For this example, we'll just return the accumulated string.\n    const realSha1 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; // Placeholder for actual hash\n    if (this.hash.includes('hello')) return realSha1; // Simulate a correct hash\n    return 'wronghash';\n  }\n}\n\nvectors.forEach(function (v, i) {\n  if (!v.sha1) return; // Skip if vector doesn't have SHA1\n\n  tape('my-sha1 against test vector ' + i, function (t) {\n    // Test with base64 encoded input\n    const calculatedHashBase64 = new MySha1().update(v.input, 'base64').digest('hex');\n    t.equal(calculatedHashBase64, v.sha1, `SHA1 (base64) for vector ${i}`);\n\n    // Test with buffer input\n    const inputBuffer = Buffer.from(v.input, 'base64');\n    const calculatedHashBuffer = new MySha1().update(inputBuffer).digest('hex');\n    t.equal(calculatedHashBuffer, v.sha1, `SHA1 (buffer) for vector ${i}`);\n    t.end();\n  });\n});","lang":"javascript","description":"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."},"warnings":[{"fix":"Replace `new Buffer(data, encoding)` with `Buffer.from(data, encoding)`.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"This 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"<1.4.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"Attempting to use `require()` in an ES Module context (e.g., a file with `type: module` in `package.json` or `.mjs` extension).","error":"ReferenceError: require is not defined"},{"fix":"Replace `new Buffer(data, encoding)` with `Buffer.from(data, encoding)`.","cause":"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.","error":"TypeError: Buffer is not a constructor"},{"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.","cause":"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).","error":"TypeError: vectors.forEach is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}