Skip to content

Instantly share code, notes, and snippets.

@HamdaanAliQuatil
Created February 11, 2025 18:22
Show Gist options
  • Select an option

  • Save HamdaanAliQuatil/2170d5336ed18e9261e6b77a434dae63 to your computer and use it in GitHub Desktop.

Select an option

Save HamdaanAliQuatil/2170d5336ed18e9261e6b77a434dae63 to your computer and use it in GitHub Desktop.
const x25519_key = await crypto.subtle.generateKey(
{ name: "X25519" },
true, // Extractable
["deriveKey", "deriveBits"]
);
// Export the keys
const privateKeyJwk = await crypto.subtle.exportKey("pkcs8", x25519_key.privateKey);
const publicKeyJwk = await crypto.subtle.exportKey("spki", x25519_key.publicKey);
// Convert ArrayBuffer to Base64 for readability
const arrayBufferToBase64 = (buffer) => {
const binary = String.fromCharCode(...new Uint8Array(buffer));
return btoa(binary);
};
console.log("Private Key (Base64 PKCS8):", arrayBufferToBase64(privateKeyJwk));
console.log("Public Key (Base64 SPKI):", arrayBufferToBase64(publicKeyJwk));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment