Computer >> Máy Tính >  >> Lập trình >> Javascript

Phương thức crypto.createSign () trong Node.js

Crypto.createSign () sẽ tạo và trả về một đối tượng dấu hiệu, sử dụng thuật toán đã truyền trong tham số. Người ta có thể sử dụng, crypto.getHashing () để lấy tên của tất cả các thuật toán thông báo có sẵn. Bạn có thể tạo phiên bản Ký bằng cách sử dụng tên của thuật toán chữ ký, chẳng hạn như 'RHA-SHA256' chỉ trong một số trường hợp, thay vì thuật toán thông báo.

Cú pháp

crypto.createSign(algorithm, [options])

Tham số

Các thông số trên được mô tả như bên dưới -

  • thuật toán - Nó lấy đầu vào cho tên thuật toán được sử dụng trong khi tạo đối tượng / thể hiện dấu hiệu.

  • tùy chọn - Đây là một tham số tùy chọn có thể được sử dụng để kiểm soát hành vi của luồng.

Ví dụ

Tạo một tệp với tên - createSign.js và sao chép đoạn mã bên dưới. Sau khi tạo tệp, sử dụng lệnh sau để chạy mã này như được hiển thị trong ví dụ bên dưới -

node createSign.js

createSign.js

// Node.js program to demonstrate the use of createSign() method

// Importing the crypto module
const crypto = require('crypto');

// Creating sign object with the input algorithm
const sign = crypto.createSign('SHA256');

// Returning the sign object
console.log(sign);

Đầu ra

C:\home\node>> node createSign.js
Sign {
   _handle: {},
   _writableState:
   WritableState {
      objectMode: false,
      highWaterMark: 16384,
      finalCalled: false,
      needDrain: false,
      ending: false,
      ended: false,
      finished: false,
      destroyed: false,
      decodeStrings: true,
      defaultEncoding: 'utf8',
      length: 0,
      writing: false,
      corked: 0,
      sync: true,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: null,
      writelen: 0,
      bufferedRequest: null,
      lastBufferedRequest: null,
      pendingcb: 0,
      prefinished: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: false,
      bufferedRequestCount: 0,
      corkedRequestsFree:
      {    next: null,
         entry: null,
         finish: [Function: bound onCorkedFinish] } },
   writable: true,
   _events: [Object: null prototype] {},
   _eventsCount: 0,
   _maxListeners: undefined }

Ví dụ

Hãy xem thêm một ví dụ.

// Node.js program to demonstrate the use of createSign() method

// Importing the crypto module
const crypto = require('crypto');

// Creating sign object with the input algorithm
const sign = crypto.createSign('SHA256');

// Returning the sign object
console.log(sign.write('Welcome to Tutorials Point'));

Đầu ra

C:\home\node>> node createSign.js
true