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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads