Open In App

Node.js Crypto Complete Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Node.js crypto module handles cryptographic functionality. Cryptography is an important aspect when we deal with network security. ‘Crypto’ means secret or hidden. Cryptography is the science of secret writing with the intention of keeping the data secret.

Example:

Javascript




<script>
    // Node.js program to demonstrate the
    // cipher.final() method
      
    // Importing crypto module
    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 the cipher object
    const key = crypto.scryptSync(password, 'salt', 24);
      
    // 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
    let value = cipher.final('hex');
      
    // Display the result
    console.log("buffer :- " + value);
</script>


Output:

buffer :- b9be42878310d599e4e49e040d1badb9

The Complete List of crypto module are listed below:

Node.js Crypto

Description
Node.js cipher.final() Method This is used to return the buffer containing the value of cipher object.
Node.js cipher.update() Method This is used to update the cipher with data according to the given encoding format.
Node.js crypto.getCiphers() Method The crypto.getCiphers() method returns an array the names of all the supported cipher algorithms.
Node.js crypto.createECDH() Method This  is an inbuilt application programming interface of crypto module which is used to create a Decipher object, with the stated algorithm, key and initialization vector 
Node.js crypto.createDecipheriv() Method This is an inbuilt application programming interface of crypto module which is used to create a Decipher object, with the stated algorithm, key and initialization vector 
Node.js crypto.createCipheriv() Method This  is used to create a Cipher object, with the stated algorithm, key and initialization vector
Node.js crypto.getDiffieHellman() Method This is used to create a predefined DiffieHellmanGroup key exchange object.
Node.js crypto.pbkdf2() Method The crypto.pbkdf2() method gives an asynchronous Password-Based Key Derivation Function 2 i.e. (PBKDF2) implementation
Node.js crypto.createHash() Method This is used to create a Hash object that can be used to create hash digests by using the stated algorithm. 
Node.js crypto.createHmac() Method The crypto.createHmac() method is used to create an Hmac object that uses the stated ‘algorithm’ and ‘key’.
Node.js crypto.randomBytes() Method This is used to generate a cryptographically well-built artificial random data and the number of bytes to be generated in the written code.
Node.js crypto.publicDecrypt() Method This is used to decrypt the content of the buffer with key which was previously encrypted using the corresponding private key, i.e. crypto.privateEncrypt() method.
Node.js crypto.pbkdf2Sync() Method The crypto.pbkdf2Sync() method gives a synchronous Password-Based Key Derivation Function 2 i.e, (PBKDF2) implementation.
Node.js crypto.getCurves() Method This is an inbuilt application programming interface of the crypto module which is used to display an array with the names of all the supported elliptic curves.
Node.js crypto.getHashes() Method This is an inbuilt application programming interface of crypto module which is used to display the names of all the supported hash algorithms in an array
Node.js crypto.createSign() Method This is used to create a Sign object that uses the stated algorithm. Moreover, you can use crypto.getHashes() method to access the names of all the available digest algorithms.
Node.js crypto.createVerify() Method The crypto.createVerify() method is used to create a Verify object that uses the stated algorithm.
Node.js crypto.createDiffieHellmanGroup() Method The crypto.createDiffieHellmanGroup() method is an inbuilt application programming interface of the crypto module which is used to create a DiffieHellmanGroup.
Node.js crypto.createDiffieHellman(primeLength, generator) Method This is used to create a DiffieHellman key exchange object. Also, creates prime of primeLength bits with the help of an optional specific numeric generator. Moreover, if the generator is not defined, then the value 2 is used.
Node.js crypto.privateDecrypt() Method The crypto.privateDecrypt() method is used to decrypt the content of the buffer with privateKey.buffer which was previously encrypted using the corresponding public key, i.e. crypto.publicEncrypt().
Node.js crypto.createDiffieHellman(prime, primeEncoding, generator, generatorEncoding) Method The crypto.createDiffieHellman() method is used to create a DiffieHellman key exchange object by using the stated prime and an optional specified generator.
Node.js crypto.generateKeyPair() Method  This is used to generate a new asymmetric key pair of the specified type.
Node.js crypto.publicEncrypt() Method This  is an inbuilt application programming interface of the crypto module which is used to encrypt the stated content of the buffer with the parameter ‘key’.
Node.js crypto.generateKeyPairSync() Method This is an inbuilt application programming interface of crypto module which is used to generate a new asymmetric key pair of the specified type
Node.js crypto.randomFillSync() Method This is an inbuilt application programming interface of crypto module which is used to return the object passed as buffer argument.
Node.js crypto.randomFill() Method The crypto.randomFill() method is same as crypto.randomBytes() method but the only difference is that here the first argument is buffer
Node.js crypto.scrypt() Method The crypto.scrypt() method is an inbuilt application programming interface of crypto module which is used to enable an implementation of an asynchronous scrypt.
Node.js crypto.privateEncrypt() Method The crypto.privateEncrypt() method is used to encrypt the stated content of the buffer with the parameter ‘privateKey’.
Node.js script.createCachedData() Method This is used to create a code cache that can be used with the cachedData option of the script constructor.
Node.js decipher.update() Method This is used to update the decipher with the data according to the given encoding format.
Node.js decipher.final() Method  This is an inbuilt application programming interface of class Decipher within crypto module which is used to return the buffer containing the value of decipher object.


Last Updated : 05 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads