Namespace: Cipher

D.crypto.Cipher

Returned by D.crypto.createCipher.

Instances of the Cipher object are used to encrypt data.
The object can be used in one of two ways:

  • As a stream that is both readable and writable, where plain unencrypted data is written to produce encrypted data on the readable side.
  • Using the cipher.update() and cipher.final() methods to produce the encrypted data.

Methods

(static) final(outputEncodingopt) → {string|Buffer}

Returns any remaining enciphered contents.
If no encoding is provided, then a Buffer is returned.

Once the cipher.final() method has been called, the Cipher object can no longer be used to encrypt data.
Attempts to call cipher.final() more than once will result in an error being thrown.

Parameters:
Name Type Attributes Description
outputEncoding binary | base64 | hex <optional>
The encoding of the return value.
Can be 'binary', 'base64' or 'hex'.
If no encoding is provided, then a buffer is returned.
Returns:
- Any remaining enciphered contents
Type
string | Buffer
Example
D.crypto.createCipher('sha1', 'key', 'initialization vector').final('binary')

(static) setAutoPadding(autoPaddingopt)

When using block encryption algorithms, the Cipher class will automatically add padding to the input data to the appropriate block size.

To disable the default padding call cipher.setAutoPadding(false).
When autoPadding is false, the length of the entire input data must be a multiple of the cipher's block size or cipher.final() will throw an error.
Disabling automatic padding is useful for non-standard padding, for instance using 0x0 instead of PKCS padding.

The cipher.setAutoPadding() method must be called before cipher.final().

Parameters:
Name Type Attributes Default Description
autoPadding bool <optional>
true The auto padding boolean flag.
Example
D.crypto.createCipher('sha1', 'key', 'initialization vector').setAutoPadding(false)

(static) update(data, inputEncodingopt, outputEncodingopt) → {string|Buffer}

Updates the cipher with data.
The cipher.update() method can be called multiple times with new data until cipher.final() is called.
Calling cipher.update() after cipher.final() will result in an error being thrown.

Input Encoding options:

  • If the inputEncoding argument is given, the data argument is a string using the specified encoding.
  • If the inputEncoding argument is not given, data must be a Buffer.
Output Encoding options:
  • If the outputEncoding is specified, a string using the specified encoding is returned.
  • If no outputEncoding is provided, a Buffer is returned.
Parameters:
Name Type Attributes Description
data string | Buffer The data object.
inputEncoding binary | ascii | utf8 <optional>
The encoding of the data.
If data is a Buffer this argument is ignored.
outputEncoding binary | base64 | hex <optional>
The encoding of the return value.
The output encoding specifies the output format of the enciphered data, and can be 'binary', 'base64' or 'hex'.
If no encoding is provided, then a buffer is returned.
Returns:
- Returns the enciphered contents, and can be called many times with new data as it is streamed.
Type
string | Buffer
Example
D.crypto.createCipher('sha1', 'key', 'initialization vector').update('data', 'binary', 'binary')