X25519 for WebAssembly
npm install x25519-wasm-vn``ts
import instantiate, { DiffieHellman, diffie_hellman, hkdf_sha_256, public_key } from 'x25519-wasm-vn/web'
await instantiate()
/*
// Generate a random string that length is 32 bytes
// https://www.avast.com/random-password-generator#pc
const bob_private_key = encoder.encode('h6wned9SXw2wnw8Q8Hjp1YWeOVhsWzQ4');
const bob_public_key = public_key(bob_private_key);
const alice_private_key = encoder.encode('JOukpRSoQ1J31Of9a56hlDTODMPvX7SM');
const alice_public_key = public_key(alice_private_key);
let alice_shared = diffie_hellman(alice_private_key, bob_public_key);
let bob_shared = diffie_hellman(bob_private_key, alice_public_key);
console.log("Shared key (Bob): ", bob_shared);
console.log("Shared key (Alice): ", alice_shared);
const info = encoder.encode('application-key')
let alice_enc_key = hkdf_sha_256(alice_shared, undefined, info, 32);
console.log("Derive key (Alice): ", alice_enc_key);
let bob_enc_key = hkdf_sha_256(bob_shared, undefined, info, 32);
console.log("Derive key (Bob): ", alice_enc_key);
*/
const getPublicKey = async (privateKey: Uint8Array): Promise
return public_key(privateKey);
}
const diffieHellman = async (privateKey: Uint8Array, publicKey: Uint8Array): Promise
let _wasm: DiffieHellman | null = null
try {
_wasm = new DiffieHellman(privateKey)
return _wasm.get_shared_key(publicKey)
} catch (err) {
console.error(err)
} finally {
if (_wasm) {
_wasm.free()
}
}
}
``