Hướng dẫn javascript ecdh - javascript ecdh

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọcecdh.generateKeys[] method is an inbuilt application programming interface of class ECDH within the crypto module which is used to generate private and public key values of the Elliptic Curve Diffie-Hellman [ECDH] object. It returns only the public key in the given format and encoding.

    Syntax:

    ecdh.generateKeys[ encoding, format ]

    Bàn luận This method accepts two parameters as mentioned above and described below:

    • Phương thức ECDH.GenerateKeys [] là giao diện lập trình ứng dụng sẵn có của lớp ECDH trong mô-đun tiền điện tử được sử dụng để tạo các giá trị khóa riêng và công khai của đối tượng đường cong elip Diffie-Hellman [ECDH]. Nó chỉ trả về khóa công khai trong định dạng đã cho và mã hóa.This is a string value that specifies the encoding of the return value.
    • Tham số: Phương pháp này chấp nhận hai tham số như đã đề cập ở trên và được mô tả dưới đây:It is a string that specifies the format of the keys. The value can be ‘compressed’ or ‘uncompressed’.

    Mã hóa: Đây là một giá trị chuỗi chỉ định mã hóa giá trị trả về. It returns the Elliptic Curve DiffieHellman public key in the specified encoding. When the encoding is not provided, it is returned as a Buffer, otherwise a String is returned.

    Định dạng: Đó là một chuỗi chỉ định định dạng của các khóa. Giá trị có thể được nén hoặc ’không nén.

    Giá trị trả về: Nó trả về khóa công khai đường cong hình elip khác nhau trong mã hóa được chỉ định. Khi mã hóa không được cung cấp, nó được trả về dưới dạng bộ đệm, nếu không một chuỗi được trả về.

    Các ví dụ dưới đây thể hiện phương pháp này:

    const crypto = require['crypto'];

    const geek = crypto.createECDH['secp521r1'];

    const geekAPublicKey = geek.generateKeys[];

    console.log[

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    0
    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    1
    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    2

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    3

    Ví dụ 1:

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    0
    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    6
    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    7

    Output:

    JavaScript
    Private Key of Geek A is:

    console.log[

    Các ví dụ dưới đây thể hiện phương pháp này:

    const crypto = require['crypto'];

    const crypto = require[1'secp521r1'];

    const crypto = require[4

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    0const crypto = require[6const crypto = require[7];

    console.log[

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    0
    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    1'crypto'2

    'crypto'3'secp521r1'];

    'crypto'6

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    0'crypto'8const crypto = require[7];0];1];

    console.log[

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    0];5];6

    Output:

    Ví dụ 1:
    Public Key of Geek B is: AwCz/x81YIdVtPyy5B3YKwgR3hgzOXrFZsXi3M2WFcvRL2yh0VKza0/b8Mw1Z/p4Pnl1gGO2JMUfLNI4FFvzaxb9Og==

    JavaScript //nodejs.org/api/crypto.html#crypto_ecdh_generatekeys_encoding_format


    Cải thiện bài viết

    Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọcecdh.computeSecret[] method is an inbuilt application programming interface of class ECDH within the crypto module which is used to create the shared secret using the public key of the other party. The encoding of both the input public key and the output secret key can be specified using the respective parameters.

    Bàn luận

    Syntax:

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]

    Phương thức ECDH.computesecret [] là giao diện lập trình ứng dụng sẵn có của lớp ECDH trong mô -đun tiền điện tử được sử dụng để tạo bí mật chung bằng cách sử dụng khóa công khai của bên kia. Việc mã hóa cả khóa công khai đầu vào và khóa bí mật đầu ra có thể được chỉ định bằng các tham số tương ứng. This method accepts three parameters as mentioned above and described below:

    • Lỗi ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY được ném khi khóa công khai nằm ngoài đường cong hình elip. & NBSP;It is the other party’s public key based on which the shared secret is generated.
    • Tham số: Phương pháp này chấp nhận ba tham số như đã đề cập ở trên và được mô tả dưới đây:This is a string value that specifies the encoding of the other party’s public key. When this parameter is not specified, the key is expected as a Buffer TypedArray, or DataView.
    • OtherPublicKey: Đây là khóa công khai của bên khác dựa trên đó bí mật được chia sẻ được tạo ra.This is a string value that specifies the encoding of the shared secret that will be generated.

    InputEncoding: Đây là một giá trị chuỗi chỉ định mã hóa của khóa công khai bên kia. Khi tham số này không được chỉ định, khóa được dự kiến ​​là một bộ đệm đánh máy hoặc Dataview. It returns the Elliptic Curve DiffieHellman shared secret key in the specified encoding. When the encoding is not provided, it is returned as a Buffer, otherwise a string is returned.

    OutputEncoding: Đây là một giá trị chuỗi chỉ định mã hóa bí mật được chia sẻ sẽ được tạo.

    Giá trị trả về: Nó trả về khóa bí mật được chia sẻ đường cong hình elip trong mã mã hóa được chỉ định. Khi mã hóa không được cung cấp, nó được trả về dưới dạng bộ đệm, nếu không một chuỗi được trả về.In this example, the shared secrets of both users are created using the keys of both parties and then they are compared to see if they are equal.

    JavaScript

    const crypto = require['crypto'];

    const crypto = require[1'secp521r1'];

    const geek = crypto.createECDH[3const crypto = require[7];

    ____10console.log[6console.log[7];0const crypto = require[7__

    const geek = crypto.createECDH[9const crypto = require[7];

    'secp521r1'2'secp521r1'3'secp521r1'4'secp521r1'5

    'secp521r1'6'secp521r1'4'secp521r1'3'secp521r1'9

    console.log[];1];2

    console.log[];4];5

    ];6

    ];7];8 ];9

    ];7const geekAPublicKey = geek.generateKeys[];1const geekAPublicKey = geek.generateKeys[];2

    Output:

    Secret of A is: Ac7p1CjFXyTrdcVxx0HIs0Jqjr3fGb7sUTxfgdUQ+xgXmpJgWKS9SECkFf3ehly+xyvE2MtWFcAxF2gq9F7k7tT5Secret of B is: Ac7p1CjFXyTrdcVxx0HIs0Jqjr3fGb7sUTxfgdUQ+xgXmpJgWKS9SECkFf3ehly+xyvE2MtWFcAxF2gq9F7k7tT5The secrets match!
    Secret of B is: Ac7p1CjFXyTrdcVxx0HIs0Jqjr3fGb7sUTxfgdUQ+xgXmpJgWKS9SECkFf3ehly+xyvE2MtWFcAxF2gq9F7k7tT5
    The secrets match!

    Ví dụ 2: Trong ví dụ này, tham số InputEncoding được truyền dưới dạng null như phương thức cersoveKeys [] không mã hóa các khóa khi tạo chúng.In this example, the inputEncoding parameter is passed as null as the generateKeys[] method does not encode the keys when generating them.

    JavaScript

    const crypto = require['crypto'];

    const geekAPublicKey = geek.generateKeys[];6'secp521r1'];

    const geekAPublicKey = geek.generateKeys[];9

    console.log[0'secp521r1'];

    console.log[3

    console.log[4

    ____10console.log[6console.log[7];0const crypto = require[7__

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    01

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    0
    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    03console.log[7];0const crypto = require[7];

    console.log[

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    09
    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    10

    console.log[

    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    12
    ecdh.computeSecret[ otherPublicKey, inputEncoding, outputEncoding ]
    13

    Output:

    Secret of Geek One is: ACc+SKe9XQMw5quzSEKs0Os+OhGKPRqHIwkW13+lxhs2HNwUEvbZdCEOE/PCzdNKk3v5zqdWSHO0kfRy1qBM8Kc6Secret of Geek Two is: ACc+SKe9XQMw5quzSEKs0Os+OhGKPRqHIwkW13+lxhs2HNwUEvbZdCEOE/PCzdNKk3v5zqdWSHO0kfRy1qBM8Kc6
    Secret of Geek Two is: ACc+SKe9XQMw5quzSEKs0Os+OhGKPRqHIwkW13+lxhs2HNwUEvbZdCEOE/PCzdNKk3v5zqdWSHO0kfRy1qBM8Kc6

    Tham khảo: //nodejs.org/api/crypto.html#crypto_ecdh_computesecret_otherpublicKey_inputencoding_outputencoding //nodejs.org/api/crypto.html#crypto_ecdh_computesecret_otherpublickey_inputencoding_outputencoding


    Bài Viết Liên Quan

    Chủ Đề