Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert an exported key to base64 in nodejs? #7715

Closed
markg85 opened this issue Oct 5, 2020 · 1 comment
Closed

How to convert an exported key to base64 in nodejs? #7715

markg85 opened this issue Oct 5, 2020 · 1 comment
Labels
kind/bug A bug in existing code (including security flaws) need/triage Needs initial labeling and prioritization

Comments

@markg85
Copy link
Contributor

markg85 commented Oct 5, 2020

Hi,

I'm using IPFS 0.7.0 with the shiny new feature to export keys.

So i do:

ipfs key gen test
ipfs key export test

Which gives me a "test.key" in binary libp2p protobuf format.

What i try to do now is get that key working in nodejs crypto.
This is where i'm hitting some snags.

In node.js i need to import the key as a PEM key to be able to import and use it. This means it needs to be in this exact format:

-----BEGIN PRIVATE KEY-----
<THE KEY>
-----END PRIVATE KEY-----

Begin and end are easy :)
The key needs to be in base64.

Now to load this into javascript i did the following:

const fs = require("fs")
const crypto = require('crypto');
const Schema = require("./crypto_pb")

let data = fs.readFileSync("./test.key")
let deserializedData = Schema.PrivateKey.deserializeBinary(data)
let keyBuffer = deserializedData.getData();
console.log(Buffer.from(keyBuffer).toString('base64'))

Note that very last console.log line. There i'm printing the key, which results in:

2qLSIF08WHkcz5f+ca90C+BRe7I8vjNlDarPREXCmkcNUzmzNro7dN0obChI5pQF3aK/owNVgEhw1OMd4dDUiA==

That's too long.
The keyBuffer object is (i verified that) 64 bytes long. So it does seem to match what the libp2p spec tells.
The base64 result is however 88 characters which doesn't seem to be right. Importing it as that gives me an error of: header too long.

I'm sure i'm missing a conversion somewhere. But where and from what to what is not clear to me.

Thank a lot!

Cheers,
Mark

@markg85 markg85 added kind/bug A bug in existing code (including security flaws) need/triage Needs initial labeling and prioritization labels Oct 5, 2020
@jacobheun
Copy link
Contributor

jacobheun commented Oct 9, 2020

@markg85 please ask usage questions on the forums, https://discuss.ipfs.io.

You can marshal the key in JS with https://github.com/libp2p/js-libp2p-crypto#cryptokeysimportencryptedkey-password and then handle your conversion to PEM from there. Feel free to ping me on the forums.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug A bug in existing code (including security flaws) need/triage Needs initial labeling and prioritization
Projects
None yet
Development

No branches or pull requests

2 participants