Open In App

Node.js crypto.checkPrimeSync() Function

Last Updated : 20 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The checkPrimeSync() is  an inbuilt application programming interface of class Crypto within crypto module which is used to check if the passed buffer object is prime or not.

Syntax:

const crypto.checkPrimeSync(candidate[, options])

Parameters: This function takes the following arguments as the parameter.

  • candidate: This is an object of  buffer representing a sequence of big endian octets of arbitrary length.
  • option: The option which will alter the operation of this function.

Return Value: This function returns true if and only if the candidate is a prime.

Example 1:

index.js




// Node.js program to demonstrate the  
// crypto.checkPrimeSync() function
  
// Importing crypto module
const crypto = require('crypto')
  
// creating and initializing new 
// ArrayBuffer object
const buffer = new ArrayBuffer(8)
  
// checking if the buffer object is prime or not
// by using checkPrimeSync() method
const val = crypto.checkPrimeSync(buffer)
  
//display the result
if(val)
console.log("candidate is a prime")
else
console.log("candidate is not a prime")


Run the index.js file using the following command:

node index.js

Output:

candidate is not a prime

Example 2:

index.js




// Node.js program to demonstrate the  
// crypto.checkPrimeSync() function
  
// Importing crypto module
const crypto = require('crypto')
  
// creating and initializing new 
// BigInt object
const buffer = BigInt("0o377777777777777777")
  
// checking if the buffer object is prime or not
// by using checkPrimeSync() method
const val = crypto.checkPrimeSync(buffer)
  
//display the result
if(val)
console.log("candidate is a prime")
else
console.log("candidate is not a prime")


Run the index.js file using the following command:

node index.js

Output:

candidate is not a prime

Reference: https://nodejs.org/dist/latest-v15.x/docs/api/crypto.html#crypto_crypto_checkprimesync_candidate_options



Similar Reads

Node.js crypto.scryptSync( ) Function
The crypto.scryptSync() is an inbuilt function which Provides a synchronous scrypt implementation in Node.js. scrypt is a password-based key derivation function. It is intended to be costly computationally plus memory-wise. So, the brute-force attacks are made unsuccessful. Syntax: crypto.scryptSync(password, salt, keylen[, options]) Parameter: Thi
3 min read
Node.js crypto.sign() Function
Cryptography is the process of converting plain text into unreadable which is hashed from text and vice-versa and the crypto.sign() is used to create signature of data. Syntax: crypto.sign(algorithm, data, key)Parameters: This function accepts the following parameters: algorithm: It is a string-type value. A signature can be created by applying the
3 min read
Node.js crypto.verify() Function
The crypto.verify() is a method of the inbuilt module of node.js crypto that is used to verify the signature of data that is hashed using different kinds of hashing functions Like SHA256 algorithm etc. Syntax: crypto.verify(algorithm, data, publicKey, signature) Parameters: algorithm: It is a string-type value. A signature can be verified by applyi
2 min read
Node.js crypto.hkdf() Function
The crypto.hkdf() is an inbuilt application programming interface of class Crypto within crypto module which is used to derive a key of the particular length. Syntax: const crypto.hkdf(digest, key, salt, info, keylen, callback) Parameters: This function takes the following arguments as the parameter. digest: It's a string of digest algorithm.key: I
2 min read
Node.js crypto.checkPrime() Function
The crypto.checkPrime() is an inbuilt application programming interface of class Crypto within the crypto module which is used to check if the passed buffer object is prime or not. Syntax: const crypto.checkPrime(candidate[, options, [callback]])Parameters: This API takes the following arguments as the parameter. candidate: It is an object of buffe
2 min read
Node.js crypto.randomUUID( ) Function
The crypto.randomUUID() is an inbuilt application programming interface of class Crypto within crypto module which is used to generate a random RFC 4122 Version 4 UUID. Syntax: const crypto.randomUUID([options]) Parameters: This function takes the disableEntropyCache as a parameter Return Value: This function returns a random RFC 4122 Version 4 UUI
1 min read
Node.js crypto.setFips() Function
The crypto.setFips() method is an inbuilt application programming interface of class Crypto within the crypto module which is used to enable the FIPS compliant crypto provider in a FIPS-enabled Node.js build. Syntax: const crypto.setFips(bool)Parameters: This API takes the Boolean value as a parameter. Return Value: This API does not return anythin
2 min read
Node.js crypto.timingSafeEqual() Function
Node.js is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of a web browser. Node.js allows developers to utilize JavaScript to create command-line tools and server-side scripting, which involves running scripts on the server before sending the page to the user's brows
2 min read
Node.js crypto.setEngine() Function
Node.js is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of a web browser. Node.js allows developers to utilize JavaScript to create command-line tools and server-side scripting, which involves running scripts on the server before sending the page to the user's brows
2 min read
Node JS | Password Hashing with Crypto module
In real-life applications with User authentication functionality, storing the user passwords as the original string in the database is not practical. Still, it is good practice to hash the password and then store them in the database. Crypto module for Node JS helps developers to hash user passwords. Examples: Original Password : portalforgeeks Has
5 min read