Sign
The Sign
class is a utility for generating signatures. It can be used in one of two ways:
As a writable
stream
, where data to be signed is written and thesign.sign()
method is used to generate and return the signature, orUsing the
sign.update()
andsign.sign()
methods to produce the signature.
The {@link createSign} method is used to create Sign
instances. The argument is the string name of the hash function to use. Sign
objects are not to be created directly using the new
keyword.
Example: Using Sign
and Verify
objects as streams:
const {
generateKeyPairSync,
createSign,
createVerify,
} = await import('node:crypto');
const { privateKey, publicKey } = generateKeyPairSync('ec', {
namedCurve: 'sect239k1',
});
const sign = createSign('SHA256');
sign.write('some data to sign');
sign.end();
const signature = sign.sign(privateKey, 'hex');
const verify = createVerify('SHA256');
verify.write('some data to sign');
verify.end();
console.log(verify.verify(publicKey, signature, 'hex'));
// Prints: true
Example: Using the sign.update()
and verify.update()
methods:
const {
generateKeyPairSync,
createSign,
createVerify,
} = await import('node:crypto');
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
modulusLength: 2048,
});
const sign = createSign('SHA256');
sign.update('some data to sign');
sign.end();
const signature = sign.sign(privateKey);
const verify = createVerify('SHA256');
verify.update('some data to sign');
verify.end();
console.log(verify.verify(publicKey, signature));
// Prints: true
Since
v0.1.92
Properties
Is true
if it is safe to call writable.write()
, which means the stream has not been destroyed, errored, or ended.
Number of times writable.uncork()
needs to be called in order to fully uncork the stream.
Is true
after writable.end()
has been called. This property does not indicate whether the data has been flushed, for this use writable.writableFinished
instead.
Is set to true
immediately before the 'finish'
event is emitted.
Return the value of highWaterMark
passed when creating this Writable
.
This property contains the number of bytes (or objects) in the queue ready to be written. The value provides introspection data regarding the status of the highWaterMark
.
Is true
if the stream's buffer has been full and stream will emit 'drain'
.
Getter for the property objectMode
of a given Writable
stream.
Functions
Event emitter The defined events on documents including:
Destroy the stream. Optionally emit an 'error'
event, and emit a 'close'
event (unless emitClose
is set to false
). After this call, the writable stream has ended and subsequent calls to write()
or end()
will result in an ERR_STREAM_DESTROYED
error. This is a destructive and immediate way to destroy a stream. Previous calls to write()
may not have drained, and may trigger an ERR_STREAM_DESTROYED
error. Use end()
instead of destroy if data should flush before close, or wait for the 'drain'
event before destroying the stream.
Calling the writable.end()
method signals that no more data will be written to the Writable
. The optional chunk
and encoding
arguments allow one final additional chunk of data to be written immediately before closing the stream.
Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbol
s.
Returns the current max listener value for the EventEmitter
which is either set by emitter.setMaxListeners(n)
or defaults to {@link defaultMaxListeners}.
The writable.setDefaultEncoding()
method sets the default encoding
for a Writable
stream.
By default EventEmitter
s will print a warning if more than 10
listeners are added for a particular event. This is a useful default that helps finding memory leaks. The emitter.setMaxListeners()
method allows the limit to be modified for this specific EventEmitter
instance. The value can be set to Infinity
(or 0
) to indicate an unlimited number of listeners.
Calculates the signature on all the data passed through using either sign.update()
or sign.write()
.
Updates the Sign
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.
The writable.write()
method writes some data to the stream, and calls the supplied callback
once the data has been fully handled. If an error occurs, the callback
will be called with the error as its first argument. The callback
is called asynchronously and before 'error'
is emitted.