edo999
edo9994mo ago

How to use crypto generateKeyPairSync?

I'm trying to make generateKeyPairSync work, but it fails. The sample code at the bottom of https://docs.deno.com/api/node/crypto/~/generateKeyPairSync does not work on deno 1.44.4 (MacOS Sonoma 14.5 M2 chip). I get:
error: Uncaught (in promise) Error: Not implemented: crypto.PrivateKeyObject.prototype.export
const { publicKey, privateKey } = generateKeyPairSync("rsa", {
^
at notImplemented (ext:deno_node/_utils.ts:9:9)
at PrivateKeyObject.export (ext:deno_node/internal/crypto/keys.ts:206:5)
at generateKeyPairSync (ext:deno_node/internal/crypto/keygen.ts:77:37)
at file:///deno/test.ts:3:35
error: Uncaught (in promise) Error: Not implemented: crypto.PrivateKeyObject.prototype.export
const { publicKey, privateKey } = generateKeyPairSync("rsa", {
^
at notImplemented (ext:deno_node/_utils.ts:9:9)
at PrivateKeyObject.export (ext:deno_node/internal/crypto/keys.ts:206:5)
at generateKeyPairSync (ext:deno_node/internal/crypto/keygen.ts:77:37)
at file:///deno/test.ts:3:35
I get the same thing with generateKeyPair. The only way it works, is if I remove the both key encodings from the options:
const { generateKeyPairSync } = await import("node:crypto");

const { publicKey, privateKey } = generateKeyPairSync("rsa", {
modulusLength: 4096,
});
const { generateKeyPairSync } = await import("node:crypto");

const { publicKey, privateKey } = generateKeyPairSync("rsa", {
modulusLength: 4096,
});
What am I doing wrong?
1 Reply
Deno AI Helper
Deno AI Helper4mo ago
I apologize for the inconvenience, but I wasn't able to find the answer to your question in the official Deno documentation. However, based on the error message you've provided, it seems that the crypto.PrivateKeyObject.prototype.export method is not implemented in your current version of Deno. The error message indicates that the export method on PrivateKeyObject is not available, which suggests that the issue might be related to the specific encoding options you are using for the keys. Since you mentioned that removing the key encodings from the options makes it work, it seems that the problem is indeed related to the key encoding. Here's the part of your code that seems to be causing the issue:
const { publicKey, privateKey } = generateKeyPairSync("rsa", {
// ... encoding options might be here
});
const { publicKey, privateKey } = generateKeyPairSync("rsa", {
// ... encoding options might be here
});
Since I cannot provide a solution based on the Deno documentation, I recommend double-checking the compatibility of the key encoding options you are using with the version of Deno you have installed. It's possible that the version of Deno you are using does not support certain encoding options for key generation. Please note that this advice is not based on the Deno documentation, and I advise you to double-check the accuracy of the advice given. If you continue to experience issues, you may want to consider reaching out to the Deno community or checking the GitHub issues for Deno to see if others have encountered and resolved similar problems.