Node.js crypto.checkPrimeSync()函数
checkPrimeSync()是加密模块中 Crypto 类的内置应用程序编程接口,用于检查传递的缓冲区对象是否为素数。
句法:
const crypto.checkPrimeSync(candidate[, options])
参数:此函数将以下参数作为参数。
- 候选:这是一个缓冲区对象,表示任意长度的大端字节序列。
- 选项:将改变此函数操作的选项。
返回值:当且仅当候选是素数时,此函数才返回 true。
示例 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")
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")
使用以下命令运行 index.js 文件:
node index.js
输出:
candidate is not a prime
示例 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")
使用以下命令运行 index.js 文件:
node index.js
输出:
candidate is not a prime
参考:https://nodejs.org/dist/latest-v15.x/docs/api/crypto.html#crypto_crypto_checkprimesync_candidate_options