阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js crypto ecdh.setPublicKey(废弃)

      Node.js crypto ecdh.setPublicKey(publicKey[, encoding]) (废弃)

      新增于: v0.11.14

      废弃于: v5.2.0

      • publicKey < string> | < Buffer> ||
      • encoding < string> The encoding of the publicKey string.

      Sets the EC Diffie-Hellman public key. If encoding is provided publicKey is expected to be a string; otherwise a Buffer, TypedArray, or DataView is expected.

      Note that there is not normally a reason to call this method because ECDH only requires a private key and the other party’s public key to compute the shared secret. Typically either ecdh.generateKeys() or ecdh.setPrivateKey() will be called. The ecdh.setPrivateKey() method attempts to generate the public point/key associated with the private key being set.

      Example (obtaining a shared secret):

      const crypto = require('crypto');
      const alice = crypto.createECDH('secp256k1');
      const bob = crypto.createECDH('secp256k1');
      
      // This is a shortcut way of specifying one of Alice's previous private
      // keys. It would be unwise to use such a predictable private key in a real
      // application.
      alice.setPrivateKey(
        crypto.createHash('sha256').update('alice', 'utf8').digest()
      );
      
      // Bob uses a newly generated cryptographically strong
      // pseudorandom key pair
      bob.generateKeys();
      
      const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
      const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
      
      // aliceSecret and bobSecret should be the same shared secret value
      console.log(aliceSecret === bobSecret);
      

      更多内容请参考:Node.js crypto 加密,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录