joyent/node-http-signature — pull request #26 by bahamas10 (2014-03-11 17:00:54+00:00)
Title: pem => ssh-rsa conversion

# 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 httpsig = require('./')
> var pem = httpsig.sshKeyToPEM(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(httpsig.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

```
$ npm test

> http-signature@0.10.0 test /Users/dave/dev/node-http-signature
> tap test/*.js

ok test/convert.test.js ................................. 9/9
ok test/parser.test.js ................................ 64/64
ok test/signer.test.js .................................. 8/8
ok test/verify.test.js ................................ 22/22
total ............................................... 103/103

ok
```