{ "source": "doc/api/crypto.markdown", "modules": [ { "textRaw": "Crypto", "name": "crypto", "desc": "
Stability: 2 - Unstable; API changes are being discussed for\nfuture versions.  Breaking changes will be minimized.  See below.
\n

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

\n

The crypto module requires OpenSSL to be available on the underlying platform.\nIt offers a way of encapsulating secure credentials to be used as part\nof a secure HTTPS net or http connection.\n\n

\n

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

\n", "methods": [ { "textRaw": "crypto.createCredentials(details)", "type": "method", "name": "createCredentials", "desc": "

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

\n\n

If no 'ca' details are given, then node.js will use the default publicly 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

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

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

\n

algorithm is dependent on the available algorithms supported by the version\nof OpenSSL on the platform. Examples are 'sha1', 'md5', 'sha256', 'sha512', etc.\nOn recent releases, openssl list-message-digest-algorithms will display the available digest algorithms.\n\n

\n

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

\n
var filename = process.argv[2];\nvar crypto = require('crypto');\nvar fs = require('fs');\n\nvar shasum = crypto.createHash('sha1');\n\nvar s = fs.ReadStream(filename);\ns.on('data', function(d) {\n  shasum.update(d);\n});\n\ns.on('end', function() {\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 algorithm and key.\n\n

\n

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

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

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

\n

algorithm is dependent on OpenSSL, examples are 'aes192', etc.\nOn recent releases, openssl list-cipher-algorithms will display the\navailable cipher algorithms.\npassword is used to derive key and IV, which must be a 'binary' encoded\nstring or a buffer.\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 iv.\n\n

\n

algorithm is the same as the argument to createCipher().\nkey is the raw key used by the algorithm.\niv is an initialization\nvector.\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.createDecipher(algorithm, password)", "type": "method", "name": "createDecipher", "desc": "

Creates and returns a decipher object, with the given algorithm and key.\nThis 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 and iv.\nThis is the mirror of the [createCipheriv()][] above.\n\n

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

Creates and returns a signing object, with the given algorithm.\nOn recent OpenSSL releases, openssl list-public-key-algorithms will display\nthe 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.createDiffieHellman(prime_length)", "type": "method", "name": "createDiffieHellman", "desc": "

Creates a Diffie-Hellman key exchange object and generates a prime of the\ngiven bit length. The generator used is 2.\n\n

\n", "signatures": [ { "params": [ { "name": "prime_length" } ] } ] }, { "textRaw": "crypto.createDiffieHellman(prime, [encoding])", "type": "method", "name": "createDiffieHellman", "desc": "

Creates a Diffie-Hellman key exchange object using the supplied prime. The\ngenerator used is 2. Encoding can be 'binary', 'hex', or 'base64'.\nDefaults to 'binary'.\n\n

\n", "signatures": [ { "params": [ { "name": "prime" }, { "name": "encoding", "optional": true } ] } ] }, { "textRaw": "crypto.getDiffieHellman(group_name)", "type": "method", "name": "getDiffieHellman", "desc": "

Creates a predefined Diffie-Hellman key exchange object.\nThe supported groups are: 'modp1', 'modp2', 'modp5'\n(defined in [RFC 2412][])\nand 'modp14', 'modp15', 'modp16', 'modp17', 'modp18'\n(defined in [RFC 3526][]).\nThe returned object mimics the interface of objects created by\n[crypto.createDiffieHellman()][] above, but\nwill not allow to change the keys (with\n[diffieHellman.setPublicKey()][] for example).\nThe advantage of using this routine is that the parties don't have to\ngenerate nor exchange group modulus beforehand, saving both processor and\ncommunication time.\n\n

\n

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

\n
var crypto = require('crypto');\nvar alice = crypto.getDiffieHellman('modp5');\nvar bob = crypto.getDiffieHellman('modp5');\n\nalice.generateKeys();\nbob.generateKeys();\n\nvar alice_secret = alice.computeSecret(bob.getPublicKey(), 'binary', 'hex');\nvar bob_secret = bob.computeSecret(alice.getPublicKey(), 'binary', '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.pbkdf2(password, salt, iterations, keylen, callback)", "type": "method", "name": "pbkdf2", "desc": "

Asynchronous PBKDF2 applies pseudorandom function HMAC-SHA1 to derive\na key of given length from the given password, salt and iterations.\nThe callback gets two arguments (err, derivedKey).\n\n

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

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

\n
// async\ncrypto.randomBytes(256, function(ex, buf) {\n  if (ex) throw ex;\n  console.log('Have %d bytes of random data: %s', buf.length, buf);\n});\n\n// sync\ntry {\n  var buf = crypto.randomBytes(256);\n  console.log('Have %d bytes of random data: %s', buf.length, buf);\n} catch (ex) {\n  // handle error\n}
\n", "signatures": [ { "params": [ { "name": "size" }, { "name": "callback", "optional": true } ] } ] } ], "classes": [ { "textRaw": "Class: Hash", "type": "class", "name": "Hash", "desc": "

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

\n

Returned by crypto.createHash.\n\n

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

Updates the hash content with the given data, the encoding of which is given\nin input_encoding and can be 'utf8', 'ascii' or 'binary'.\nDefaults to 'binary'.\nThis 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": "hash.digest([encoding])", "type": "method", "name": "digest", "desc": "

Calculates the digest of all of the passed data to be hashed.\nThe encoding can be 'hex', 'binary' or 'base64'.\nDefaults to 'binary'.\n\n

\n

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

\n", "signatures": [ { "params": [ { "name": "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.update(data)", "type": "method", "name": "update", "desc": "

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

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

Calculates the digest of all of the passed data to the hmac.\nThe encoding can be 'hex', 'binary' or 'base64'.\nDefaults to 'binary'.\n\n

\n

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

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] } ] }, { "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", "methods": [ { "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'.\nDefaults to 'binary'.\n\n

\n

The output_encoding specifies the output format of the enciphered data,\nand can be 'binary', 'base64' or 'hex'. Defaults to 'binary'.\n\n

\n

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

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

Returns any remaining enciphered contents, with output_encoding being one of:\n'binary', 'base64' or 'hex'. Defaults to 'binary'.\n\n

\n

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

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

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

\n", "signatures": [ { "params": [ { "name": "auto_padding", "default": "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", "methods": [ { "textRaw": "decipher.update(data, [input_encoding], [output_encoding])", "type": "method", "name": "update", "desc": "

Updates the decipher with data, which is encoded in 'binary', 'base64'\nor 'hex'. Defaults to 'binary'.\n\n

\n

The output_decoding specifies in what format to return the deciphered\nplaintext: 'binary', 'ascii' or 'utf8'. Defaults to 'binary'.\n\n

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

Returns any remaining plaintext which is deciphered,\nwith output_encoding being one of: 'binary', 'ascii' or 'utf8'.\nDefaults to 'binary'.\n\n

\n

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

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

You can disable auto padding if the data has been encrypted without standard block padding to prevent\ndecipher.final from checking and removing it. Can only work if the input data's length is a multiple of the\nciphers block size. You must call this before streaming data to decipher.update.\n\n

\n", "signatures": [ { "params": [ { "name": "auto_padding", "default": "true" } ] } ] } ] }, { "textRaw": "Class: Signer", "type": "class", "name": "Signer", "desc": "

Class for generating signatures.\n\n

\n

Returned by crypto.createSign.\n\n

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

Updates the signer object with data.\nThis can be called many times with new data as it is streamed.\n\n

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

Calculates the signature on all the updated data passed through the signer.\nprivate_key is a string containing the PEM encoded private key for signing.\n\n

\n

Returns the signature in output_format which can be 'binary', 'hex' or\n'base64'. Defaults to 'binary'.\n\n

\n

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

\n", "signatures": [ { "params": [ { "name": "private_key" }, { "name": "output_format", "optional": true } ] } ] } ] }, { "textRaw": "Class: Verify", "type": "class", "name": "Verify", "desc": "

Class for verifying signatures.\n\n

\n

Returned by crypto.createVerify.\n\n

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

Updates the verifier object with data.\nThis can be called many times with 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. object is a\nstring containing a PEM encoded object, which can be one of RSA public key,\nDSA public key, or X.509 certificate. signature is the previously calculated\nsignature for the data, in the signature_format which can be 'binary',\n'hex' or 'base64'. Defaults to 'binary'.\n\n

\n

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

\n

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

\n", "signatures": [ { "params": [ { "name": "object" }, { "name": "signature" }, { "name": "signature_format", "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.generateKeys([encoding])", "type": "method", "name": "generateKeys", "desc": "

Generates private and public Diffie-Hellman key values, and returns the\npublic key in the specified encoding. This key should be transferred to the\nother party. Encoding can be 'binary', 'hex', or 'base64'.\nDefaults to 'binary'.\n\n

\n", "signatures": [ { "params": [ { "name": "encoding", "optional": true } ] } ] }, { "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 party's\npublic key and returns the computed shared secret. Supplied key is\ninterpreted using specified input_encoding, and secret is encoded using\nspecified output_encoding. Encodings can be 'binary', 'hex', or\n'base64'. The input encoding defaults to 'binary'.\nIf no output encoding is given, the input encoding is used as output encoding.\n\n

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

Returns the Diffie-Hellman prime in the specified encoding, which can be\n'binary', 'hex', or 'base64'. Defaults to 'binary'.\n\n

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

Returns the Diffie-Hellman prime in the specified encoding, which can be\n'binary', 'hex', or 'base64'. Defaults to 'binary'.\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 can\nbe 'binary', 'hex', or 'base64'. Defaults to 'binary'.\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, which can\nbe 'binary', 'hex', or 'base64'. Defaults to 'binary'.\n\n

\n", "signatures": [ { "params": [ { "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', 'hex',\nor 'base64'. Defaults to 'binary'.\n\n

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

Sets the Diffie-Hellman private key. Key encoding can be 'binary', 'hex',\nor 'base64'. Defaults to 'binary'.\n\n

\n", "signatures": [ { "params": [ { "name": "public_key" }, { "name": "encoding", "optional": true } ] } ] } ] } ], "modules": [ { "textRaw": "Proposed API Changes in Future Versions of Node", "name": "proposed_api_changes_in_future_versions_of_node", "desc": "

The Crypto module was added to Node 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 classes, and many methods accept and return Binary-encoded\nstrings by default rather than Buffers.\n\n

\n

A future version of node will make Buffers the default data type.\nThis will be 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 now get a binary string and then present the binary string to the\nVerify object, you'll get a Buffer, and present the Buffer to the\nVerify object.\n\n

\n

However, if you are 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.\n\n

\n

Also, a Streaming API will be provided, but this will be done in such\na way as to preserve the legacy API surface.\n\n\n

\n", "type": "module", "displayName": "Proposed API Changes in Future Versions of Node" } ], "type": "module", "displayName": "Crypto" } ] }