Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Node.js crypto.checkPrime() Function

  • Last Updated : 30 Jul, 2021

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 buffer representing a sequence of big endian octets of arbitrary length.
  • option: Any other option which will alter the operation of this API.
  • callback: It is the callback function that is executed and is passed as an optional parameter.

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

Example 1:



index.js




// Node.js program to demonstrate the  
// crypto.checkPrime() api
  
// Importing crypto module
const crypto = require('crypto')
  
// Creating and ini
tializing new 
// ArrayBuffer object
const buffer = new ArrayBuffer(8)
  
// Checking if the buffer object is prime or not
// by using checkPrime() method
crypto.checkPrime(buffer, (err, val) => {
  
    // Checking if any error is found
    if (err) throw new Error('Uh oh!');
  
    // 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.checkPrime() api
  
// Importing crypto module
const crypto = require('crypto')
  
// Creating and initializing new 
// ArrayBuffer object
const buffer = BigInt("0o377777777777777777")
  
// Checking if the buffer object is prime or not
// by using checkPrime() method
crypto.checkPrime(buffer, (err, val) => {
  
    // Checking if any error is found
    if (err) throw new Error('Uh oh!');
  
    // 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_checkprime_candidate_options_callback




My Personal Notes arrow_drop_up
Recommended Articles
Page :