The Hmac
class is a utility for creating cryptographic HMAC digests. It can
be used in one of two ways:
hmac.update()
][] and [hmac.digest()
][] methods to produce the
computed HMAC digest.The [crypto.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 crypto = require('crypto');
const hmac = crypto.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:
const crypto = require('crypto');
const fs = require('fs');
const hmac = crypto.createHmac('sha256', 'a secret');
const input = fs.createReadStream('test.js');
input.pipe(hmac).pipe(process.stdout);
Example: Using the [hmac.update()
][] and [hmac.digest()
][] methods:
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'a secret');
hmac.update('some data to hash');
console.log(hmac.digest('hex'));
// Prints:
// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
hmac.digest([encoding])
encoding
{string} The [encoding][] of the return value.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.
hmac.update(data[, inputEncoding])
data
{string | Buffer | TypedArray | DataView}inputEncoding
{string} The [encoding][] of the data
string.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
, or
DataView
, then inputEncoding
is ignored.
This can be called many times with new data as it is streamed.
Was this page helpful?
很高兴听到! 请告诉我们,我们如何才能改善.
很遗憾听到这个消息。 请告诉我们,我们如何才能改善.