📜  Node.js crypto.sign()函数

📅  最后修改于: 2022-05-13 01:56:46.824000             🧑  作者: Mango

Node.js crypto.sign()函数

密码学是将纯文本转换为不可读的过程,该过程文本散列而来,反之亦然, crypto.sign()用于创建数据签名。

句法:

crypto.sign(algorithm, data, key)

参数:此函数接受以下参数:

  • 算法:它是一个字符串类型的值。可以通过应用签名算法的名称(如“ SHA256 ”)代替摘要算法来创建签名。
  • data:它应该是缓冲区、TypedArray 或 DataView的对象。读取 Buffer.from() 方法将字符串转换为 Buffer。
  • key:应该是keyObjectprivateKey 。如果您没有任何私钥,则可以使用 crypto.generateKeyPairSync() 方法创建私钥和公钥。

模块安装:使用以下命令安装所需模块:

npm install crypto

返回值:根据指定的算法、数据和密钥返回签名值。返回值默认为 Buffer,但可以使用 buffer.toString() 方法转换为其他格式。

示例 1:签署一个字符串。

signature.js
const crypto = require('crypto');
const buffer = require('buffer');
  
// Create a private key
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
});
  
// Convert string to buffer 
const data = Buffer.from("I Love GeeksForGeeks");
  
// Sign the data and returned signature in buffer 
const sign = crypto.sign("SHA256", data , privateKey);
  
// Convert returned buffer to base64
const signature = sign.toString('base64');
  
// Printing the signature 
console.log(`Signature:\n\n ${signature}`);


signature.js
const crypto = require('crypto');
const buffer = require('buffer');
const fs = require('fs');
  
// Create a private key
const { privateKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
});
  
console.log("Reading File...\n");
// Reading file
const text = fs.readFileSync('./doc.txt');
console.log(`File content: ${text}`);
  
// Convert string to buffer 
const data = Buffer.from(text);
  
// Sign the data and returned signature in buffer 
const sign = crypto.sign("SHA256", data , privateKey);
  
// Convert returned buffer to base64
const signature = sign.toString('base64');
  
// Printing the signature 
console.log(`Signature:\n\n ${signature}`);


signature.js
const crypto = require('crypto');
const buffer = require('buffer');
  
// Create a private key
const { privateKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
});
  
// JSON object
const person = {
    name: "Raktim Banerjee",
    email:"example@gmail.com",
    address: "4 main street"
}
  
// Convert Stringified json data to buffer  
const data = Buffer.from( JSON.stringify(person) );
  
// Sign the data and returned signature in buffer 
const sign = crypto.sign("SHA256", data , privateKey);
  
// Convert returned buffer to base64
const signature = sign.toString('base64');
  
// Printing the signature 
console.log(`Signature:\n\n ${signature}`);


使用以下命令运行signature.js文件:

node signature.js

输出:

示例 2:签署文件。

签名.js

const crypto = require('crypto');
const buffer = require('buffer');
const fs = require('fs');
  
// Create a private key
const { privateKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
});
  
console.log("Reading File...\n");
// Reading file
const text = fs.readFileSync('./doc.txt');
console.log(`File content: ${text}`);
  
// Convert string to buffer 
const data = Buffer.from(text);
  
// Sign the data and returned signature in buffer 
const sign = crypto.sign("SHA256", data , privateKey);
  
// Convert returned buffer to base64
const signature = sign.toString('base64');
  
// Printing the signature 
console.log(`Signature:\n\n ${signature}`);

使用以下命令运行signature.js文件:

node signature.js

输出:

示例 3:签署 JSON 数据。

签名.js

const crypto = require('crypto');
const buffer = require('buffer');
  
// Create a private key
const { privateKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
});
  
// JSON object
const person = {
    name: "Raktim Banerjee",
    email:"example@gmail.com",
    address: "4 main street"
}
  
// Convert Stringified json data to buffer  
const data = Buffer.from( JSON.stringify(person) );
  
// Sign the data and returned signature in buffer 
const sign = crypto.sign("SHA256", data , privateKey);
  
// Convert returned buffer to base64
const signature = sign.toString('base64');
  
// Printing the signature 
console.log(`Signature:\n\n ${signature}`);

使用以下命令运行signature.js文件:

node signature.js

输出:

参考: https://nodejs.org/api/crypto.html#crypto_crypto_sign_algorithm_data_key_callback