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'`.
61 ### crypto.createHmac(algorithm, key)
63 Creates and returns a hmac object, a cryptographic hmac with the given algorithm and key.
65 `algorithm` is dependent on the available algorithms supported by OpenSSL - see createHash above.
66 `key` is the hmac key to be used.
70 Update the hmac content with the given `data`.
71 This can be called many times with new data as it is streamed.
73 ### hmac.digest(encoding='binary')
75 Calculates the digest of all of the passed data to the hmac.
76 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
79 ### crypto.createCipher(algorithm, password)
81 Creates and returns a cipher object, with the given algorithm and password.
83 `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc.
84 On recent releases, `openssl list-cipher-algorithms` will display the
85 available cipher algorithms.
86 `password` is used to derive key and IV, which must be `'binary'` encoded
87 string (See the [Buffers](buffers.html) for more information).
89 ### crypto.createCipheriv(algorithm, key, iv)
91 Creates and returns a cipher object, with the given algorithm, key and iv.
93 `algorithm` is the same as the `createCipher()`. `key` is a raw key used in
94 algorithm. `iv` is an Initialization vector. `key` and `iv` must be `'binary'`
95 encoded string (See the [Buffers](buffers.html) for more information).
97 ### cipher.update(data, input_encoding='binary', output_encoding='binary')
99 Updates the cipher with `data`, the encoding of which is given in `input_encoding`
100 and can be `'utf8'`, `'ascii'` or `'binary'`. The `output_encoding` specifies
101 the output format of the enciphered data, and can be `'binary'`, `'base64'` or `'hex'`.
103 Returns the enciphered contents, and can be called many times with new data as it is streamed.
105 ### cipher.final(output_encoding='binary')
107 Returns any remaining enciphered contents, with `output_encoding` being one of: `'binary'`, `'base64'` or `'hex'`.
109 ### crypto.createDecipher(algorithm, password)
111 Creates and returns a decipher object, with the given algorithm and key.
112 This is the mirror of the [createCipher()](#crypto.createCipher) above.
114 ### crypto.createDecipheriv(algorithm, key, iv)
116 Creates and returns a decipher object, with the given algorithm, key and iv.
117 This is the mirror of the [createCipheriv()](#crypto.createCipheriv) above.
119 ### decipher.update(data, input_encoding='binary', output_encoding='binary')
121 Updates the decipher with `data`, which is encoded in `'binary'`, `'base64'` or `'hex'`.
122 The `output_decoding` specifies in what format to return the deciphered plaintext: `'binary'`, `'ascii'` or `'utf8'`.
124 ### decipher.final(output_encoding='binary')
126 Returns any remaining plaintext which is deciphered,
127 with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
130 ### crypto.createSign(algorithm)
132 Creates and returns a signing object, with the given algorithm.
133 On recent OpenSSL releases, `openssl list-public-key-algorithms` will display
134 the available signing algorithms. Examples are `'RSA-SHA256'`.
136 ### signer.update(data)
138 Updates the signer object with data.
139 This can be called many times with new data as it is streamed.
141 ### signer.sign(private_key, output_format='binary')
143 Calculates the signature on all the updated data passed through the signer.
144 `private_key` is a string containing the PEM encoded private key for signing.
146 Returns the signature in `output_format` which can be `'binary'`, `'hex'` or `'base64'`.
148 ### crypto.createVerify(algorithm)
150 Creates and returns a verification object, with the given algorithm.
151 This is the mirror of the signing object above.
153 ### verifier.update(data)
155 Updates the verifier object with data.
156 This can be called many times with new data as it is streamed.
158 ### verifier.verify(object, signature, signature_format='binary')
160 Verifies the signed data by using the `object` and `signature`. `object` is a
161 string containing a PEM encoded object, which can be one of RSA public key,
162 DSA public key, or X.509 certificate. `signature` is the previously calculated
163 signature for the data, in the `signature_format` which can be `'binary'`,
164 `'hex'` or `'base64'`.
166 Returns true or false depending on the validity of the signature for the data and public key.