2010-10-28 14:18:16 +02:00
|
|
|
## Crypto
|
|
|
|
|
|
|
|
Use `require('crypto')` to access this module.
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
The crypto module requires OpenSSL to be available on the underlying platform.
|
|
|
|
It offers a way of encapsulating secure credentials to be used as part
|
|
|
|
of a secure HTTPS net or http connection.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
It also offers a set of wrappers for OpenSSL's hash, hmac, cipher, decipher, sign and verify methods.
|
|
|
|
|
|
|
|
### crypto.createCredentials(details)
|
|
|
|
|
|
|
|
Creates a credentials object, with the optional details being a dictionary with keys:
|
|
|
|
|
|
|
|
* `key` : a string holding the PEM encoded private key
|
|
|
|
* `cert` : a string holding the PEM encoded certificate
|
|
|
|
* `ca` : either a string or list of strings of PEM encoded CA certificates to trust.
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
If no 'ca' details are given, then node.js will use the default publicly trusted list of CAs as given in
|
|
|
|
<http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt>.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
### crypto.createHash(algorithm)
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Creates and returns a hash object, a cryptographic hash with the given algorithm
|
|
|
|
which can be used to generate hash digests.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
`algorithm` is dependent on the available algorithms supported by the version
|
|
|
|
of OpenSSL on the platform. Examples are `'sha1'`, `'md5'`, `'sha256'`, `'sha512'`, etc.
|
|
|
|
On recent releases, `openssl list-message-digest-algorithms` will display the available digest algorithms.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### hash.update(data)
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Updates the hash content with the given `data`.
|
|
|
|
This can be called many times with new data as it is streamed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### hash.digest(encoding='binary')
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Calculates the digest of all of the passed data to be hashed.
|
|
|
|
The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
### crypto.createHmac(algorithm, key)
|
|
|
|
|
|
|
|
Creates and returns a hmac object, a cryptographic hmac with the given algorithm and key.
|
|
|
|
|
|
|
|
`algorithm` is dependent on the available algorithms supported by OpenSSL - see createHash above.
|
|
|
|
`key` is the hmac key to be used.
|
|
|
|
|
|
|
|
### hmac.update(data)
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Update the hmac content with the given `data`.
|
|
|
|
This can be called many times with new data as it is streamed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### hmac.digest(encoding='binary')
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Calculates the digest of all of the passed data to the hmac.
|
|
|
|
The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
### crypto.createCipher(algorithm, key)
|
|
|
|
|
|
|
|
Creates and returns a cipher object, with the given algorithm and key.
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
`algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc.
|
|
|
|
On recent releases, `openssl list-cipher-algorithms` will display the available cipher algorithms.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### cipher.update(data, input_encoding='binary', output_encoding='binary')
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Updates the cipher with `data`, the encoding of which is given in `input_encoding`
|
|
|
|
and can be `'utf8'`, `'ascii'` or `'binary'`. The `output_encoding` specifies
|
|
|
|
the output format of the enciphered data, and can be `'binary'`, `'base64'` or `'hex'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Returns the enciphered contents, and can be called many times with new data as it is streamed.
|
|
|
|
|
|
|
|
### cipher.final(output_encoding='binary')
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Returns any remaining enciphered contents, with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### crypto.createDecipher(algorithm, key)
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Creates and returns a decipher object, with the given algorithm and key.
|
|
|
|
This is the mirror of the cipher object above.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### decipher.update(data, input_encoding='binary', output_encoding='binary')
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Updates the decipher with `data`, which is encoded in `'binary'`, `'base64'` or `'hex'`.
|
|
|
|
The `output_decoding` specifies in what format to return the deciphered plaintext: `'binary'`, `'ascii'` or `'utf8'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### decipher.final(output_encoding='binary')
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Returns any remaining plaintext which is deciphered,
|
|
|
|
with `output_encoding' being one of: `'binary'`, `'ascii'` or `'utf8'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
### crypto.createSign(algorithm)
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Creates and returns a signing object, with the given algorithm.
|
|
|
|
On recent OpenSSL releases, `openssl list-public-key-algorithms` will display
|
|
|
|
the available signing algorithms. Examples are `'RSA-SHA256'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### signer.update(data)
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Updates the signer object with data.
|
|
|
|
This can be called many times with new data as it is streamed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### signer.sign(private_key, output_format='binary')
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Calculates the signature on all the updated data passed through the signer.
|
|
|
|
`private_key` is a string containing the PEM encoded private key for signing.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Returns the signature in `output_format` which can be `'binary'`, `'hex'` or `'base64'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### crypto.createVerify(algorithm)
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Creates and returns a verification object, with the given algorithm.
|
|
|
|
This is the mirror of the signing object above.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### verifier.update(data)
|
|
|
|
|
2011-02-10 04:07:17 +01:00
|
|
|
Updates the verifier object with data.
|
2010-11-18 13:00:24 +01:00
|
|
|
This can be called many times with new data as it is streamed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-02-10 04:07:17 +01:00
|
|
|
### verifier.verify(cert, signature, signature_format='binary')
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-02-10 04:07:17 +01:00
|
|
|
Verifies the signed data by using the `cert` which is a string containing
|
2011-03-29 00:59:41 +02:00
|
|
|
the PEM encoded certificate, and `signature`, which is the previously calculates
|
2010-11-18 13:00:24 +01:00
|
|
|
signature for the data, in the `signature_format` which can be `'binary'`, `'hex'` or `'base64'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Returns true or false depending on the validity of the signature for the data and public key.
|