ecdh.setPublicKey(publicKey[, encoding])
添加於:v0.11.14已棄用:v5.2.0
Stability: 0 - 已棄用
參數
publicKey
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView>encoding
<string>publicKey
字符串的encoding。
設置 EC Diffie-Hellman 公鑰。如果提供了encoding
,則publicKey
應該是一個字符串;否則會出現
、 Buffer
TypedArray
或 DataView
。
通常沒有理由調用此方法,因為ECDH
隻需要私鑰和對方的公鑰來計算共享 key 。通常將調用
或ecdh.generateKeys()
。 ecdh.setPrivateKey()
方法嘗試生成與正在設置的私鑰關聯的公共點/ key 。ecdh.setPrivateKey()
示例(獲取共享 key ):
const { createECDH, createHash } = await import('node:crypto'); const alice = createECDH('secp256k1'); const bob = 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( 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);
const { createECDH, createHash, } = require('node:crypto'); const alice = createECDH('secp256k1'); const bob = 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( 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 ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])用法及代碼示例
- Node.js ECDH用法及代碼示例
- Node.js EventEmitter用法及代碼示例
- Node.js Error.message用法及代碼示例
- Node.js Error.stack用法及代碼示例
- Node.js Error.captureStackTrace(targetObject[, constructorOpt])用法及代碼示例
- Node.js EventTarget.addEventListener(type, listener[, options])用法及代碼示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代碼示例
- Node.js http2.Http2ServerRequest request.url用法及代碼示例
- Node.js request.socket用法及代碼示例
- Node.js assert.notEqual(actual, expected[, message])用法及代碼示例
- Node.js tlsSocket.authorized用法及代碼示例
- Node.js zlib.deflateRaw()用法及代碼示例
- Node.js http.IncomingMessage message.rawHeaders用法及代碼示例
- Node.js Console用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js URL.protocol用法及代碼示例
- Node.js http.Agent.reuseSocket(socket, request)用法及代碼示例
- Node.js fs.filehandle.datasync()用法及代碼示例
- Node.js socket.bind()用法及代碼示例
- Node.js v8.getHeapSpaceStatistics()用法及代碼示例
- Node.js http2session.destroyed用法及代碼示例
- Node.js http.ServerResponse response.statusCode用法及代碼示例
- Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代碼示例
- Node.js Http2ServerResponse.finished用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 ECDH.setPublicKey(publicKey[, encoding])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。