Doc improvements and change argument name.
[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, password)
80
81 Creates and returns a cipher object, with the given algorithm and password.
82
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).
88
89 ### crypto.createCipheriv(algorithm, key, iv)
90
91 Creates and returns a cipher object, with the given algorithm, key and iv.
92
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).
96
97 ### cipher.update(data, input_encoding='binary', output_encoding='binary')
98
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'`.
102
103 Returns the enciphered contents, and can be called many times with new data as it is streamed.
104
105 ### cipher.final(output_encoding='binary')
106
107 Returns any remaining enciphered contents, with `output_encoding` being one of: `'binary'`, `'base64'` or `'hex'`.
108
109 ### crypto.createDecipher(algorithm, password)
110
111 Creates and returns a decipher object, with the given algorithm and key.
112 This is the mirror of the [createCipher()](#crypto.createCipher) above.
113
114 ### crypto.createDecipheriv(algorithm, key, iv)
115
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.
118
119 ### decipher.update(data, input_encoding='binary', output_encoding='binary')
120
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'`.
123
124 ### decipher.final(output_encoding='binary')
125
126 Returns any remaining plaintext which is deciphered,
127 with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
128
129
130 ### crypto.createSign(algorithm)
131
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'`.
135
136 ### signer.update(data)
137
138 Updates the signer object with data.
139 This can be called many times with new data as it is streamed.
140
141 ### signer.sign(private_key, output_format='binary')
142
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.
145
146 Returns the signature in `output_format` which can be `'binary'`, `'hex'` or `'base64'`.
147
148 ### crypto.createVerify(algorithm)
149
150 Creates and returns a verification object, with the given algorithm.
151 This is the mirror of the signing object above.
152
153 ### verifier.update(data)
154
155 Updates the verifier object with data.
156 This can be called many times with new data as it is streamed.
157
158 ### verifier.verify(object, signature, signature_format='binary')
159
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'`.
165
166 Returns true or false depending on the validity of the signature for the data and public key.