I want to convert the following code from a Node (v18.1) program to Deno (v1.22), where a key pair is generated during SSH key exchange:
// generate.mjs
import { generateKeyPairSync } from "crypto";
const keys = generateKeyPairSync("x25519");
The code above is part of a larger SSH client, which mostly works in Deno when run in Node compatibility mode (--compat). However, generateKeyPairSync is not yet implemented, so I want to change it to a Deno equivalent:
deno run --compat --unstable --allow-env ./generate.mjs
error: Uncaught Error: Not implemented: crypto.generateKeyPairSync
throw new Error(message);
^
at notImplemented (https://deno.land/std@0.142.0/node/_utils.ts:22:9)
at generateKeyPairSync (https://deno.land/std@0.142.0/node/internal/crypto/keygen.ts:662:3)
at file:///C:/Users/gjzwiers/repos/nodeno_ssh/generate.mjs:3:14
I have looked at generateKey in the WebCrypto API, but it seems that it does not support x25519? Is there an alternative for x25519 in the WebCrypto API , or is there another way to get this working in Deno?
I ended up using an external module as suggested in the comments. I used node-forge to generate a keypair, which works nicely when run in Node compatibility mode for Deno:
// run with: deno run --compat --unstable --allow-env --allow-read generate.mjs
import forge from 'node-forge';
const ed25519 = forge.pki.ed25519;
const keypair = ed25519.generateKeyPair();