javascriptecdsaecdhed25519node-forge

Is there a way for generation of ecdh shared key in 'node-forge' or other checked and secure js oackage


Well what i want to obtain is something like this

import * as forge from 'node-forge'
const aliceKey = forge.pki.ed25519.generateKeyPair();
const bobKey = forge.pki.ed25519.generateKeyPair();

//get shared secret for bob and alice
function getSharedKey(publicKey, privateKey){
//some code
}

const sharedKeyFromAlicePerspective = getSharedKey(bobKey.publicKey , aliceKey.privateKey);
const sharedKeyFromBobPerspective = getSharedKey(aliceKey.publicKey, bobKey.privateKey);

console.log(sharedKeyFromAlicePerspective === sharedKeyFromBobPerspective)//expect 'true'

Also i am open for changing library for other secure and well maintained and tested if its not possible in standard that this library provides (my point is to make smallest possible secure public keys for encryption beetween parties)


Solution

  • well beside @Topaco answear i found out if you are in browser that support Crypto.subtle api you can do without modules.

    const aliceKey = await window.crypto.subtle.generateKey({name: 'ECDH', namedCurve: 'P-521'},false, ['deriveBits']);
    const bobKey = await window.crypto.subtle.generateKey({name: 'ECDH', namedCurve: 'P-521'},false, ['deriveBits']);
    const secret = await window.crypto.subtle.deriveBits({
            name: "ECDH",
            namedCurve: "P-521",
            public: bobKey.publicKey
        },
        aliceKey.privateKey, 
        521
    )