{ "source": "doc/api/crypto.markdown", "modules": [ { "textRaw": "Crypto", "name": "crypto", "stability": 2, "stabilityText": "Stable", "desc": "

Use require('crypto') to access this module.\n\n

\n

The crypto module offers a way of encapsulating secure credentials to be\nused as part of a secure HTTPS net or http connection.\n\n

\n

It also offers a set of wrappers for OpenSSL's hash, hmac, cipher,\ndecipher, sign and verify methods.\n\n

\n", "classes": [ { "textRaw": "Class: Certificate", "type": "class", "name": "Certificate", "desc": "

The class used for working with signed public key & challenges. The most\ncommon usage for this series of functions is when dealing with the <keygen>\nelement. https://www.openssl.org/docs/apps/spkac.html\n\n

\n

Returned by crypto.Certificate.\n\n

\n", "methods": [ { "textRaw": "Certificate.exportChallenge(spkac)", "type": "method", "name": "exportChallenge", "desc": "

Exports the encoded challenge associated with the SPKAC.\n\n

\n", "signatures": [ { "params": [ { "name": "spkac" } ] } ] }, { "textRaw": "Certificate.exportPublicKey(spkac)", "type": "method", "name": "exportPublicKey", "desc": "

Exports the encoded public key from the supplied SPKAC.\n\n

\n", "signatures": [ { "params": [ { "name": "spkac" } ] } ] }, { "textRaw": "Certificate.verifySpkac(spkac)", "type": "method", "name": "verifySpkac", "desc": "

Returns true of false based on the validity of the SPKAC.\n\n

\n", "signatures": [ { "params": [ { "name": "spkac" } ] } ] } ] }, { "textRaw": "Class: Cipher", "type": "class", "name": "Cipher", "desc": "

Class for encrypting data.\n\n

\n

Returned by crypto.createCipher and crypto.createCipheriv.\n\n

\n

Cipher objects are [streams][] that are both readable and writable.\nThe written plain text data is used to produce the encrypted data on\nthe readable side. The legacy update and final methods are also\nsupported.\n\n

\n", "methods": [ { "textRaw": "cipher.final([output_encoding])", "type": "method", "name": "final", "desc": "

Returns any remaining enciphered contents, with output_encoding\nbeing one of: 'binary', 'base64' or 'hex'. If no encoding is\nprovided, then a buffer is returned.\n\n

\n

Note: cipher object can not be used after final() method has been\ncalled.\n\n

\n", "signatures": [ { "params": [ { "name": "output_encoding", "optional": true } ] } ] }, { "textRaw": "cipher.getAuthTag()", "type": "method", "name": "getAuthTag", "desc": "

For authenticated encryption modes (currently supported: GCM), this\nmethod returns a Buffer that represents the authentication tag that\nhas been computed from the given data. Should be called after\nencryption has been completed using the final method!\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "cipher.setAAD(buffer)", "type": "method", "name": "setAAD", "desc": "

For authenticated encryption modes (currently supported: GCM), this\nmethod sets the value used for the additional authenticated data (AAD) input\nparameter.\n\n

\n", "signatures": [ { "params": [ { "name": "buffer" } ] } ] }, { "textRaw": "cipher.setAutoPadding(auto_padding=true)", "type": "method", "name": "setAutoPadding", "desc": "

You can disable automatic padding of the input data to block size. If\nauto_padding is false, the length of the entire input data must be a\nmultiple of the cipher's block size or final will fail. Useful for\nnon-standard padding, e.g. using 0x0 instead of PKCS padding. You\nmust call this before cipher.final.\n\n

\n", "signatures": [ { "params": [ { "name": "auto_padding", "default": "true" } ] } ] }, { "textRaw": "cipher.update(data[, input_encoding][, output_encoding])", "type": "method", "name": "update", "desc": "

Updates the cipher with data, the encoding of which is given in\ninput_encoding and can be 'utf8', 'ascii' or 'binary'. If no\nencoding is provided, then a buffer is expected.\nIf data is a Buffer then input_encoding is ignored.\n\n

\n

The output_encoding specifies the output format of the enciphered\ndata, and can be 'binary', 'base64' or 'hex'. If no encoding is\nprovided, then a buffer is returned.\n\n

\n

Returns the enciphered contents, and can be called many times with new\ndata as it is streamed.\n\n

\n", "signatures": [ { "params": [ { "name": "data" }, { "name": "input_encoding", "optional": true }, { "name": "output_encoding", "optional": true } ] } ] } ] }, { "textRaw": "Class: Decipher", "type": "class", "name": "Decipher", "desc": "

Class for decrypting data.\n\n

\n

Returned by [crypto.createDecipher][] and [crypto.createDecipheriv][].\n\n

\n

Decipher objects are [streams][] that are both readable and writable.\nThe written enciphered data is used to produce the plain-text data on\nthe the readable side. The legacy update and final methods are also\nsupported.\n\n

\n", "methods": [ { "textRaw": "decipher.final([output_encoding])", "type": "method", "name": "final", "desc": "

Returns any remaining plaintext which is deciphered, with\noutput_encoding being one of: 'binary', 'ascii' or 'utf8'. If\nno encoding is provided, then a buffer is returned.\n\n

\n

Note: decipher object can not be used after final() method has been\ncalled.\n\n

\n", "signatures": [ { "params": [ { "name": "output_encoding", "optional": true } ] } ] }, { "textRaw": "decipher.setAAD(buffer)", "type": "method", "name": "setAAD", "desc": "

For authenticated encryption modes (currently supported: GCM), this\nmethod sets the value used for the additional authenticated data (AAD) input\nparameter.\n\n

\n", "signatures": [ { "params": [ { "name": "buffer" } ] } ] }, { "textRaw": "decipher.setAuthTag(buffer)", "type": "method", "name": "setAuthTag", "desc": "

For authenticated encryption modes (currently supported: GCM), this\nmethod must be used to pass in the received authentication tag.\nIf no tag is provided or if the ciphertext has been tampered with,\nfinal will throw, thus indicating that the ciphertext should\nbe discarded due to failed authentication.\n\n

\n", "signatures": [ { "params": [ { "name": "buffer" } ] } ] }, { "textRaw": "decipher.setAutoPadding(auto_padding=true)", "type": "method", "name": "setAutoPadding", "desc": "

You can disable auto padding if the data has been encrypted without\nstandard block padding to prevent decipher.final from checking and\nremoving it. This will only work if the input data's length is a multiple of\nthe ciphers block size. You must call this before streaming data to\n[decipher.update][].\n\n

\n", "signatures": [ { "params": [ { "name": "auto_padding", "default": "true" } ] } ] }, { "textRaw": "decipher.update(data[, input_encoding][, output_encoding])", "type": "method", "name": "update", "desc": "

Updates the decipher with data, which is encoded in 'binary',\n'base64' or 'hex'. If no encoding is provided, then a buffer is\nexpected.\nIf data is a Buffer then input_encoding is ignored.\n\n

\n

The output_decoding specifies in what format to return the\ndeciphered plaintext: 'binary', 'ascii' or 'utf8'. If no\nencoding is provided, then a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "data" }, { "name": "input_encoding", "optional": true }, { "name": "output_encoding", "optional": true } ] } ] } ] }, { "textRaw": "Class: DiffieHellman", "type": "class", "name": "DiffieHellman", "desc": "

The class for creating Diffie-Hellman key exchanges.\n\n

\n

Returned by crypto.createDiffieHellman.\n\n

\n", "methods": [ { "textRaw": "diffieHellman.computeSecret(other_public_key[, input_encoding][, output_encoding])", "type": "method", "name": "computeSecret", "desc": "

Computes the shared secret using other_public_key as the other\nparty's public key and returns the computed shared secret. Supplied\nkey is interpreted using specified input_encoding, and secret is\nencoded using specified output_encoding. Encodings can be\n'binary', 'hex', or 'base64'. If the input encoding is not\nprovided, then a buffer is expected.\n\n

\n

If no output encoding is given, then a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "other_public_key" }, { "name": "input_encoding", "optional": true }, { "name": "output_encoding", "optional": true } ] } ] }, { "textRaw": "diffieHellman.generateKeys([encoding])", "type": "method", "name": "generateKeys", "desc": "

Generates private and public Diffie-Hellman key values, and returns\nthe public key in the specified encoding. This key should be\ntransferred to the other party. Encoding can be 'binary', 'hex',\nor 'base64'. If no encoding is provided, then a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "diffieHellman.getGenerator([encoding])", "type": "method", "name": "getGenerator", "desc": "

Returns the Diffie-Hellman generator in the specified encoding, which can\nbe 'binary', 'hex', or 'base64'. If no encoding is provided,\nthen a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "diffieHellman.getPrime([encoding])", "type": "method", "name": "getPrime", "desc": "

Returns the Diffie-Hellman prime in the specified encoding, which can\nbe 'binary', 'hex', or 'base64'. If no encoding is provided,\nthen a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "diffieHellman.getPrivateKey([encoding])", "type": "method", "name": "getPrivateKey", "desc": "

Returns the Diffie-Hellman private key in the specified encoding,\nwhich can be 'binary', 'hex', or 'base64'. If no encoding is\nprovided, then a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "diffieHellman.getPublicKey([encoding])", "type": "method", "name": "getPublicKey", "desc": "

Returns the Diffie-Hellman public key in the specified encoding, which\ncan be 'binary', 'hex', or 'base64'. If no encoding is provided,\nthen a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "diffieHellman.setPrivateKey(private_key[, encoding])", "type": "method", "name": "setPrivateKey", "desc": "

Sets the Diffie-Hellman private key. Key encoding can be 'binary',\n'hex' or 'base64'. If no encoding is provided, then a buffer is\nexpected.\n\n

\n", "signatures": [ { "params": [ { "name": "private_key" }, { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "diffieHellman.setPublicKey(public_key[, encoding])", "type": "method", "name": "setPublicKey", "desc": "

Sets the Diffie-Hellman public key. Key encoding can be 'binary',\n'hex' or 'base64'. If no encoding is provided, then a buffer is\nexpected.\n\n

\n", "signatures": [ { "params": [ { "name": "public_key" }, { "name": "encoding", "optional": true } ] } ] } ], "properties": [ { "textRaw": "diffieHellman.verifyError", "name": "verifyError", "desc": "

A bit field containing any warnings and/or errors as a result of a check performed\nduring initialization. The following values are valid for this property\n(defined in constants module):\n\n

\n\n" } ] }, { "textRaw": "Class: ECDH", "type": "class", "name": "ECDH", "desc": "

The class for creating EC Diffie-Hellman key exchanges.\n\n

\n

Returned by crypto.createECDH.\n\n

\n", "methods": [ { "textRaw": "ECDH.computeSecret(other_public_key[, input_encoding][, output_encoding])", "type": "method", "name": "computeSecret", "desc": "

Computes the shared secret using other_public_key as the other\nparty's public key and returns the computed shared secret. Supplied\nkey is interpreted using specified input_encoding, and secret is\nencoded using specified output_encoding. Encodings can be\n'binary', 'hex', or 'base64'. If the input encoding is not\nprovided, then a buffer is expected.\n\n

\n

If no output encoding is given, then a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "other_public_key" }, { "name": "input_encoding", "optional": true }, { "name": "output_encoding", "optional": true } ] } ] }, { "textRaw": "ECDH.generateKeys([encoding[, format]])", "type": "method", "name": "generateKeys", "desc": "

Generates private and public EC Diffie-Hellman key values, and returns\nthe public key in the specified format and encoding. This key should be\ntransferred to the other party.\n\n

\n

Format specifies point encoding and can be 'compressed', 'uncompressed', or\n'hybrid'. If no format is provided - the point will be returned in\n'uncompressed' format.\n\n

\n

Encoding can be 'binary', 'hex', or 'base64'. If no encoding is provided,\nthen a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding" }, { "name": "format]", "optional": true } ] } ] }, { "textRaw": "ECDH.getPrivateKey([encoding])", "type": "method", "name": "getPrivateKey", "desc": "

Returns the EC Diffie-Hellman private key in the specified encoding,\nwhich can be 'binary', 'hex', or 'base64'. If no encoding is\nprovided, then a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "ECDH.getPublicKey([encoding[, format]])", "type": "method", "name": "getPublicKey", "desc": "

Returns the EC Diffie-Hellman public key in the specified encoding and format.\n\n

\n

Format specifies point encoding and can be 'compressed', 'uncompressed', or\n'hybrid'. If no format is provided - the point will be returned in\n'uncompressed' format.\n\n

\n

Encoding can be 'binary', 'hex', or 'base64'. If no encoding is provided,\nthen a buffer is returned.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding" }, { "name": "format]", "optional": true } ] } ] }, { "textRaw": "ECDH.setPrivateKey(private_key[, encoding])", "type": "method", "name": "setPrivateKey", "desc": "

Sets the EC Diffie-Hellman private key. Key encoding can be 'binary',\n'hex' or 'base64'. If no encoding is provided, then a buffer is\nexpected.\n\n

\n

Example (obtaining a shared secret):\n\n

\n
const crypto = require('crypto');\nconst alice = crypto.createECDH('secp256k1');\nconst bob = crypto.createECDH('secp256k1');\n\nalice.generateKeys();\nbob.generateKeys();\n\nconst alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');\nconst bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');\n\n/* alice_secret and bob_secret should be the same */\nconsole.log(alice_secret == bob_secret);
\n", "signatures": [ { "params": [ { "name": "private_key" }, { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "ECDH.setPublicKey(public_key[, encoding])", "type": "method", "name": "setPublicKey", "desc": "

Sets the EC Diffie-Hellman public key. Key encoding can be 'binary',\n'hex' or 'base64'. If no encoding is provided, then a buffer is\nexpected.\n\n

\n", "signatures": [ { "params": [ { "name": "public_key" }, { "name": "encoding", "optional": true } ] } ] } ] }, { "textRaw": "Class: Hash", "type": "class", "name": "Hash", "desc": "

The class for creating hash digests of data.\n\n

\n

It is a [stream][] that is both readable and writable. The written data\nis used to compute the hash. Once the writable side of the stream is ended,\nuse the read() method to get the computed hash digest. The legacy update\nand digest methods are also supported.\n\n

\n

Returned by crypto.createHash.\n\n

\n", "methods": [ { "textRaw": "hash.digest([encoding])", "type": "method", "name": "digest", "desc": "

Calculates the digest of all of the passed data to be hashed. The\nencoding can be 'hex', 'binary' or 'base64'. If no encoding\nis provided, then a buffer is returned.\n\n

\n

Note: hash object can not be used after digest() method has been\ncalled.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "hash.update(data[, input_encoding])", "type": "method", "name": "update", "desc": "

Updates the hash content with the given data, the encoding of which\nis given in input_encoding and can be 'utf8', 'ascii' or\n'binary'. If no encoding is provided, and the input is a string, an\nencoding of 'binary' is enforced. If data is a Buffer then\ninput_encoding is ignored.\n\n

\n

This can be called many times with new data as it is streamed.\n\n

\n", "signatures": [ { "params": [ { "name": "data" }, { "name": "input_encoding", "optional": true } ] } ] } ] }, { "textRaw": "Class: Hmac", "type": "class", "name": "Hmac", "desc": "

Class for creating cryptographic hmac content.\n\n

\n

Returned by crypto.createHmac.\n\n

\n", "methods": [ { "textRaw": "hmac.digest([encoding])", "type": "method", "name": "digest", "desc": "

Calculates the digest of all of the passed data to the hmac. The\nencoding can be 'hex', 'binary' or 'base64'. If no encoding\nis provided, then a buffer is returned.\n\n

\n

Note: hmac object can not be used after digest() method has been\ncalled.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "hmac.update(data)", "type": "method", "name": "update", "desc": "

Update the hmac content with the given data. This can be called\nmany times with new data as it is streamed.\n\n

\n", "signatures": [ { "params": [ { "name": "data" } ] } ] } ] }, { "textRaw": "Class: Sign", "type": "class", "name": "Sign", "desc": "

Class for generating signatures.\n\n

\n

Returned by crypto.createSign.\n\n

\n

Sign objects are writable [streams][]. The written data is used to\ngenerate the signature. Once all of the data has been written, the\nsign method will return the signature. The legacy update method\nis also supported.\n\n

\n", "methods": [ { "textRaw": "sign.sign(private_key[, output_format])", "type": "method", "name": "sign", "desc": "

Calculates the signature on all the updated data passed through the\nsign.\n\n

\n

private_key can be an object or a string. If private_key is a string, it is\ntreated as the key with no passphrase.\n\n

\n

private_key:\n\n

\n\n

Returns the signature in output_format which can be 'binary',\n'hex' or 'base64'. If no encoding is provided, then a buffer is\nreturned.\n\n

\n

Note: sign object can not be used after sign() method has been\ncalled.\n\n

\n", "signatures": [ { "params": [ { "name": "private_key" }, { "name": "output_format", "optional": true } ] } ] }, { "textRaw": "sign.update(data)", "type": "method", "name": "update", "desc": "

Updates the sign object with data. This can be called many times\nwith new data as it is streamed.\n\n

\n", "signatures": [ { "params": [ { "name": "data" } ] } ] } ] }, { "textRaw": "Class: Verify", "type": "class", "name": "Verify", "desc": "

Class for verifying signatures.\n\n

\n

Returned by crypto.createVerify.\n\n

\n

Verify objects are writable [streams][]. The written data is used to\nvalidate against the supplied signature. Once all of the data has been\nwritten, the verify method will return true if the supplied signature\nis valid. The legacy update method is also supported.\n\n

\n", "methods": [ { "textRaw": "verifier.update(data)", "type": "method", "name": "update", "desc": "

Updates the verifier object with data. This can be called many times\nwith new data as it is streamed.\n\n

\n", "signatures": [ { "params": [ { "name": "data" } ] } ] }, { "textRaw": "verifier.verify(object, signature[, signature_format])", "type": "method", "name": "verify", "desc": "

Verifies the signed data by using the object and signature.\nobject is a string containing a PEM encoded object, which can be\none of RSA public key, DSA public key, or X.509 certificate.\nsignature is the previously calculated signature for the data, in\nthe signature_format which can be 'binary', 'hex' or 'base64'.\nIf no encoding is specified, then a buffer is expected.\n\n

\n

Returns true or false depending on the validity of the signature for\nthe data and public key.\n\n

\n

Note: verifier object can not be used after verify() method has been\ncalled.\n\n

\n", "signatures": [ { "params": [ { "name": "object" }, { "name": "signature" }, { "name": "signature_format", "optional": true } ] } ] } ] } ], "properties": [ { "textRaw": "crypto.DEFAULT_ENCODING", "name": "DEFAULT_ENCODING", "desc": "

The default encoding to use for functions that can take either strings\nor buffers. The default value is 'buffer', which makes it default\nto using Buffer objects. This is here to make the crypto module more\neasily compatible with legacy programs that expected 'binary' to be\nthe default encoding.\n\n

\n

Note that new programs will probably expect buffers, so only use this\nas a temporary measure.\n\n

\n" } ], "methods": [ { "textRaw": "crypto.createCipher(algorithm, password)", "type": "method", "name": "createCipher", "desc": "

Creates and returns a cipher object, with the given algorithm and\npassword.\n\n

\n

algorithm is dependent on OpenSSL, examples are 'aes192', etc. On\nrecent releases, openssl list-cipher-algorithms will display the\navailable cipher algorithms. password is used to derive key and IV,\nwhich must be a 'binary' encoded string or a [buffer][].\n\n

\n

It is a [stream][] that is both readable and writable. The written data\nis used to compute the hash. Once the writable side of the stream is ended,\nuse the read() method to get the enciphered contents. The legacy update\nand final methods are also supported.\n\n

\n

Note: createCipher derives keys with the OpenSSL function [EVP_BytesToKey][]\nwith the digest algorithm set to MD5, one iteration, and no salt. The lack of\nsalt allows dictionary attacks as the same password always creates the same key.\nThe low iteration count and non-cryptographically secure hash algorithm allow\npasswords to be tested very rapidly.\n\n

\n

In line with OpenSSL's recommendation to use pbkdf2 instead of [EVP_BytesToKey][] it\nis recommended you derive a key and iv yourself with [crypto.pbkdf2][] and to\nthen use [createCipheriv()][] to create the cipher stream.\n\n

\n", "signatures": [ { "params": [ { "name": "algorithm" }, { "name": "password" } ] } ] }, { "textRaw": "crypto.createCipheriv(algorithm, key, iv)", "type": "method", "name": "createCipheriv", "desc": "

Creates and returns a cipher object, with the given algorithm, key and\niv.\n\n

\n

algorithm is the same as the argument to createCipher(). key is\nthe raw key used by the algorithm. iv is an [initialization vector][].\n\n

\n

key and iv must be 'binary' encoded strings or [buffers][].\n\n

\n", "signatures": [ { "params": [ { "name": "algorithm" }, { "name": "key" }, { "name": "iv" } ] } ] }, { "textRaw": "crypto.createCredentials(details)", "type": "method", "name": "createCredentials", "stability": 0, "stabilityText": "Deprecated: Use [`tls.createSecureContext`][] instead.", "desc": "

Creates a credentials object, with the optional details being a\ndictionary with keys:\n\n

\n\n

If no 'ca' details are given, then Node.js will use the default\npublicly trusted list of CAs as given in\n

\n

http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt.\n\n

\n", "signatures": [ { "params": [ { "name": "details" } ] } ] }, { "textRaw": "crypto.createDecipher(algorithm, password)", "type": "method", "name": "createDecipher", "desc": "

Creates and returns a decipher object, with the given algorithm and\nkey. This is the mirror of the [createCipher()][] above.\n\n

\n", "signatures": [ { "params": [ { "name": "algorithm" }, { "name": "password" } ] } ] }, { "textRaw": "crypto.createDecipheriv(algorithm, key, iv)", "type": "method", "name": "createDecipheriv", "desc": "

Creates and returns a decipher object, with the given algorithm, key\nand iv. This is the mirror of the [createCipheriv()][] above.\n\n

\n", "signatures": [ { "params": [ { "name": "algorithm" }, { "name": "key" }, { "name": "iv" } ] } ] }, { "textRaw": "crypto.createDiffieHellman(prime[, prime_encoding][, generator][, generator_encoding])", "type": "method", "name": "createDiffieHellman", "desc": "

Creates a Diffie-Hellman key exchange object using the supplied prime and an\noptional specific generator.\ngenerator can be a number, string, or Buffer.\nIf no generator is specified, then 2 is used.\nprime_encoding and generator_encoding can be 'binary', 'hex', or 'base64'.\nIf no prime_encoding is specified, then a Buffer is expected for prime.\nIf no generator_encoding is specified, then a Buffer is expected for generator.\n\n

\n", "signatures": [ { "params": [ { "name": "prime" }, { "name": "prime_encoding", "optional": true }, { "name": "generator", "optional": true }, { "name": "generator_encoding", "optional": true } ] } ] }, { "textRaw": "crypto.createDiffieHellman(prime_length[, generator])", "type": "method", "name": "createDiffieHellman", "desc": "

Creates a Diffie-Hellman key exchange object and generates a prime of\nprime_length bits and using an optional specific numeric generator.\nIf no generator is specified, then 2 is used.\n\n

\n", "signatures": [ { "params": [ { "name": "prime_length" }, { "name": "generator", "optional": true } ] } ] }, { "textRaw": "crypto.createECDH(curve_name)", "type": "method", "name": "createECDH", "desc": "

Creates an Elliptic Curve (EC) Diffie-Hellman key exchange object using a\npredefined curve specified by the curve_name string. Use [getCurves()][] to\nobtain a list of available curve names. On recent releases,\nopenssl ecparam -list_curves will also display the name and description of\neach available elliptic curve.\n\n

\n", "signatures": [ { "params": [ { "name": "curve_name" } ] } ] }, { "textRaw": "crypto.createHash(algorithm)", "type": "method", "name": "createHash", "desc": "

Creates and returns a hash object, a cryptographic hash with the given\nalgorithm which can be used to generate hash digests.\n\n

\n

algorithm is dependent on the available algorithms supported by the\nversion of OpenSSL on the platform. Examples are 'sha256',\n'sha512', etc. On recent releases, openssl\nlist-message-digest-algorithms will display the available digest\nalgorithms.\n\n

\n

Example: this program that takes the sha256 sum of a file\n\n

\n
const filename = process.argv[2];\nconst crypto = require('crypto');\nconst fs = require('fs');\n\nconst shasum = crypto.createHash('sha256');\n\nconst s = fs.ReadStream(filename);\ns.on('data', (d) => {\n  shasum.update(d);\n});\n\ns.on('end', () => {\n  var d = shasum.digest('hex');\n  console.log(`${d}  ${filename}`);\n});
\n", "signatures": [ { "params": [ { "name": "algorithm" } ] } ] }, { "textRaw": "crypto.createHmac(algorithm, key)", "type": "method", "name": "createHmac", "desc": "

Creates and returns a hmac object, a cryptographic hmac with the given\nalgorithm and key.\n\n

\n

It is a [stream][] that is both readable and writable. The written\ndata is used to compute the hmac. Once the writable side of the\nstream is ended, use the read() method to get the computed digest.\nThe legacy update and digest methods are also supported.\n\n

\n

algorithm is dependent on the available algorithms supported by\nOpenSSL - see createHash above. key is the hmac key to be used.\n\n

\n", "signatures": [ { "params": [ { "name": "algorithm" }, { "name": "key" } ] } ] }, { "textRaw": "crypto.createSign(algorithm)", "type": "method", "name": "createSign", "desc": "

Creates and returns a signing object, with the given algorithm. On\nrecent OpenSSL releases, openssl list-public-key-algorithms will\ndisplay the available signing algorithms. Examples are 'RSA-SHA256'.\n\n

\n", "signatures": [ { "params": [ { "name": "algorithm" } ] } ] }, { "textRaw": "crypto.createVerify(algorithm)", "type": "method", "name": "createVerify", "desc": "

Creates and returns a verification object, with the given algorithm.\nThis is the mirror of the signing object above.\n\n

\n", "signatures": [ { "params": [ { "name": "algorithm" } ] } ] }, { "textRaw": "crypto.getCiphers()", "type": "method", "name": "getCiphers", "desc": "

Returns an array with the names of the supported ciphers.\n\n

\n

Example:\n\n

\n
const ciphers = crypto.getCiphers();\nconsole.log(ciphers); // ['aes-128-cbc', 'aes-128-ccm', ...]
\n", "signatures": [ { "params": [] } ] }, { "textRaw": "crypto.getCurves()", "type": "method", "name": "getCurves", "desc": "

Returns an array with the names of the supported elliptic curves.\n\n

\n

Example:\n\n

\n
const curves = crypto.getCurves();\nconsole.log(curves); // ['secp256k1', 'secp384r1', ...]
\n", "signatures": [ { "params": [] } ] }, { "textRaw": "crypto.getDiffieHellman(group_name)", "type": "method", "name": "getDiffieHellman", "desc": "

Creates a predefined Diffie-Hellman key exchange object. The\nsupported groups are: 'modp1', 'modp2', 'modp5' (defined in\n[RFC 2412][], but see [Caveats][]) and 'modp14', 'modp15',\n'modp16', 'modp17', 'modp18' (defined in [RFC 3526][]). The\nreturned object mimics the interface of objects created by\n[crypto.createDiffieHellman()][] above, but will not allow changing\nthe keys (with [diffieHellman.setPublicKey()][] for example). The\nadvantage of using this routine is that the parties do not have to\ngenerate nor exchange group modulus beforehand, saving both processor\nand communication time.\n\n

\n

Example (obtaining a shared secret):\n\n

\n
const crypto = require('crypto');\nconst alice = crypto.getDiffieHellman('modp14');\nconst bob = crypto.getDiffieHellman('modp14');\n\nalice.generateKeys();\nbob.generateKeys();\n\nconst alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');\nconst bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');\n\n/* alice_secret and bob_secret should be the same */\nconsole.log(alice_secret == bob_secret);
\n", "signatures": [ { "params": [ { "name": "group_name" } ] } ] }, { "textRaw": "crypto.getHashes()", "type": "method", "name": "getHashes", "desc": "

Returns an array with the names of the supported hash algorithms.\n\n

\n

Example:\n\n

\n
const hashes = crypto.getHashes();\nconsole.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]
\n", "signatures": [ { "params": [] } ] }, { "textRaw": "crypto.pbkdf2(password, salt, iterations, keylen[, digest], callback)", "type": "method", "name": "pbkdf2", "desc": "

Asynchronous PBKDF2 function. Applies the selected HMAC digest function\n(default: SHA1) to derive a key of the requested byte length from the password,\nsalt and number of iterations. The callback gets two arguments:\n(err, derivedKey).\n\n

\n

The number of iterations passed to pbkdf2 should be as high as possible, the\nhigher the number, the more secure it will be, but will take a longer amount of\ntime to complete.\n\n

\n

Chosen salts should also be unique. It is recommended that the salts are random\nand their length is greater than 16 bytes. See [NIST SP 800-132] for details.\n\n

\n

Example:\n\n

\n
crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', function(err, key) {\n  if (err)\n    throw err;\n  console.log(key.toString('hex'));  // 'c5e478d...1469e50'\n});
\n

You can get a list of supported digest functions with [crypto.getHashes()][].\n\n

\n", "signatures": [ { "params": [ { "name": "password" }, { "name": "salt" }, { "name": "iterations" }, { "name": "keylen" }, { "name": "digest", "optional": true }, { "name": "callback" } ] } ] }, { "textRaw": "crypto.pbkdf2Sync(password, salt, iterations, keylen[, digest])", "type": "method", "name": "pbkdf2Sync", "desc": "

Synchronous PBKDF2 function. Returns derivedKey or throws error.\n\n

\n", "signatures": [ { "params": [ { "name": "password" }, { "name": "salt" }, { "name": "iterations" }, { "name": "keylen" }, { "name": "digest", "optional": true } ] } ] }, { "textRaw": "crypto.privateDecrypt(private_key, buffer)", "type": "method", "name": "privateDecrypt", "desc": "

Decrypts buffer with private_key.\n\n

\n

private_key can be an object or a string. If private_key is a string, it is\ntreated as the key with no passphrase and will use RSA_PKCS1_OAEP_PADDING.\n\n

\n

private_key:\n\n

\n\n

NOTE: All paddings are defined in constants module.\n\n

\n", "signatures": [ { "params": [ { "name": "private_key" }, { "name": "buffer" } ] } ] }, { "textRaw": "crypto.privateEncrypt(private_key, buffer)", "type": "method", "name": "privateEncrypt", "desc": "

See above for details. Has the same API as crypto.privateDecrypt.\nDefault padding is RSA_PKCS1_PADDING.\n\n

\n", "signatures": [ { "params": [ { "name": "private_key" }, { "name": "buffer" } ] } ] }, { "textRaw": "crypto.publicDecrypt(public_key, buffer)", "type": "method", "name": "publicDecrypt", "desc": "

See above for details. Has the same API as crypto.publicEncrypt. Default\npadding is RSA_PKCS1_PADDING.\n\n

\n", "signatures": [ { "params": [ { "name": "public_key" }, { "name": "buffer" } ] } ] }, { "textRaw": "crypto.publicEncrypt(public_key, buffer)", "type": "method", "name": "publicEncrypt", "desc": "

Encrypts buffer with public_key. Only RSA is currently supported.\n\n

\n

public_key can be an object or a string. If public_key is a string, it is\ntreated as the key with no passphrase and will use RSA_PKCS1_OAEP_PADDING.\nSince RSA public keys may be derived from private keys you may pass a private\nkey to this method.\n\n

\n

public_key:\n\n

\n\n

NOTE: All paddings are defined in constants module.\n\n

\n", "signatures": [ { "params": [ { "name": "public_key" }, { "name": "buffer" } ] } ] }, { "textRaw": "crypto.randomBytes(size[, callback])", "type": "method", "name": "randomBytes", "desc": "

Generates cryptographically strong pseudo-random data. Usage:\n\n

\n
// async\ncrypto.randomBytes(256, (ex, buf) => {\n  if (ex) throw ex;\n  console.log('Have %d bytes of random data: %s', buf.length, buf);\n});\n\n// sync\nconst buf = crypto.randomBytes(256);\nconsole.log('Have %d bytes of random data: %s', buf.length, buf);
\n

NOTE: This will block if there is insufficient entropy, although it should\nnormally never take longer than a few milliseconds. The only time when this\nmay conceivably block is right after boot, when the whole system is still\nlow on entropy.\n\n

\n", "signatures": [ { "params": [ { "name": "size" }, { "name": "callback", "optional": true } ] } ] }, { "textRaw": "crypto.setEngine(engine[, flags])", "type": "method", "name": "setEngine", "desc": "

Load and set engine for some/all OpenSSL functions (selected by flags).\n\n

\n

engine could be either an id or a path to the engine's shared library.\n\n

\n

flags is optional and has ENGINE_METHOD_ALL value by default. It could take\none of or mix of following flags (defined in constants module):\n\n

\n\n", "signatures": [ { "params": [ { "name": "engine" }, { "name": "flags", "optional": true } ] } ] } ], "modules": [ { "textRaw": "Recent API Changes", "name": "recent_api_changes", "desc": "

The Crypto module was added to Node.js before there was the concept of a\nunified Stream API, and before there were Buffer objects for handling\nbinary data.\n\n

\n

As such, the streaming classes don't have the typical methods found on\nother Node.js classes, and many methods accepted and returned\nBinary-encoded strings by default rather than Buffers. This was\nchanged to use Buffers by default instead.\n\n

\n

This is a breaking change for some use cases, but not all.\n\n

\n

For example, if you currently use the default arguments to the Sign\nclass, and then pass the results to the Verify class, without ever\ninspecting the data, then it will continue to work as before. Where\nyou once got a binary string and then presented the binary string to\nthe Verify object, you'll now get a Buffer, and present the Buffer to\nthe Verify object.\n\n

\n

However, if you were doing things with the string data that will not\nwork properly on Buffers (such as, concatenating them, storing in\ndatabases, etc.), or you are passing binary strings to the crypto\nfunctions without an encoding argument, then you will need to start\nproviding encoding arguments to specify which encoding you'd like to\nuse. To switch to the previous style of using binary strings by\ndefault, set the crypto.DEFAULT_ENCODING field to 'binary'. Note\nthat new programs will probably expect buffers, so only use this as a\ntemporary measure.\n\n

\n", "type": "module", "displayName": "Recent API Changes" }, { "textRaw": "Caveats", "name": "caveats", "desc": "

The crypto module still supports some algorithms which are already\ncompromised. And the API also allows the use of ciphers and hashes\nwith a small key size that are considered to be too weak for safe use.\n\n

\n

Users should take full responsibility for selecting the crypto\nalgorithm and key size according to their security requirements.\n\n

\n

Based on the recommendations of [NIST SP 800-131A]:\n\n

\n\n

See the reference for other recommendations and details.\n\n

\n", "type": "module", "displayName": "Caveats" } ], "type": "module", "displayName": "Crypto" } ] }