title: Sign in Javascript and Verify in Flask
body: <p>It is easy to sign and verify in Javascript using existing libraries. However, it is confusing if we want to generate a public-private key pair in Javascript, sign a text and then verify in Flask. I already know some differences, like the default hashing in Javascript side as against python side. However, the verification in Flask side still fails.</p>
  <p><strong>index.html</strong></p>
  <pre><code>function send(){
      promise =   window.crypto.subtle.generateKey(algo,
                      true, //whether the key is extractable (i.e. can be used in exportKey)
                      [&quot;sign&quot;, &quot;verify&quot;] //can be any combination of &quot;sign&quot; and &quot;verify&quot;
                      );
      console.log(promise)
      promise.then( (keys) =&gt; {
          priv = keys.privateKey
          pub = keys.publicKey
          console.log(pub)
          console.log(exportCryptoKey(pub))
          const pub_key_export = exportCryptoKey(pub)
          return pub_key_export.then( (pub_key) =&gt; {
              console.log(&quot;storing keys in&quot;, pub_key)
              signature = window.crypto.subtle.sign(algo, priv, enc_msg);
              signature.then((sign) =&gt; {
                  sgn = window.btoa(ab2str(sign));
                  $.post(&quot;verify&quot;, {&quot;pub&quot;: pub_key, &quot;data&quot;: ab2str(enc_msg), &quot;signature&quot;: sgn}, function(data){
                      console.log(&quot;data&quot;, data);
                  })
              })
          })
      })
  }
  </code></pre>
  
  
  <p><strong>verify.py</strong></p>
  <pre><code>def verifySignature(signature, data, pub_key):
      key = RSA.importKey(pub_key)
      h = SHA256.new(data.encode(&quot;utf-8&quot;))
      verifier = PKCS1_v1_5.new(key)
      return verifier.verify(h, signature)
  </code></pre>