-
Notifications
You must be signed in to change notification settings - Fork 790
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 use crypto api keys with forge #284
Comments
This would help me out a lot too! |
This is related to #255. You probably just want to export as 'pkcs8' and then convert to a forge buffer and use forge's asn1 API calls from there. Let me see if I can get it working real quick. |
This worked for me: (function() {
var crypto = window.crypto.subtle;
crypto.generateKey({
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {
name: "SHA-256"
},
}, true, ["encrypt", "decrypt"]).then(function(keyPair) {
crypto.exportKey("pkcs8", keyPair.privateKey).then(
function(exportedPrivateKey) {
// How to convert to PEM or forge format?
console.log(keyPair.privateKey);
console.log(exportedPrivateKey);
// convert to forge buffer, then from DER, then export to PEM
var buffer = new forge.util.ByteBuffer(exportedPrivateKey);
var asn1 = forge.asn1.fromDer(buffer);
var privateKey = forge.pki.privateKeyFromAsn1(asn1);
var pem = forge.pki.privateKeyToPem(privateKey);
console.log(pem);
});
});
})(); Depending on what you want to do, you may be able to instead just base64-encode the exported PKCS8-formatted ArrayBuffer and then wrap it in a PEM header. But the above demonstrates how to get the key into something forge can use and export with its API. |
Worked for me too!!!!! |
You are my hero! |
Haha! Great, glad it worked for you guys. :) |
Hi,
is there a way to use browsers crypto api key generation with forge?
I create a keypair using the crypto api and want to convert the resulting private key to PEM format, so i can use it in forge or export it.
The problem is that i cannot find a way to convert between the different formats or how to tell forge that it should use
window.crypto.subtle.generateKey
for key generation.Hope somebody can help me :)
The text was updated successfully, but these errors were encountered: