mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-03-15 21:50:54 +00:00
fix: browser rsa enc/dec
This commit is contained in:
parent
9f747a173f
commit
b8e2414420
@ -45,7 +45,7 @@
|
|||||||
"keypair": "^1.0.1",
|
"keypair": "^1.0.1",
|
||||||
"libp2p-crypto-secp256k1": "~0.4.0",
|
"libp2p-crypto-secp256k1": "~0.4.0",
|
||||||
"multihashing-async": "~0.7.0",
|
"multihashing-async": "~0.7.0",
|
||||||
"node-forge": "~0.8.5",
|
"node-forge": "^0.9.1",
|
||||||
"pem-jwk": "^2.0.0",
|
"pem-jwk": "^2.0.0",
|
||||||
"protons": "^1.0.1",
|
"protons": "^1.0.1",
|
||||||
"rsa-pem-to-jwk": "^1.1.3",
|
"rsa-pem-to-jwk": "^1.1.3",
|
||||||
|
@ -121,8 +121,33 @@ function derivePublicFromPrivate (jwKey) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bloody dark magic. webcrypto's why.
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
Explanation:
|
||||||
|
- Convert JWK to PEM
|
||||||
|
- Load PEM with nodeForge
|
||||||
|
- Convert msg buffer to nodeForge buffer
|
||||||
|
- Convert resulting nodeForge buffer to buffer
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const forge = require('node-forge')
|
||||||
|
const pki = forge.pki
|
||||||
|
const jwkToPem = require('pem-jwk').jwk2pem
|
||||||
|
function convertKey (key, pub, msg, handle) {
|
||||||
|
const pem = jwkToPem(key)
|
||||||
|
const fkey = pki[pub ? 'publicKeyFromPem' : 'privateKeyFromPem'](pem)
|
||||||
|
const fmsg = forge.util.hexToBytes(Buffer.from(msg).toString('hex'))
|
||||||
|
const fomsg = handle(fmsg, fkey)
|
||||||
|
return Buffer.from(forge.util.bytesToHex(fomsg), 'hex')
|
||||||
|
}
|
||||||
|
|
||||||
exports.encrypt = async function (key, msg) {
|
exports.encrypt = async function (key, msg) {
|
||||||
key = Object.assign({}, key)
|
return convertKey(key, true, msg, (msg, key) => key.encrypt(msg))
|
||||||
|
|
||||||
|
/* key = Object.assign({}, key)
|
||||||
key.key_ops = ['encrypt']
|
key.key_ops = ['encrypt']
|
||||||
|
|
||||||
return webcrypto.subtle.importKey(
|
return webcrypto.subtle.importKey(
|
||||||
@ -140,11 +165,13 @@ exports.encrypt = async function (key, msg) {
|
|||||||
publicKey,
|
publicKey,
|
||||||
Uint8Array.from(msg)
|
Uint8Array.from(msg)
|
||||||
)
|
)
|
||||||
}).then((enc) => Buffer.from(enc))
|
}).then((enc) => Buffer.from(enc)) */
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.decrypt = async function (key, msg) {
|
exports.decrypt = async function (key, msg) {
|
||||||
key = Object.assign({}, key)
|
return convertKey(key, false, msg, (msg, key) => key.decrypt(msg))
|
||||||
|
|
||||||
|
/* key = Object.assign({}, key)
|
||||||
key.key_ops = ['decrypt']
|
key.key_ops = ['decrypt']
|
||||||
|
|
||||||
return webcrypto.subtle.importKey(
|
return webcrypto.subtle.importKey(
|
||||||
@ -162,5 +189,5 @@ exports.decrypt = async function (key, msg) {
|
|||||||
privateKey,
|
privateKey,
|
||||||
Uint8Array.from(msg)
|
Uint8Array.from(msg)
|
||||||
)
|
)
|
||||||
}).then((dec) => Buffer.from(dec))
|
}).then((dec) => Buffer.from(dec)) */
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user