3 Use `require('crypto')` to access this module.
5 The crypto module requires OpenSSL to be available on the underlying platform.
6 It offers a way of encapsulating secure credentials to be used as part
7 of a secure HTTPS net or http connection.
9 It also offers a set of wrappers for OpenSSL's hash, hmac, cipher, decipher, sign and verify methods.
11 ### crypto.createCredentials(details)
13 Creates a credentials object, with the optional details being a dictionary with keys:
15 * `key` : a string holding the PEM encoded private key
16 * `cert` : a string holding the PEM encoded certificate
17 * `ca` : either a string or list of strings of PEM encoded CA certificates to trust.
19 If no 'ca' details are given, then node.js will use the default publicly trusted list of CAs as given in
20 <http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt>.
23 ### crypto.createHash(algorithm)
25 Creates and returns a hash object, a cryptographic hash with the given algorithm
26 which can be used to generate hash digests.
28 `algorithm` is dependent on the available algorithms supported by the version
29 of OpenSSL on the platform. Examples are `'sha1'`, `'md5'`, `'sha256'`, `'sha512'`, etc.
30 On recent releases, `openssl list-message-digest-algorithms` will display the available digest algorithms.
32 Example: this program that takes the sha1 sum of a file
34 var filename = process.argv[2];
35 var crypto = require('crypto');
36 var fs = require('fs');
38 var shasum = crypto.createHash('sha1');
40 var s = fs.ReadStream(filename);
41 s.on('data', function(d) {
45 s.on('end', function() {
46 var d = shasum.digest('hex');
47 console.log(d + ' ' + filename);
52 Updates the hash content with the given `data`.
53 This can be called many times with new data as it is streamed.
55 ### hash.digest(encoding='binary')
57 Calculates the digest of all of the passed data to be hashed.
58 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
60 Note: `hash` object can not be used after `digest()` method been called.
63 ### crypto.createHmac(algorithm, key)
65 Creates and returns a hmac object, a cryptographic hmac with the given algorithm and key.
67 `algorithm` is dependent on the available algorithms supported by OpenSSL - see createHash above.
68 `key` is the hmac key to be used.
72 Update the hmac content with the given `data`.
73 This can be called many times with new data as it is streamed.
75 ### hmac.digest(encoding='binary')
77 Calculates the digest of all of the passed data to the hmac.
78 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
80 Note: `hmac` object can not be used after `digest()` method been called.
83 ### crypto.createCipher(algorithm, password)
85 Creates and returns a cipher object, with the given algorithm and password.
87 `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc.
88 On recent releases, `openssl list-cipher-algorithms` will display the
89 available cipher algorithms.
90 `password` is used to derive key and IV, which must be `'binary'` encoded
91 string (See the [Buffers](buffers.html) for more information).
93 ### crypto.createCipheriv(algorithm, key, iv)
95 Creates and returns a cipher object, with the given algorithm, key and iv.
97 `algorithm` is the same as the `createCipher()`. `key` is a raw key used in
98 algorithm. `iv` is an Initialization vector. `key` and `iv` must be `'binary'`
99 encoded string (See the [Buffers](buffers.html) for more information).
101 ### cipher.update(data, input_encoding='binary', output_encoding='binary')
103 Updates the cipher with `data`, the encoding of which is given in `input_encoding`
104 and can be `'utf8'`, `'ascii'` or `'binary'`. The `output_encoding` specifies
105 the output format of the enciphered data, and can be `'binary'`, `'base64'` or `'hex'`.
107 Returns the enciphered contents, and can be called many times with new data as it is streamed.
109 ### cipher.final(output_encoding='binary')
111 Returns any remaining enciphered contents, with `output_encoding` being one of: `'binary'`, `'base64'` or `'hex'`.
113 Note: `cipher` object can not be used after `final()` method been called.
116 ### crypto.createDecipher(algorithm, password)
118 Creates and returns a decipher object, with the given algorithm and key.
119 This is the mirror of the [createCipher()](#crypto.createCipher) above.
121 ### crypto.createDecipheriv(algorithm, key, iv)
123 Creates and returns a decipher object, with the given algorithm, key and iv.
124 This is the mirror of the [createCipheriv()](#crypto.createCipheriv) above.
126 ### decipher.update(data, input_encoding='binary', output_encoding='binary')
128 Updates the decipher with `data`, which is encoded in `'binary'`, `'base64'` or `'hex'`.
129 The `output_decoding` specifies in what format to return the deciphered plaintext: `'binary'`, `'ascii'` or `'utf8'`.
131 ### decipher.final(output_encoding='binary')
133 Returns any remaining plaintext which is deciphered,
134 with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
136 Note: `decipher` object can not be used after `final()` method been called.
139 ### crypto.createSign(algorithm)
141 Creates and returns a signing object, with the given algorithm.
142 On recent OpenSSL releases, `openssl list-public-key-algorithms` will display
143 the available signing algorithms. Examples are `'RSA-SHA256'`.
145 ### signer.update(data)
147 Updates the signer object with data.
148 This can be called many times with new data as it is streamed.
150 ### signer.sign(private_key, output_format='binary')
152 Calculates the signature on all the updated data passed through the signer.
153 `private_key` is a string containing the PEM encoded private key for signing.
155 Returns the signature in `output_format` which can be `'binary'`, `'hex'` or `'base64'`.
157 Note: `signer` object can not be used after `sign()` method been called.
160 ### crypto.createVerify(algorithm)
162 Creates and returns a verification object, with the given algorithm.
163 This is the mirror of the signing object above.
165 ### verifier.update(data)
167 Updates the verifier object with data.
168 This can be called many times with new data as it is streamed.
170 ### verifier.verify(object, signature, signature_format='binary')
172 Verifies the signed data by using the `object` and `signature`. `object` is a
173 string containing a PEM encoded object, which can be one of RSA public key,
174 DSA public key, or X.509 certificate. `signature` is the previously calculated
175 signature for the data, in the `signature_format` which can be `'binary'`,
176 `'hex'` or `'base64'`.
178 Returns true or false depending on the validity of the signature for the data and public key.
180 Note: `verifier` object can not be used after `verify()` method been called.