Node.js cipher.setAutoPadding() Method Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss about the setAutoPadding() method in the cipher class of the crypto module in Node JS. This method is used to automatically add padding to the input data of the appropriate size. To disable the padding, one can use cipher.setAutoPadding() with the false parameter. Note: This method must be called before the final method. Syntax: cipher.setAutoPadding( autoPadding ) Parameter: This method accepts a single parameter as mentioned above and discussed below: autoPadding: It is used to specify if automatic padding has to be used. Example 1: JavaScript const crypto = require('crypto'); // Creating and initializing algorithm and password const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // Getting key for cipher object crypto.scrypt(password, 'salt', 24, { N: 512 }, (err, key) => { if (err) throw err; // Creating and initializing the static iv const iv = Buffer.alloc(16, 0); // Creating and initializing the cipher object const cipher = crypto.createCipheriv(algorithm, key, iv); // Getting buffer value // by using final() method // Using the cipher.setAutoPadding() method // with the false parameter console.log(cipher.setAutoPadding(false)); let value = cipher.final('hex'); }); Output: Cipheriv { _decoder: null, _options: undefined, [Symbol(kHandle)]: CipherBase {} } Example 2: In this example, we called the setAutoPadding method after the final method. If you call this after the final method we got an error. JavaScript const crypto = require('crypto'); // Creating and initializing algorithm and password const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // Getting key for cipher object crypto.scrypt(password, 'salt', 24, { N: 512 }, (err, key) => { if (err) throw err; // Creating and initializing the static iv const iv = Buffer.alloc(16, 0); // Creating and initializing the cipher object const cipher = crypto.createCipheriv(algorithm, key, iv); // Getting buffer value // by using final() method // Calling the setAutoPadding() method // after the final method let value = cipher.final('hex'); console.log(cipher.setAutoPadding(false)); }); Output: throw new ERR_CRYPTO_INVALID_STATE('setAutoPadding'); Reference: https://nodejs.org/api/crypto.html#ciphersetautopadding Comment More infoAdvertise with us Next Article Node.js cipher.setAutoPadding() Method N neeraj3304 Follow Improve Article Tags : Web Technologies Node.js Node.js-crypto-module Similar Reads Node.js cipher.setAAD() Method The cipher.setAAD() method is used in Node.js to set the additional authenticated data (AAD) for an encrypt/decrypt stream. The AAD is a chunk of data that is authenticated but not encrypted. It is useful for sending data alongside an encrypted message that needs to be authenticated but does not nee 2 min read Node.js decipher.update() Method The decipher.update() method is an inbuilt application programming interface of class Decipher within crypto module which is used to update the decipher with the data according to the given encoding format. Syntax: const decipher.update(data[, inputEncoding][, outputEncoding]) Parameters: This metho 2 min read Node.js tls.getCiphers() Method The tls.getCiphers() is an inbuilt application programming interface of class TLS within tls module which is used to return the array of the supported TLS ciphers. Syntax: const tls.getCiphers() Parameters: This method does not accept any parameter. Return Value: This method returns the array of the 3 min read Node.js tlsSocket.getCipher() Method The tlsSocket.getCipher() method is an inbuilt application programming interface of class TLSSocket within tls module which is used to return objects containing information on the negotiated cipher suite. Syntax: const tlsSocket.getCipher() Parameters: This method does not accept any parameter. Retu 3 min read Node.js Buffer.toString() Method The Buffer.toString() method is used to decode a buffer data to string according to the specified encoding type. The start and end offset is used to decode only particular subset of a buffer. If the byte sequence in the buffer data is not valid according to the provided encoding, then it is replaced 2 min read Like