Node.js crypto.setFips()函数
crypto.setFips()方法是加密模块中 Crypto 类的内置应用程序编程接口,用于在启用 FIPS 的 Node.js 构建中启用符合 FIPS 的加密提供程序。
句法:
const crypto.setFips(bool)
参数:此 API 将布尔值作为参数。
返回值:此 API 不返回任何内容。
示例 1:
index.js
// Node.js program to demonstrate the
// crypto.setFips() method
// Importing crypto module
const crypto = require('crypto')
// Checking for error
try {
// Enabling the FIPS compliant
// crypto provider by using
// setFips() method
const val = crypto.setFips(true);
// Display the result
console.log("FIPS compliant crypto"
+ " provider has been enabled")
} catch (e) {
// Display error if any
console.log("FIPS mode is not available.");
}
index.js
// Node.js program to demonstrate the
// crypto.setFips() method
// Importing crypto module
const crypto = require('crypto')
// Enabling the FIPS compliant
// crypto provider by using
// setFips() method
try {
// Function call
const val = crypto.setFips(false);
// Display the result
console.log("FIPS compliant crypto "
+ "provider has been disabled");
} catch (e) {
console.log("ERR_CRYPTO_FIPS_UNAVAILABLE")
}
使用以下命令运行index.js文件:
node index.js
输出:
FIPS mode is not available.
示例 2:
index.js
// Node.js program to demonstrate the
// crypto.setFips() method
// Importing crypto module
const crypto = require('crypto')
// Enabling the FIPS compliant
// crypto provider by using
// setFips() method
try {
// Function call
const val = crypto.setFips(false);
// Display the result
console.log("FIPS compliant crypto "
+ "provider has been disabled");
} catch (e) {
console.log("ERR_CRYPTO_FIPS_UNAVAILABLE")
}
输出:
ERR_CRYPTO_FIPS_UNAVAILABLE
参考: https://nodejs.org/dist/latest-v15.x/docs/api/crypto.html#crypto_crypto_setfips_bool