Open In App
Related Articles

Node.js Crypto Complete Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
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() MethodThis is used to return the buffer containing the value of cipher object.
Node.js cipher.update() MethodThis is used to update the cipher with data according to the given encoding format.
Node.js crypto.getCiphers() MethodThe crypto.getCiphers() method returns an array the names of all the supported cipher algorithms.
Node.js crypto.createECDH() MethodThis  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() MethodThis 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() MethodThis  is used to create a Cipher object, with the stated algorithm, key and initialization vector
Node.js crypto.getDiffieHellman() MethodThis is used to create a predefined DiffieHellmanGroup key exchange object.
Node.js crypto.pbkdf2() MethodThe crypto.pbkdf2() method gives an asynchronous Password-Based Key Derivation Function 2 i.e. (PBKDF2) implementation
Node.js crypto.createHash() MethodThis is used to create a Hash object that can be used to create hash digests by using the stated algorithm. 
Node.js crypto.createHmac() MethodThe crypto.createHmac() method is used to create an Hmac object that uses the stated ‘algorithm’ and ‘key’.
Node.js crypto.randomBytes() MethodThis 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() MethodThis 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() MethodThe crypto.pbkdf2Sync() method gives a synchronous Password-Based Key Derivation Function 2 i.e, (PBKDF2) implementation.
Node.js crypto.getCurves() MethodThis 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() MethodThis 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() MethodThis 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() MethodThe crypto.createVerify() method is used to create a Verify object that uses the stated algorithm.
Node.js crypto.createDiffieHellmanGroup() MethodThe 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) MethodThis 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() MethodThe 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) MethodThe 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() MethodThis  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() MethodThis 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() MethodThis is an inbuilt application programming interface of crypto module which is used to return the object passed as buffer argument.
Node.js crypto.randomFill() MethodThe 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() MethodThe 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() MethodThe crypto.privateEncrypt() method is used to encrypt the stated content of the buffer with the parameter ‘privateKey’.
Node.js script.createCachedData() MethodThis is used to create a code cache that can be used with the cachedData option of the script constructor.
Node.js decipher.update() MethodThis 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