如何将Node Crypto.generateKeypairsync转换为DENO?

发布于 2025-02-06 12:09:22 字数 1400 浏览 2 评论 0原文

我想将以下代码从节点(v18.1)程序转换为deno(v1.22),其中在ssh密钥交换过程中生成密钥对:

// generate.mjs
import { generateKeyPairSync } from "crypto";

const keys = generateKeyPairSync("x25519");

上面的代码是较大的SSH客户端的一部分DENO以节点兼容模式运行(- compat)。但是,GERTATEKEYPAIRSYNC尚未实施,所以我想将其更改为deno等效:

deno run --compat --unstable --allow-env ./generate.mjs

error: Uncaught Error: Not implemented: crypto.generateKeyPairSync
  throw new Error(message);
        ^
    at notImplemented (https://deno.land/[email protected]/node/_utils.ts:22:9)
    at generateKeyPairSync (https://deno.land/[email protected]/node/internal/crypto/keygen.ts:662:3)
    at file:///C:/Users/gjzwiers/repos/nodeno_ssh/generate.mjs:3:14

我已经看了 generateKey在WebCrypto api 中,但似乎它不支持x25519? WebCrypto API中是否有X25519的替代方法,还是有其他方法可以在DeNo中使用此操作?

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/[email protected]/node/_utils.ts:22:9)
    at generateKeyPairSync (https://deno.land/[email protected]/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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏尔 2025-02-13 12:09:22

新答案:截至Deno 1.33 GenerateKeypairsync支持如下:

import { generateKeyPairSync } from "node:crypto";

const keys = generateKeyPairSync("x25519");

旧答案:

我最终使用了评论中建议的外部模块。我使用node-forge生成键盘:

// deno run generate.mjs
import forge from 'npm:node-forge';

const ed25519 = forge.pki.ed25519;

const keypair = ed25519.generateKeyPair();

New answer: As of Deno 1.33 generateKeyPairSync is supported as follows:

import { generateKeyPairSync } from "node:crypto";

const keys = generateKeyPairSync("x25519");

Old answer:

I ended up using an external module as suggested in the comments. I used node-forge to generate a keypair:

// deno run generate.mjs
import forge from 'npm:node-forge';

const ed25519 = forge.pki.ed25519;

const keypair = ed25519.generateKeyPair();

还如梦归 2025-02-13 12:09:22

也许这可能是可能的解决方案:

最初是窗口。
你应该(或可能)
请参阅描述Bellow


const x25519_key = await crypto
  .subtle
  .generateKey({ name: 'X25519' },
    true,
    ['deriveKey', 'deriveBits'],
  );

请参阅以下答案的原始来源的链接下方的

https://github.com/tqsw/webcrypto-curve25519/blob/master/master/explainer.md#key-agreement-with-with-x25519

标准(现在我们甚至可以在MDN文档(又名Cli-Browser)中查看他的徽标。

perhaps this can be possible solution:

originally was window.crypto.subtle.generateKey... but in case with Deno
you should (or may) ommit this
see description bellow

const x25519_key = await crypto
  .subtle
  .generateKey({ name: 'X25519' },
    true,
    ['deriveKey', 'deriveBits'],
  );

below the link with original source of this answer:

https://github.com/tQsW/webcrypto-curve25519/blob/master/explainer.md#key-agreement-with-x25519

P.S. Because Deno is very carefully follows Web API standards (now we can even see his logo in browser-compatibility section on MDN documentation, aka cli-browser)... so you can simply use browser's api without any imports of node's modules etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文