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
61 ### crypto.createHmac(algorithm, key)
62
63 Creates and returns a hmac object, a cryptographic hmac with the given algorithm and key.
64
65 `algorithm` is dependent on the available algorithms supported by OpenSSL - see createHash above.
66 `key` is the hmac key to be used.
67
68 ### hmac.update(data)
69
70 Update the hmac content with the given `data`.
71 This can be called many times with new data as it is streamed.
72
73 ### hmac.digest(encoding='binary')
74
75 Calculates the digest of all of the passed data to the hmac.
76 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
77
78
79 ### crypto.createCipher(algorithm, key)
80
81 Creates and returns a cipher object, with the given algorithm and key.
82
83 `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc.
84 On recent releases, `openssl list-cipher-algorithms` will display the available cipher algorithms.
85
86 ### cipher.update(data, input_encoding='binary', output_encoding='binary')
87
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'`.
91
92 Returns the enciphered contents, and can be called many times with new data as it is streamed.
93
94 ### cipher.final(output_encoding='binary')
95
96 Returns any remaining enciphered contents, with `output_encoding` being one of: `'binary'`, `'base64'` or `'hex'`.
97
98 ### crypto.createDecipher(algorithm, key)
99
100 Creates and returns a decipher object, with the given algorithm and key.
101 This is the mirror of the cipher object above.
102
103 ### decipher.update(data, input_encoding='binary', output_encoding='binary')
104
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'`.
107
108 ### decipher.final(output_encoding='binary')
109
110 Returns any remaining plaintext which is deciphered,
111 with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
112
113
114 ### crypto.createSign(algorithm)
115
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'`.
119
120 ### signer.update(data)
121
122 Updates the signer object with data.
123 This can be called many times with new data as it is streamed.
124
125 ### signer.sign(private_key, output_format='binary')
126
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.
129
130 Returns the signature in `output_format` which can be `'binary'`, `'hex'` or `'base64'`.
131
132 ### crypto.createVerify(algorithm)
133
134 Creates and returns a verification object, with the given algorithm.
135 This is the mirror of the signing object above.
136
137 ### verifier.update(data)
138
139 Updates the verifier object with data.
140 This can be called many times with new data as it is streamed.
141
142 ### verifier.verify(object, signature, signature_format='binary')
143
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'`.
149
150 Returns true or false depending on the validity of the signature for the data and public key.
151
152 ### crypto.createDiffieHellman(prime_length)
153
154 Creates a Diffie-Hellman key exchange object and generates a prime of the
155 given bit length. The generator used is `2`.
156
157 ### crypto.createDiffieHellman(prime, encoding='binary')
158
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'`.
161
162 ### diffieHellman.generateKeys(encoding='binary')
163
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'`.
167
168 ### diffieHellman.computeSecret(other_public_key, input_encoding='binary', output_encoding=input_encoding)
169
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
175 output encoding.
176
177 ### diffieHellman.getPrime(encoding='binary')
178
179 Returns the Diffie-Hellman prime in the specified encoding, which can be
180 `'binary'`, `'hex'`, or `'base64'`.
181
182 ### diffieHellman.getGenerator(encoding='binary')
183
184 Returns the Diffie-Hellman prime in the specified encoding, which can be
185 `'binary'`, `'hex'`, or `'base64'`.
186
187 ### diffieHellman.getPublicKey(encoding='binary')
188
189 Returns the Diffie-Hellman public key in the specified encoding, which can
190 be `'binary'`, `'hex'`, or `'base64'`.
191
192 ### diffieHellman.getPrivateKey(encoding='binary')
193
194 Returns the Diffie-Hellman private key in the specified encoding, which can
195 be `'binary'`, `'hex'`, or `'base64'`.
196
197 ### diffieHellman.setPublicKey(public_key, encoding='binary')
198
199 Sets the Diffie-Hellman public key. Key encoding can be `'binary'`, `'hex'`,
200 or `'base64'`.
201
202 ### diffieHellman.setPrivateKey(public_key, encoding='binary')
203
204 Sets the Diffie-Hellman private key. Key encoding can be `'binary'`, `'hex'`, or `'base64'`.
205