mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-04-29 06:52:16 +00:00
59 lines
1.3 KiB
JavaScript
59 lines
1.3 KiB
JavaScript
|
'use strict'
|
||
|
|
||
|
const crypto = require('./webcrypto')()
|
||
|
const nodeify = require('nodeify')
|
||
|
|
||
|
exports.generateEphmeralKeyPair = function (curve, callback) {
|
||
|
nodeify(crypto.subtle.generateKey(
|
||
|
{
|
||
|
name: 'ECDH',
|
||
|
namedCurve: curve
|
||
|
},
|
||
|
true,
|
||
|
['deriveBits']
|
||
|
).then((pair) => {
|
||
|
// forcePrivate is used for testing only
|
||
|
const genSharedKey = (theirPub, forcePrivate, cb) => {
|
||
|
if (typeof forcePrivate === 'function') {
|
||
|
cb = forcePrivate
|
||
|
forcePrivate = undefined
|
||
|
}
|
||
|
|
||
|
const privateKey = forcePrivate || pair.privateKey
|
||
|
nodeify(crypto.subtle.importKey(
|
||
|
'spki',
|
||
|
theirPub,
|
||
|
{
|
||
|
name: 'ECDH',
|
||
|
namedCurve: curve
|
||
|
},
|
||
|
false,
|
||
|
[]
|
||
|
).then((publicKey) => {
|
||
|
return crypto.subtle.deriveBits(
|
||
|
{
|
||
|
name: 'ECDH',
|
||
|
namedCurve: curve,
|
||
|
public: publicKey
|
||
|
},
|
||
|
privateKey,
|
||
|
256
|
||
|
)
|
||
|
}).then((bits) => {
|
||
|
// return p.derive(pub.getPublic()).toBuffer('be')
|
||
|
return Buffer.from(bits)
|
||
|
}), cb)
|
||
|
}
|
||
|
|
||
|
return crypto.subtle.exportKey(
|
||
|
'spki',
|
||
|
pair.publicKey
|
||
|
).then((publicKey) => {
|
||
|
return {
|
||
|
key: Buffer.from(publicKey),
|
||
|
genSharedKey
|
||
|
}
|
||
|
})
|
||
|
}), callback)
|
||
|
}
|