JSFiddle - React, Tailwind, and code Playground

by Josh Pullen

JavaScript

(async () => {

const { publicKey, privateKey } = await crypto.subtle.generateKey(
  { name: "ECDSA", namedCurve: "P-256" },
  true,
  ["sign", "verify"]
);
console.log(publicKey, privateKey);

const exported = await crypto.subtle.exportKey("jwk", privateKey);
console.log(exported);


const encoder = new TextEncoder();
const data = encoder.encode("My snazzy message");
const signatureBuffer = await crypto.subtle.sign(
  {
    name: "ECDSA",
    hash: { name: "SHA-256" }
  },
  privateKey,
  data
);

const signatureArray = Array.from(new Uint8Array(signatureBuffer));
const signatureStr = signatureArray.map(b => b.toString(2).padStart(8, '0')).join("");

console.log(signatureStr);




})();