fix verifier.verify() docs
[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 ### hash.update(data)
33
34 Updates the hash content with the given `data`.
35 This can be called many times with new data as it is streamed.
36
37 ### hash.digest(encoding='binary')
38
39 Calculates the digest of all of the passed data to be hashed.
40 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
41
42
43 ### crypto.createHmac(algorithm, key)
44
45 Creates and returns a hmac object, a cryptographic hmac with the given algorithm and key.
46
47 `algorithm` is dependent on the available algorithms supported by OpenSSL - see createHash above.
48 `key` is the hmac key to be used.
49
50 ### hmac.update(data)
51
52 Update the hmac content with the given `data`.
53 This can be called many times with new data as it is streamed.
54
55 ### hmac.digest(encoding='binary')
56
57 Calculates the digest of all of the passed data to the hmac.
58 The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
59
60
61 ### crypto.createCipher(algorithm, key)
62
63 Creates and returns a cipher object, with the given algorithm and key.
64
65 `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc.
66 On recent releases, `openssl list-cipher-algorithms` will display the available cipher algorithms.
67
68 ### cipher.update(data, input_encoding='binary', output_encoding='binary')
69
70 Updates the cipher with `data`, the encoding of which is given in `input_encoding`
71 and can be `'utf8'`, `'ascii'` or `'binary'`. The `output_encoding` specifies
72 the output format of the enciphered data, and can be `'binary'`, `'base64'` or `'hex'`.
73
74 Returns the enciphered contents, and can be called many times with new data as it is streamed.
75
76 ### cipher.final(output_encoding='binary')
77
78 Returns any remaining enciphered contents, with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
79
80 ### crypto.createDecipher(algorithm, key)
81
82 Creates and returns a decipher object, with the given algorithm and key.
83 This is the mirror of the cipher object above.
84
85 ### decipher.update(data, input_encoding='binary', output_encoding='binary')
86
87 Updates the decipher with `data`, which is encoded in `'binary'`, `'base64'` or `'hex'`.
88 The `output_decoding` specifies in what format to return the deciphered plaintext: `'binary'`, `'ascii'` or `'utf8'`.
89
90 ### decipher.final(output_encoding='binary')
91
92 Returns any remaining plaintext which is deciphered,
93 with `output_encoding' being one of: `'binary'`, `'ascii'` or `'utf8'`.
94
95
96 ### crypto.createSign(algorithm)
97
98 Creates and returns a signing object, with the given algorithm.
99 On recent OpenSSL releases, `openssl list-public-key-algorithms` will display
100 the available signing algorithms. Examples are `'RSA-SHA256'`.
101
102 ### signer.update(data)
103
104 Updates the signer object with data.
105 This can be called many times with new data as it is streamed.
106
107 ### signer.sign(private_key, output_format='binary')
108
109 Calculates the signature on all the updated data passed through the signer.
110 `private_key` is a string containing the PEM encoded private key for signing.
111
112 Returns the signature in `output_format` which can be `'binary'`, `'hex'` or `'base64'`.
113
114 ### crypto.createVerify(algorithm)
115
116 Creates and returns a verification object, with the given algorithm.
117 This is the mirror of the signing object above.
118
119 ### verifier.update(data)
120
121 Updates the verifier object with data.
122 This can be called many times with new data as it is streamed.
123
124 ### verifier.verify(cert, signature, signature_format='binary')
125
126 Verifies the signed data by using the `cert` which is a string containing
127 the PEM encoded public key, and `signature`, which is the previously calculates
128 signature for the data, in the `signature_format` which can be `'binary'`, `'hex'` or `'base64'`.
129
130 Returns true or false depending on the validity of the signature for the data and public key.