5 Use `require('crypto')` to access this module.
7 The crypto module requires OpenSSL to be available on the underlying platform.
8 It offers a way of encapsulating secure credentials to be used as part
9 of a secure HTTPS net or http connection.
11 It also offers a set of wrappers for OpenSSL's hash, hmac, cipher, decipher, sign and verify methods.
13 ## crypto.createCredentials(details)
15 Creates a credentials object, with the optional details being a dictionary with keys:
17 * `pfx` : A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates
18 * `key` : a string holding the PEM encoded private key
19 * `cert` : a string holding the PEM encoded certificate
20 * `passphrase` : A string of passphrase for the private key or pfx
21 * `ca` : either a string or list of strings of PEM encoded CA certificates to trust.
22 * `ciphers`: a string describing the ciphers to use or exclude. Consult
23 <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT> for details
26 If no 'ca' details are given, then node.js will use the default publicly trusted list of CAs as given in
27 <http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt>.
30 ## crypto.createHash(algorithm)
32 Creates and returns a hash object, a cryptographic hash with the given algorithm
33 which can be used to generate hash digests.
35 `algorithm` is dependent on the available algorithms supported by the version
36 of OpenSSL on the platform. Examples are `'sha1'`, `'md5'`, `'sha256'`, `'sha512'`, etc.
37 On recent releases, `openssl list-message-digest-algorithms` will display the available digest algorithms.
39 Example: this program that takes the sha1 sum of a file
41 var filename = process.argv[2];
42 var crypto = require('crypto');
43 var fs = require('fs');
45 var shasum = crypto.createHash('sha1');
47 var s = fs.ReadStream(filename);
48 s.on('data', function(d) {
52 s.on('end', function() {
53 var d = shasum.digest('hex');
54 console.log(d + ' ' + filename);
59 The class for creating hash digests of data.
61 Returned by `crypto.createHash`.
63 ### hash.update(data, [input_encoding])
65 Updates the hash content with the given `data`, the encoding of which is given
66 in `input_encoding` and can be `'utf8'`, `'ascii'` or `'binary'`.
67 Defaults to `'binary'`.
68 This can be called many times with new data as it is streamed.
70 ### hash.digest([encoding])
72 Calculates the digest of all of the passed data to be hashed.
73 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
74 Defaults to `'binary'`.
76 Note: `hash` object can not be used after `digest()` method been called.
79 ## crypto.createHmac(algorithm, key)
81 Creates and returns a hmac object, a cryptographic hmac with the given algorithm and key.
83 `algorithm` is dependent on the available algorithms supported by OpenSSL - see createHash above.
84 `key` is the hmac key to be used.
88 Class for creating cryptographic hmac content.
90 Returned by `crypto.createHmac`.
94 Update the hmac content with the given `data`.
95 This can be called many times with new data as it is streamed.
97 ### hmac.digest([encoding])
99 Calculates the digest of all of the passed data to the hmac.
100 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
101 Defaults to `'binary'`.
103 Note: `hmac` object can not be used after `digest()` method been called.
106 ## crypto.createCipher(algorithm, password)
108 Creates and returns a cipher object, with the given algorithm and password.
110 `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc.
111 On recent releases, `openssl list-cipher-algorithms` will display the
112 available cipher algorithms.
113 `password` is used to derive key and IV, which must be `'binary'` encoded
114 string (See the [Buffer section](buffer.html) for more information).
116 ## crypto.createCipheriv(algorithm, key, iv)
118 Creates and returns a cipher object, with the given algorithm, key and iv.
120 `algorithm` is the same as the `createCipher()`. `key` is a raw key used in
121 algorithm. `iv` is an Initialization vector. `key` and `iv` must be `'binary'`
122 encoded string (See the [Buffer section](buffer.html) for more information).
126 Class for encrypting data.
128 Returned by `crypto.createCipher` and `crypto.createCipheriv`.
130 ### cipher.update(data, [input_encoding], [output_encoding])
132 Updates the cipher with `data`, the encoding of which is given in
133 `input_encoding` and can be `'utf8'`, `'ascii'` or `'binary'`.
134 Defaults to `'binary'`.
136 The `output_encoding` specifies the output format of the enciphered data,
137 and can be `'binary'`, `'base64'` or `'hex'`. Defaults to `'binary'`.
139 Returns the enciphered contents, and can be called many times with new data as it is streamed.
141 ### cipher.final([output_encoding])
143 Returns any remaining enciphered contents, with `output_encoding` being one of:
144 `'binary'`, `'base64'` or `'hex'`. Defaults to `'binary'`.
146 Note: `cipher` object can not be used after `final()` method been called.
149 ## crypto.createDecipher(algorithm, password)
151 Creates and returns a decipher object, with the given algorithm and key.
152 This is the mirror of the [createCipher()](#crypto.createCipher) above.
154 ## crypto.createDecipheriv(algorithm, key, iv)
156 Creates and returns a decipher object, with the given algorithm, key and iv.
157 This is the mirror of the [createCipheriv()](#crypto.createCipheriv) above.
161 Class for decrypting data.
163 Returned by `crypto.createDecipher` and `crypto.createDecipheriv`.
165 ### decipher.update(data, [input_encoding], [output_encoding])
167 Updates the decipher with `data`, which is encoded in `'binary'`, `'base64'`
168 or `'hex'`. Defaults to `'binary'`.
170 The `output_decoding` specifies in what format to return the deciphered
171 plaintext: `'binary'`, `'ascii'` or `'utf8'`. Defaults to `'binary'`.
173 ### decipher.final([output_encoding])
175 Returns any remaining plaintext which is deciphered,
176 with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
177 Defaults to `'binary'`.
179 Note: `decipher` object can not be used after `final()` method been called.
182 ## crypto.createSign(algorithm)
184 Creates and returns a signing object, with the given algorithm.
185 On recent OpenSSL releases, `openssl list-public-key-algorithms` will display
186 the available signing algorithms. Examples are `'RSA-SHA256'`.
190 Class for generating signatures.
192 Returned by `crypto.createSign`.
194 ### signer.update(data)
196 Updates the signer object with data.
197 This can be called many times with new data as it is streamed.
199 ### signer.sign(private_key, [output_format])
201 Calculates the signature on all the updated data passed through the signer.
202 `private_key` is a string containing the PEM encoded private key for signing.
204 Returns the signature in `output_format` which can be `'binary'`, `'hex'` or
205 `'base64'`. Defaults to `'binary'`.
207 Note: `signer` object can not be used after `sign()` method been called.
209 ## crypto.createVerify(algorithm)
211 Creates and returns a verification object, with the given algorithm.
212 This is the mirror of the signing object above.
216 Class for verifying signatures.
218 Returned by `crypto.createVerify`.
220 ### verifier.update(data)
222 Updates the verifier object with data.
223 This can be called many times with new data as it is streamed.
225 ### verifier.verify(object, signature, [signature_format])
227 Verifies the signed data by using the `object` and `signature`. `object` is a
228 string containing a PEM encoded object, which can be one of RSA public key,
229 DSA public key, or X.509 certificate. `signature` is the previously calculated
230 signature for the data, in the `signature_format` which can be `'binary'`,
231 `'hex'` or `'base64'`. Defaults to `'binary'`.
233 Returns true or false depending on the validity of the signature for the data and public key.
235 Note: `verifier` object can not be used after `verify()` method been called.
237 ## crypto.createDiffieHellman(prime_length)
239 Creates a Diffie-Hellman key exchange object and generates a prime of the
240 given bit length. The generator used is `2`.
242 ## crypto.createDiffieHellman(prime, [encoding])
244 Creates a Diffie-Hellman key exchange object using the supplied prime. The
245 generator used is `2`. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
246 Defaults to `'binary'`.
248 ## Class: DiffieHellman
250 The class for creating Diffie-Hellman key exchanges.
252 Returned by `crypto.createDiffieHellman`.
254 ### diffieHellman.generateKeys([encoding])
256 Generates private and public Diffie-Hellman key values, and returns the
257 public key in the specified encoding. This key should be transferred to the
258 other party. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
259 Defaults to `'binary'`.
261 ### diffieHellman.computeSecret(other_public_key, [input_encoding], [output_encoding])
263 Computes the shared secret using `other_public_key` as the other party's
264 public key and returns the computed shared secret. Supplied key is
265 interpreted using specified `input_encoding`, and secret is encoded using
266 specified `output_encoding`. Encodings can be `'binary'`, `'hex'`, or
267 `'base64'`. The input encoding defaults to `'binary'`.
268 If no output encoding is given, the input encoding is used as output encoding.
270 ### diffieHellman.getPrime([encoding])
272 Returns the Diffie-Hellman prime in the specified encoding, which can be
273 `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
275 ### diffieHellman.getGenerator([encoding])
277 Returns the Diffie-Hellman prime in the specified encoding, which can be
278 `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
280 ### diffieHellman.getPublicKey([encoding])
282 Returns the Diffie-Hellman public key in the specified encoding, which can
283 be `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
285 ### diffieHellman.getPrivateKey([encoding])
287 Returns the Diffie-Hellman private key in the specified encoding, which can
288 be `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
290 ### diffieHellman.setPublicKey(public_key, [encoding])
292 Sets the Diffie-Hellman public key. Key encoding can be `'binary'`, `'hex'`,
293 or `'base64'`. Defaults to `'binary'`.
295 ### diffieHellman.setPrivateKey(public_key, [encoding])
297 Sets the Diffie-Hellman private key. Key encoding can be `'binary'`, `'hex'`,
298 or `'base64'`. Defaults to `'binary'`.
300 ## crypto.pbkdf2(password, salt, iterations, keylen, callback)
302 Asynchronous PBKDF2 applies pseudorandom function HMAC-SHA1 to derive
303 a key of given length from the given password, salt and iterations.
304 The callback gets two arguments `(err, derivedKey)`.
306 ## crypto.randomBytes(size, [callback])
308 Generates cryptographically strong pseudo-random data. Usage:
311 crypto.randomBytes(256, function(ex, buf) {
313 console.log('Have %d bytes of random data: %s', buf.length, buf);
318 var buf = crypto.randomBytes(256);
319 console.log('Have %d bytes of random data: %s', buf.length, buf);