This is what I've got so far to export a public and private key from a keypair:
let pub = await crypto.subtle.exportKey("spki", keyPair.publicKey);
let prv = await crypto.subtle.exportKey("spki", keyPair.privateKey);
This results in two individual array buffers holding my public and private key.
I would like to find out how to export the entire keypair at once into a single array buffer?
Something like this:
let pair = await crypto.subtle.exportKeyPair("spki", keyPair);
Is there a web api and a format for this?
Otherwise is there a safe way to concatenate the two array buffers (the exported public and private key) together in such a way that I can split them up again when importing? I would then need to have some mechanism to handle malformed input when importing the exported key pair.
I must do this because my interface requires me to return a single array buffer.
I found that I can export the public key using the following code. I haven't figured out the private key just yet. Also I built this sample from the code on:
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey#subjectpublickeyinfo_export
const exported = pub;
const exportedAsString = String.fromCharCode.apply(null, new Uint8Array(exported));
const exportedAsBase64 = window.btoa(exportedAsString);
const pemExported = '-----BEGIN PUBLIC KEY-----\n'+exportedAsBase64+'\n-----END PUBLIC KEY-----`;
console.log("Public Exported Key: ", pemExported);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With