mirror of
https://github.com/amark/gun.git
synced 2025-06-05 21:56:51 +00:00
secret
This commit is contained in:
parent
44de63dc55
commit
d42ed3ff95
2
gun.min.js
vendored
2
gun.min.js
vendored
File diff suppressed because one or more lines are too long
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gun",
|
"name": "gun",
|
||||||
"version": "0.9.992",
|
"version": "0.9.993",
|
||||||
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
|
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"browser": "gun.min.js",
|
"browser": "gun.min.js",
|
||||||
|
77
sea.js
77
sea.js
@ -502,6 +502,51 @@
|
|||||||
module.exports = SEA.decrypt;
|
module.exports = SEA.decrypt;
|
||||||
})(USE, './decrypt');
|
})(USE, './decrypt');
|
||||||
|
|
||||||
|
;USE(function(module){
|
||||||
|
var SEA = USE('./root');
|
||||||
|
var shim = USE('./shim');
|
||||||
|
var S = USE('./settings');
|
||||||
|
// Derive shared secret from other's pub and my epub/epriv
|
||||||
|
SEA.secret = async (key, pair, cb) => { try {
|
||||||
|
const pub = key.epub || key
|
||||||
|
const epub = pair.epub
|
||||||
|
const epriv = pair.epriv
|
||||||
|
const ecdhSubtle = shim.ossl || shim.subtle
|
||||||
|
const pubKeyData = keysToEcdhJwk(pub)
|
||||||
|
const props = {
|
||||||
|
...S.ecdh,
|
||||||
|
public: await ecdhSubtle.importKey(...pubKeyData, true, [])
|
||||||
|
}
|
||||||
|
const privKeyData = keysToEcdhJwk(epub, epriv)
|
||||||
|
const derived = await ecdhSubtle.importKey(...privKeyData, false, ['deriveKey'])
|
||||||
|
.then(async (privKey) => {
|
||||||
|
// privateKey scope doesn't leak out from here!
|
||||||
|
const derivedKey = await ecdhSubtle.deriveKey(props, privKey, { name: 'AES-CBC', length: 256 }, true, [ 'encrypt', 'decrypt' ])
|
||||||
|
return ecdhSubtle.exportKey('jwk', derivedKey).then(({ k }) => k)
|
||||||
|
})
|
||||||
|
const r = derived;
|
||||||
|
if(cb){ cb(r) }
|
||||||
|
return r;
|
||||||
|
} catch(e) {
|
||||||
|
SEA.err = e;
|
||||||
|
if(cb){ cb() }
|
||||||
|
return;
|
||||||
|
}}
|
||||||
|
|
||||||
|
const keysToEcdhJwk = (pub, d) => { // d === priv
|
||||||
|
//const [ x, y ] = Buffer.from(pub, 'base64').toString('utf8').split(':') // old
|
||||||
|
const [ x, y ] = pub.split('.') // new
|
||||||
|
const jwk = d ? { d } : {}
|
||||||
|
return [ // Use with spread returned value...
|
||||||
|
'jwk',
|
||||||
|
{ ...jwk, x, y, kty: 'EC', crv: 'P-256', ext: true }, // ??? refactor
|
||||||
|
S.ecdh
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = SEA.secret;
|
||||||
|
})(USE, './secret');
|
||||||
|
|
||||||
;USE(function(module){
|
;USE(function(module){
|
||||||
// Old Code...
|
// Old Code...
|
||||||
const {
|
const {
|
||||||
@ -569,38 +614,6 @@
|
|||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Derive shared secret from other's pub and my epub/epriv
|
|
||||||
SEA.derive = async (pub, { epub, epriv }) => {
|
|
||||||
try {
|
|
||||||
const ecdhSubtle = ossl || subtle
|
|
||||||
const keysToEcdhJwk = (pub, d) => { // d === priv
|
|
||||||
const [ x, y ] = Buffer.from(pub, 'base64').toString('utf8').split(':')
|
|
||||||
const jwk = d ? { d } : {}
|
|
||||||
return [ // Use with spread returned value...
|
|
||||||
'jwk',
|
|
||||||
{ ...jwk, x, y, kty: 'EC', crv: 'P-256', ext: true },
|
|
||||||
ecdhKeyProps
|
|
||||||
]
|
|
||||||
}
|
|
||||||
const pubKeyData = keysToEcdhJwk(pub)
|
|
||||||
const props = {
|
|
||||||
...ecdhKeyProps,
|
|
||||||
public: await ecdhSubtle.importKey(...pubKeyData, true, [])
|
|
||||||
}
|
|
||||||
const privKeyData = keysToEcdhJwk(epub, epriv)
|
|
||||||
const derived = await ecdhSubtle.importKey(...privKeyData, false, ['deriveKey'])
|
|
||||||
.then(async (privKey) => {
|
|
||||||
// privateKey scope doesn't leak out from here!
|
|
||||||
const derivedKey = await ecdhSubtle.deriveKey(props, privKey, { name: 'AES-CBC', length: 256 }, true, [ 'encrypt', 'decrypt' ])
|
|
||||||
return ecdhSubtle.exportKey('jwk', derivedKey).then(({ k }) => k)
|
|
||||||
})
|
|
||||||
return derived
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e)
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// all done!
|
// all done!
|
||||||
// Obviously it is missing MANY necessary features. This is only an alpha release.
|
// Obviously it is missing MANY necessary features. This is only an alpha release.
|
||||||
// Please experiment with it, audit what I've done so far, and complain about what needs to be added.
|
// Please experiment with it, audit what I've done so far, and complain about what needs to be added.
|
||||||
|
@ -11,7 +11,7 @@ Gun.chain.put = function(data, cb, as){
|
|||||||
if(typeof cb === 'string'){
|
if(typeof cb === 'string'){
|
||||||
as.soul = cb;
|
as.soul = cb;
|
||||||
} else {
|
} else {
|
||||||
as.ack = cb;
|
as.ack = as.ack || cb;
|
||||||
}
|
}
|
||||||
if(at.soul){
|
if(at.soul){
|
||||||
as.soul = at.soul;
|
as.soul = at.soul;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user