Skip to content

Instantly share code, notes, and snippets.

@codetit4n
Last active September 2, 2023 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codetit4n/a6819626c28fcc33b34eaaff731da4e8 to your computer and use it in GitHub Desktop.
Save codetit4n/a6819626c28fcc33b34eaaff731da4e8 to your computer and use it in GitHub Desktop.
convert base64 to hex
const crypto = require("crypto");
const keyAsTxt =
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEgyYqQmUAmDn9J7dR5xl-HlyAA0R2XV5sgQRnSGXbLt_xCrEdD1IVvvkyTmRD16y9p3C2O4PTZ0OF_ZYD2JgTVA==";
const keyAsBytes = Buffer.from(keyAsTxt, "base64");
const keySpec = new Uint8Array(keyAsBytes);
const algo = { name: "ECDSA", namedCurve: "P-256" };
(async () => {
const publicKey = await crypto.subtle.importKey("spki", keySpec, algo, true, [
"verify",
]);
// convert public key to hex string
const publicKeyHex = Buffer.from(
await crypto.subtle.exportKey("raw", publicKey),
).toString("hex");
console.log(publicKeyHex);
// remove the first byte (0x04) from the hex string
const publicKeyHexWithoutPrefix = publicKeyHex.slice(2);
// get x and y coordinates from the hex string
const x = publicKeyHexWithoutPrefix.slice(0, 64);
const y = publicKeyHexWithoutPrefix.slice(64);
console.log("x: ", x);
console.log("y: ", y);
})();
@shivamlync
Copy link

Thanks for the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment