在JavaScript中生成格式正确的SSH密钥 [英] Generate well-formed SSH key in JavaScript

查看:291
本文介绍了在JavaScript中生成格式正确的SSH密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JavaScript开发 Bitbucket API包装器。我正在尝试为添加SSH密钥的方法编写测试。我天真的尝试创建可信的SSH密钥失败。这是我生成虚拟密钥的方法:

I'm developing a Bitbucket API wrapper in JavaScript. I'm trying to write tests for the method which adds an SSH key. My naive attempt at creating a believable SSH key was unsuccessful. Here's how I generated a dummy key:


  1. 以 ssh-rsa开头
  2. 279个随机字符的字符串的Base64编码表示形式

  3. 附加

  4. 附加16个随机字符的字符串

  5. 附加 @

  6. 附加16个随机字符的字符串

  7. 附加 .com

  1. start with "ssh-rsa "
  2. append the Base64-encoded representation of a 279 character string of random characters
  3. append " "
  4. append a 16 character string of random characters
  5. append "@"
  6. append a 16 character string of random characters
  7. append ".com"

当我发布以此方式生成的SSH密钥。幸运的是,我可以访问Bitbucket的源代码,因此可以进行更深入的研究。 Bitbucket通过通过 ssh-keygen

Bitbucket returns 400 Bad Request when I POST an SSH key generated in this manner. Fortunately I have access to Bitbucket's source code, so I've been able to dig a little deeper. Bitbucket validates SSH keys by running them through ssh-keygen.

我不了解SSH密钥是如何生成的,因此我可能会问一个愚蠢的问题。是否可以生成足以说服 ssh-keygen 的SSH密钥?输出为:

I don't understand how SSH keys are generated, so I might be asking a silly question. Is it possible to generate an SSH key convincing enough to fool ssh-keygen? Here's the output:

$ ssh-keygen -l -f ~/Desktop/dummy_rsa.pub 
buffer_get_string_ret: bad string length 1903654498
key_from_blob: can't read key type
key_read: key_from_blob cXd2YnRzZXpha3Rld2V4YmdieWdoeWl2aXpla3hkaHBodnlteHl2ZHl0bnloYmRyYWZrdnVxaWR3cHBydnFmZWFkaHp0aGRwYml6ZXVxY3ZlZ3NiZ29lanl0cG9vZmlnZ2dyZmJ3aWxsdXJhb2puYWRjY3F0YW5rcGV3Z3dkc3lxd2tkb3d6emFzbXpubXJ1eGN2bm53a3l6bmRjenV1dnplbnFtZ3Z5bm96ZGZhandwcG9mcHVoaWFkZ25ud3VkdnB0enV6Zm51bWVxanhzanlwa2ZodGxpd2xld2pnY3dhbmJ6aXVyamp6c29rbm54dHp2enJmeWhnY2ZrcWlqemFscGNnbWJsY3lpcmRtYXFkbHB6c3l0 duvrnzsladfrmccr@xqrbenpfryhoklgw.com
 failed
/Users/dc/Desktop/dummy_rsa.pub is not a public key file.

需要明确的是,我不需要生成有效的 SSH密钥,我只需要以便能够测试各种API端点。另外,由于测试是在浏览器中运行的,因此我无法掏出钱来生成密钥。

To be clear, I don't need to generate a "valid" SSH key, I just need to be able to test various API endpoints. Also, since the tests run in a browser, I can't shell out to generate a key.

推荐答案

SSH密钥不是只是随机的字符串,这就是为什么它们不通过验证的原因。

SSH keys are not just random strings of characters, so that's why they don't pass verification.

此库这里称为Javascript密码学工具包可以帮助您生成RSA密钥(由SSH使用)。

This library here called Javascript Cryptography Toolkit can help you generate RSA keys (used by SSH).

它们有一个生成密钥: https://github.com/ats4u/titaniumcore/ blob / master / crypto / RSA.sample1.html

They have a demo of generating the key: https://github.com/ats4u/titaniumcore/blob/master/crypto/RSA.sample1.html

从演示页面的源代码中可以看到,该工具包的重量非常大,但是我想,如果那是您的需要,那那就是您必须使用的。当然,将密钥生成卸载到服务器上会容易得多,但是如果您正在编写仅客户端库,则必须坚持使用。

As you can see from the source code of the demo page, the toolkit is quite heavy weight, but if that's what you need, then that's what you have to use, I guess. Ofcourse it would have been much simpler to just offload key generation to the server, but if you are writing a client side only library, then you have to stick with this.

2019更新:更新了指向github的链接。

2019 update: updated links to github.

这篇关于在JavaScript中生成格式正确的SSH密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆