Usage in Deno
import { Hmac } from "node:crypto";
Since v20.13.0 Calling Hmac
class directly with Hmac()
or new Hmac()
is deprecated due to being internals, not intended for public use. Please use the createHmac method to create Hmac instances.
The Hmac
class is a utility for creating cryptographic HMAC digests. It can
be used in one of two ways:
- As a
stream
that is both readable and writable, where data is written to produce a computed HMAC digest on the readable side, or - Using the
hmac.update()
andhmac.digest()
methods to produce the computed HMAC digest.
The createHmac method is used to create Hmac
instances. Hmac
objects are not to be created directly using the new
keyword.
Example: Using Hmac
objects as streams:
const {
createHmac,
} = await import('node:crypto');
const hmac = createHmac('sha256', 'a secret');
hmac.on('readable', () => {
// Only one element is going to be produced by the
// hash stream.
const data = hmac.read();
if (data) {
console.log(data.toString('hex'));
// Prints:
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
}
});
hmac.write('some data to hash');
hmac.end();
Example: Using Hmac
and piped streams:
import { createReadStream } from 'node:fs';
import { stdout } from 'node:process';
const {
createHmac,
} = await import('node:crypto');
const hmac = createHmac('sha256', 'a secret');
const input = createReadStream('test.js');
input.pipe(hmac).pipe(stdout);
Example: Using the hmac.update()
and hmac.digest()
methods:
const {
createHmac,
} = await import('node:crypto');
const hmac = createHmac('sha256', 'a secret');
hmac.update('some data to hash');
console.log(hmac.digest('hex'));
// Prints:
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
digest(): Buffer
Calculates the HMAC digest of all of the data passed using hmac.update()
.
If encoding
is
provided a string is returned; otherwise a Buffer
is returned;
The Hmac
object can not be used again after hmac.digest()
has been
called. Multiple calls to hmac.digest()
will result in an error being thrown.
digest(encoding: BinaryToTextEncoding): string
update(data: BinaryLike): Hmac
Updates the Hmac
content with the given data
, the encoding of which
is given in inputEncoding
.
If encoding
is not provided, and the data
is a string, an
encoding of 'utf8'
is enforced. If data
is a Buffer
, TypedArray
, orDataView
, then inputEncoding
is ignored.
This can be called many times with new data as it is streamed.