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, key)
81 Creates and returns a cipher object, with the given algorithm and key.
83 `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc.
84 On recent releases, `openssl list-cipher-algorithms` will display the available cipher algorithms.
86 ### cipher.update(data, input_encoding='binary', output_encoding='binary')
88 Updates the cipher with `data`, the encoding of which is given in `input_encoding`
89 and can be `'utf8'`, `'ascii'` or `'binary'`. The `output_encoding` specifies
90 the output format of the enciphered data, and can be `'binary'`, `'base64'` or `'hex'`.
92 Returns the enciphered contents, and can be called many times with new data as it is streamed.
94 ### cipher.final(output_encoding='binary')
96 Returns any remaining enciphered contents, with `output_encoding` being one of: `'binary'`, `'base64'` or `'hex'`.
98 ### crypto.createDecipher(algorithm, key)
100 Creates and returns a decipher object, with the given algorithm and key.
101 This is the mirror of the cipher object above.
103 ### decipher.update(data, input_encoding='binary', output_encoding='binary')
105 Updates the decipher with `data`, which is encoded in `'binary'`, `'base64'` or `'hex'`.
106 The `output_decoding` specifies in what format to return the deciphered plaintext: `'binary'`, `'ascii'` or `'utf8'`.
108 ### decipher.final(output_encoding='binary')
110 Returns any remaining plaintext which is deciphered,
111 with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
114 ### crypto.createSign(algorithm)
116 Creates and returns a signing object, with the given algorithm.
117 On recent OpenSSL releases, `openssl list-public-key-algorithms` will display
118 the available signing algorithms. Examples are `'RSA-SHA256'`.
120 ### signer.update(data)
122 Updates the signer object with data.
123 This can be called many times with new data as it is streamed.
125 ### signer.sign(private_key, output_format='binary')
127 Calculates the signature on all the updated data passed through the signer.
128 `private_key` is a string containing the PEM encoded private key for signing.
130 Returns the signature in `output_format` which can be `'binary'`, `'hex'` or `'base64'`.
132 ### crypto.createVerify(algorithm)
134 Creates and returns a verification object, with the given algorithm.
135 This is the mirror of the signing object above.
137 ### verifier.update(data)
139 Updates the verifier object with data.
140 This can be called many times with new data as it is streamed.
142 ### verifier.verify(object, signature, signature_format='binary')
144 Verifies the signed data by using the `object` and `signature`. `object` is a
145 string containing a PEM encoded object, which can be one of RSA public key,
146 DSA public key, or X.509 certificate. `signature` is the previously calculated
147 signature for the data, in the `signature_format` which can be `'binary'`,
148 `'hex'` or `'base64'`.
150 Returns true or false depending on the validity of the signature for the data and public key.
152 ### crypto.createDiffieHellman(prime_length)
154 Creates a Diffie-Hellman key exchange object and generates a prime of the
155 given bit length. The generator used is `2`.
157 ### crypto.createDiffieHellman(prime, encoding='binary')
159 Creates a Diffie-Hellman key exchange object using the supplied prime. The
160 generator used is `2`. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
162 ### diffieHellman.generateKeys(encoding='binary')
164 Generates private and public Diffie-Hellman key values, and returns the
165 public key in the specified encoding. This key should be transferred to the
166 other party. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
168 ### diffieHellman.computeSecret(other_public_key, input_encoding='binary', output_encoding=input_encoding)
170 Computes the shared secret using `other_public_key` as the other party's
171 public key and returns the computed shared secret. Supplied key is
172 interpreted using specified `input_encoding`, and secret is encoded using
173 specified `output_encoding`. Encodings can be `'binary'`, `'hex'`, or
174 `'base64'`. If no output encoding is given, the input encoding is used as
177 ### diffieHellman.getPrime(encoding='binary')
179 Returns the Diffie-Hellman prime in the specified encoding, which can be
180 `'binary'`, `'hex'`, or `'base64'`.
182 ### diffieHellman.getGenerator(encoding='binary')
184 Returns the Diffie-Hellman prime in the specified encoding, which can be
185 `'binary'`, `'hex'`, or `'base64'`.
187 ### diffieHellman.getPublicKey(encoding='binary')
189 Returns the Diffie-Hellman public key in the specified encoding, which can
190 be `'binary'`, `'hex'`, or `'base64'`.
192 ### diffieHellman.getPrivateKey(encoding='binary')
194 Returns the Diffie-Hellman private key in the specified encoding, which can
195 be `'binary'`, `'hex'`, or `'base64'`.
197 ### diffieHellman.setPublicKey(public_key, encoding='binary')
199 Sets the Diffie-Hellman public key. Key encoding can be `'binary'`, `'hex'`,
202 ### diffieHellman.setPrivateKey(public_key, encoding='binary')
204 Sets the Diffie-Hellman private key. Key encoding can be `'binary'`, `'hex'`, or `'base64'`.