{ "source": "doc/api/crypto.markdown", "modules": [ { "textRaw": "Crypto", "name": "crypto", "stability": 3, "stabilityText": "Stable", "desc": "
Use require('crypto')
to access this module.\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
\nIt 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
\npfx
: A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificateskey
: a string holding the PEM encoded private keycert
: a string holding the PEM encoded certificatepassphrase
: A string of passphrase for the private key or pfxca
: either a string or list of strings of PEM encoded CA certificates to trust.ciphers
: a string describing the ciphers to use or exclude. Consult\nhttp://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT for details\non the format.If no 'ca' details are given, then node.js will use the default publicly trusted list of CAs as given in\n
\nhttp://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
\nalgorithm
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
Example: this program that takes the sha1 sum of a file\n\n
\nvar 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
\nalgorithm
is dependent on the available algorithms supported by OpenSSL - see createHash above.\nkey
is the hmac key to be used.\n\n
Creates and returns a cipher object, with the given algorithm and password.\n\n
\nalgorithm
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 'binary'
encoded\nstring (See the Buffer section for more information).\n\n
Creates and returns a cipher object, with the given algorithm, key and iv.\n\n
\nalgorithm
is the same as the createCipher()
. key
is a raw key used in\nalgorithm. iv
is an Initialization vector. key
and iv
must be 'binary'
\nencoded string (See the Buffer section for more information).\n\n
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
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
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
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
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
\nReturned by crypto.createHash
.\n\n
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
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
Note: hash
object can not be used after digest()
method been called.\n\n\n
Class for creating cryptographic hmac content.\n\n
\nReturned by crypto.createHmac
.\n\n
Update the hmac content with the given data
.\nThis can be called many times with new data as it is streamed.\n\n
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
Note: hmac
object can not be used after digest()
method been called.\n\n\n
Class for encrypting data.\n\n
\nReturned by crypto.createCipher
and crypto.createCipheriv
.\n\n
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
The output_encoding
specifies the output format of the enciphered data,\nand can be 'binary'
, 'base64'
or 'hex'
. Defaults to 'binary'
.\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
Note: cipher
object can not be used after final()
method been called.\n\n\n
Class for decrypting data.\n\n
\nReturned by crypto.createDecipher
and crypto.createDecipheriv
.\n\n
Updates the decipher with data
, which is encoded in 'binary'
, 'base64'
\nor 'hex'
. Defaults to 'binary'
.\n\n
The output_decoding
specifies in what format to return the deciphered\nplaintext: 'binary'
, 'ascii'
or 'utf8'
. Defaults to 'binary'
.\n\n
Returns any remaining plaintext which is deciphered,\nwith output_encoding
being one of: 'binary'
, 'ascii'
or 'utf8'
.\nDefaults to 'binary'
.\n\n
Note: decipher
object can not be used after final()
method been called.\n\n\n
Class for generating signatures.\n\n
\nReturned by crypto.createSign
.\n\n
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
Returns the signature in output_format
which can be 'binary'
, 'hex'
or\n'base64'
. Defaults to 'binary'
.\n\n
Note: signer
object can not be used after sign()
method been called.\n\n
Class for verifying signatures.\n\n
\nReturned by crypto.createVerify
.\n\n
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
Returns true or false depending on the validity of the signature for the data and public key.\n\n
\nNote: verifier
object can not be used after verify()
method been called.\n\n
The class for creating Diffie-Hellman key exchanges.\n\n
\nReturned by crypto.createDiffieHellman
.\n\n
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
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
Returns the Diffie-Hellman prime in the specified encoding, which can be\n'binary'
, 'hex'
, or 'base64'
. Defaults to 'binary'
.\n\n
Returns the Diffie-Hellman prime in the specified encoding, which can be\n'binary'
, 'hex'
, or 'base64'
. Defaults to 'binary'
.\n\n
Returns the Diffie-Hellman public key in the specified encoding, which can\nbe 'binary'
, 'hex'
, or 'base64'
. Defaults to 'binary'
.\n\n
Returns the Diffie-Hellman private key in the specified encoding, which can\nbe 'binary'
, 'hex'
, or 'base64'
. Defaults to 'binary'
.\n\n
Sets the Diffie-Hellman public key. Key encoding can be 'binary'
, 'hex'
,\nor 'base64'
. Defaults to 'binary'
.\n\n
Sets the Diffie-Hellman private key. Key encoding can be 'binary'
, 'hex'
,\nor 'base64'
. Defaults to 'binary'
.\n\n