dominictarr/ssh-key-to-pem — pull request #3 by bahamas10 (2014-03-10 23:07:55+00:00)
Title: added reverse function for pem => ssh-rsa

# summary

this adds an exported function, `pemToRsaSSHKey` that takes a public key in PEM format, and returns an `ssh-rsa` key as a string.

# example

read in my public key to the node console

``` javascript
> var pubkey = fs.readFileSync('/Users/dave/.ssh/id_rsa.pub', 'utf-8')
> console.log(pubkey)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+JUivj9iHGMnZ0eVS3hJ+jy6gHYLqwK5S6K6r/TaF0uuJv5mYEIIqhkEeg17XkYUZDW6Xcy0GdHNDI53DKTZhF4Ki3KnCQCfGYtLlNsQcvZ08WNTzXZDqXRvkIPwaGXppzuIohoI5J0Y1k89UrjfqBaY8n7JK+dAisA+OL/NXtdu6G+l3bQY28xEKTUkHSXpInQGRieS6veDReMlRg9hlpTWviGTx3SF4O0sD5SuggI+g4dYk311j8iR/AodGp0YxSWr694iBCiGJ5RtXbIaLPl2gdgSeKgonmTqdo3jVeMp7XljDxL+XPAf+Y5YbnhPUNd8P8Q8QElUfIwCJJ6hD dave@Operationss-MacBook-Pro.local
```

use this module to convert it to PEM format

``` js
> var sshkeymod = require('./')
> var pem = sshkeymod(pubkey)
> console.log(pem)
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAviVIr4/YhxjJ2dHlUt4S
fo8uoB2C6sCuUuiuq/02hdLrib+ZmBCCKoZBHoNe15GFGQ1ul3MtBnRzQyOdwyk2
YReCotypwkAnxmLS5TbEHL2dPFjU812Q6l0b5CD8Ghl6ac7iKIaCOSdGNZPPVK43
6gWmPJ+ySvnQIrAPji/zV7Xbuhvpd20GNvMRCk1JB0l6SJ0BkYnkur3g0XjJUYPY
ZaU1r4hk8d0heDtLA+UroICPoOHWJN9dY/IkfwKHRqdGMUlq+veIgQohieUbV2yG
iz5doHYEnioKJ5k6naN41XjKe15Yw8S/lzwH/mOWG54T1DXfD/EPEBJVHyMAiSeo
QwIDAQAB
-----END PUBLIC KEY-----
```

assert that the original pubkey (with trailing newline removed) is the same as `pemToRsaSSHKey(pem)` (full circle conversion)

``` js
> assert.strictEqual(sshkeymod.pemToRsaSSHKey(pem, 'dave@Operationss-MacBook-Pro.local'), pubkey.trim())
undefined
>
```

# tests

added test coverage for all 3 `ssh-rsa` keys found in the existing tests

```
dave @ [ bahamas10 :: (Darwin) ] ~/dev/ssh-key-to-pem (git:master) $ npm test

> ssh-key-to-pem@0.10.0 test /Users/dave/dev/ssh-key-to-pem
> node test/convert.test.js

TAP version 13
# 1024b pem to rsa ssh key
ok 1 should be equal
# 2048b pem to rsa ssh key
ok 2 should be equal
# 4096b pem to rsa ssh key
ok 3 should be equal
# 1024b rsa ssh key
ok 4 should be equal
# 2048b rsa ssh key
ok 5 should be equal
# 4096b rsa ssh key
ok 6 should be equal
# 1024b dsa ssh key
ok 7 should be equal
# fingerprint
ok 8 should be equal

1..8
# tests 8
# pass 8

# ok
```