title: Parsing crypto ECDH public key into JWK format to use it with WebCrypto
body: <p>I generated an ECDH key pair, and get the public key from it. Using nodejs and the crypto library. </p>
  
  <pre><code>const ecdh = crypto.createECDH('secp384r1');
  ecdh.generateKeys();
  const publicKey = ecdh.getPublicKey('hex'); 
  </code></pre>
  
  <p>I'm implementing a client side library to encrypt data using this public key, the ECDH algorithm and the <a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto" rel="nofollow">SubtleCrypto</a> interface from WebCrypto.</p>
  
  <p>To Accomplish this I have to import the public key (Generated with nodejs crypto), WebCrypto <a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey" rel="nofollow">importKey method</a> has some format options to do it, but I can get it to work with non of them. I noticed that <code>jwk</code> is more used than the others.</p>
  
  <p>An example of a <code>jwk</code> is</p>
  
  <pre><code>{  
    kty: "EC",
    crv: "P-256",
    x: "kgR_PqO07L8sZOBbw6rvv7O_f7clqDeiE3WnMkb5EoI",
    y: "djI-XqCqSyO9GFk_QT_stROMCAROIvU8KOORBgQUemE",
    d: "5aPFSt0UFVXYGu-ZKyC9FQIUOAMmnjzdIwkxCMe3Iok",
    ext: true,
  }
  </code></pre>
  
  <p>So how can I parse my public key Into a <code>jwk</code> object. My public key looks like this:
  <code>04f8b2a6e9d2ffa424c3e7b6addf23112153920fd0209390da460f99e03bf8665052e72df4a0b7927381f1b026c98a3a2b348fdd10969875e6b0e86cb1f093a5fc07e49fbbbf091922ce71af17f4a79de03f6069836a7143b137be34451f162235</code></p>