Node.js-style HMACs for Browsers
create-hmac is a foundational package within the crypto-browserify ecosystem, designed to provide a Node.js-compatible API for HMAC (Hash-based Message Authentication Code) functionality, primarily for browser environments. It shims the Node.js `crypto.createHmac` API, allowing code written for Node.js to function in the browser without modification. For Node.js environments, it utilizes the native `crypto` module, ensuring optimal performance. The current stable version is 1.1.7. Due to the static nature of cryptographic algorithms implemented and its status as a shim for a well-defined API, its release cadence is infrequent, focusing on stability rather than active feature development. Its key differentiator is providing API compatibility, enabling universal JavaScript codebases to perform HMAC operations across different runtimes reliably, though modern browser applications might prefer the Web Crypto API for native performance and security benefits.
Common errors
-
ReferenceError: Buffer is not defined
cause In browser environments, the `Buffer` global object, which is native to Node.js, is not automatically available. `create-hmac` expects `Buffer` instances for keys and input.fixYou need to explicitly polyfill `Buffer` in your browser environment. This is commonly done by installing and importing the `buffer` package from npm: `npm install buffer` and then `const { Buffer } = require('buffer');` at the top of your files, or configuring your bundler (e.g., Webpack's `fallback` option) to provide it. -
Error: Not a string, buffer, or ArrayBuffer
cause The `hmac.update()` method expects its input to be a string, a Node.js `Buffer`, or an `ArrayBuffer`. Passing other types (e.g., plain JavaScript objects or numbers) will result in this error.fixEnsure all data passed to `hmac.update()` is converted to a string, Buffer, or ArrayBuffer first. For example, `hmac.update(JSON.stringify(myObject))` or `hmac.update(Buffer.from(myArray))`.
Warnings
- gotcha The `create-hmac` package is part of the `crypto-browserify` project, which aims to polyfill Node.js's `crypto` module for browser environments. While functional, modern browser applications are encouraged to use the native Web Crypto API for better performance, security, and integration with the browser's cryptographic primitives.
- gotcha This package has seen very limited maintenance and updates since 2019. While the underlying cryptographic algorithms are stable, the lack of active development means that it may not address new browser-specific quirks, performance optimizations, or potential future vulnerabilities promptly. Relying on unmaintained dependencies, especially in security-sensitive areas, carries inherent risks.
Install
-
npm install create-hmac -
yarn add create-hmac -
pnpm add create-hmac
Imports
- createHmac
import { createHmac } from 'create-hmac'const createHmac = require('create-hmac') - createHmac (with types)
import { createHmac } from 'create-hmac';import createHmac from 'create-hmac';
Quickstart
const createHmac = require('create-hmac');
const { Buffer } = require('buffer'); // Polyfill for browser environments
// Example 1: Synchronous HMAC generation
const secretKeySync = Buffer.from('supersecret', 'utf8');
const hmacSync = createHmac('sha256', secretKeySync);
hmacSync.update('This is the message to sign.');
const digestSync = hmacSync.digest('hex');
console.log('Synchronous HMAC (SHA256, hex):', digestSync);
// Example 2: HMAC as a stream
const secretKeyStream = Buffer.from('another-secret', 'utf8');
const hmacStream = createHmac('sha512', secretKeyStream);
hmacStream.on('data', chunk => {
console.log('Stream chunk:', chunk.toString('hex'));
});
hmacStream.on('end', () => {
console.log('Stream HMAC generation complete.');
});
hmacStream.write('Part one of the streamed data.');
hmacStream.write('Part two of the streamed data.');
hmacStream.end();