Merge branch 'v0.4'
[platform/upstream/nodejs.git] / doc / api / crypto.markdown
1 ## Crypto
2
3 Use `require('crypto')` to access this module.
4
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.
8
9 It also offers a set of wrappers for OpenSSL's hash, hmac, cipher, decipher, sign and verify methods.
10
11 ### crypto.createCredentials(details)
12
13 Creates a credentials object, with the optional details being a dictionary with keys:
14
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.
18
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>.
21
22
23 ### crypto.createHash(algorithm)
24
25 Creates and returns a hash object, a cryptographic hash with the given algorithm
26 which can be used to generate hash digests.
27
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.
31
32 Example: this program that takes the sha1 sum of a file
33
34     var filename = process.argv[2];
35     var crypto = require('crypto');
36     var fs = require('fs');
37
38     var shasum = crypto.createHash('sha1');
39
40     var s = fs.ReadStream(filename);
41     s.on('data', function(d) {
42       shasum.update(d);
43     });
44
45     s.on('end', function() {
46       var d = shasum.digest('hex');
47       console.log(d + '  ' + filename);
48     });
49
50 ### hash.update(data)
51
52 Updates the hash content with the given `data`.
53 This can be called many times with new data as it is streamed.
54
55 ### hash.digest(encoding='binary')
56
57 Calculates the digest of all of the passed data to be hashed.
58 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
59
60 Note: `hash` object can not be used after `digest()` method been called.
61
62
63 ### crypto.createHmac(algorithm, key)
64
65 Creates and returns a hmac object, a cryptographic hmac with the given algorithm and key.
66
67 `algorithm` is dependent on the available algorithms supported by OpenSSL - see createHash above.
68 `key` is the hmac key to be used.
69
70 ### hmac.update(data)
71
72 Update the hmac content with the given `data`.
73 This can be called many times with new data as it is streamed.
74
75 ### hmac.digest(encoding='binary')
76
77 Calculates the digest of all of the passed data to the hmac.
78 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
79
80 Note: `hmac` object can not be used after `digest()` method been called.
81
82
83 ### crypto.createCipher(algorithm, password)
84
85 Creates and returns a cipher object, with the given algorithm and password.
86
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).
92
93 ### crypto.createCipheriv(algorithm, key, iv)
94
95 Creates and returns a cipher object, with the given algorithm, key and iv.
96
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).
100
101 ### cipher.update(data, input_encoding='binary', output_encoding='binary')
102
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'`.
106
107 Returns the enciphered contents, and can be called many times with new data as it is streamed.
108
109 ### cipher.final(output_encoding='binary')
110
111 Returns any remaining enciphered contents, with `output_encoding` being one of: `'binary'`, `'base64'` or `'hex'`.
112
113 Note: `cipher` object can not be used after `final()` method been called.
114
115
116 ### crypto.createDecipher(algorithm, password)
117
118 Creates and returns a decipher object, with the given algorithm and key.
119 This is the mirror of the [createCipher()](#crypto.createCipher) above.
120
121 ### crypto.createDecipheriv(algorithm, key, iv)
122
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.
125
126 ### decipher.update(data, input_encoding='binary', output_encoding='binary')
127
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'`.
130
131 ### decipher.final(output_encoding='binary')
132
133 Returns any remaining plaintext which is deciphered,
134 with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
135
136 Note: `decipher` object can not be used after `final()` method been called.
137
138
139 ### crypto.createSign(algorithm)
140
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'`.
144
145 ### signer.update(data)
146
147 Updates the signer object with data.
148 This can be called many times with new data as it is streamed.
149
150 ### signer.sign(private_key, output_format='binary')
151
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.
154
155 Returns the signature in `output_format` which can be `'binary'`, `'hex'` or `'base64'`.
156
157 Note: `signer` object can not be used after `sign()` method been called.
158
159
160 ### crypto.createVerify(algorithm)
161
162 Creates and returns a verification object, with the given algorithm.
163 This is the mirror of the signing object above.
164
165 ### verifier.update(data)
166
167 Updates the verifier object with data.
168 This can be called many times with new data as it is streamed.
169
170 ### verifier.verify(object, signature, signature_format='binary')
171
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'`.
177
178 Returns true or false depending on the validity of the signature for the data and public key.
179
180 Note: `verifier` object can not be used after `verify()` method been called.
181
182 ### crypto.createDiffieHellman(prime_length)
183
184 Creates a Diffie-Hellman key exchange object and generates a prime of the
185 given bit length. The generator used is `2`.
186
187 ### crypto.createDiffieHellman(prime, encoding='binary')
188
189 Creates a Diffie-Hellman key exchange object using the supplied prime. The
190 generator used is `2`. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
191
192 ### diffieHellman.generateKeys(encoding='binary')
193
194 Generates private and public Diffie-Hellman key values, and returns the
195 public key in the specified encoding. This key should be transferred to the
196 other party. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
197
198 ### diffieHellman.computeSecret(other_public_key, input_encoding='binary', output_encoding=input_encoding)
199
200 Computes the shared secret using `other_public_key` as the other party's
201 public key and returns the computed shared secret. Supplied key is
202 interpreted using specified `input_encoding`, and secret is encoded using
203 specified `output_encoding`. Encodings can be `'binary'`, `'hex'`, or
204 `'base64'`. If no output encoding is given, the input encoding is used as
205 output encoding.
206
207 ### diffieHellman.getPrime(encoding='binary')
208
209 Returns the Diffie-Hellman prime in the specified encoding, which can be
210 `'binary'`, `'hex'`, or `'base64'`.
211
212 ### diffieHellman.getGenerator(encoding='binary')
213
214 Returns the Diffie-Hellman prime in the specified encoding, which can be
215 `'binary'`, `'hex'`, or `'base64'`.
216
217 ### diffieHellman.getPublicKey(encoding='binary')
218
219 Returns the Diffie-Hellman public key in the specified encoding, which can
220 be `'binary'`, `'hex'`, or `'base64'`.
221
222 ### diffieHellman.getPrivateKey(encoding='binary')
223
224 Returns the Diffie-Hellman private key in the specified encoding, which can
225 be `'binary'`, `'hex'`, or `'base64'`.
226
227 ### diffieHellman.setPublicKey(public_key, encoding='binary')
228
229 Sets the Diffie-Hellman public key. Key encoding can be `'binary'`, `'hex'`,
230 or `'base64'`.
231
232 ### diffieHellman.setPrivateKey(public_key, encoding='binary')
233
234 Sets the Diffie-Hellman private key. Key encoding can be `'binary'`, `'hex'`, or `'base64'`.
235