title: HmacSHA256 in WebAssembly compiled with Emscripten
body: <p>I'm trying to implement JWT token (encoding only) in WebAssembly, the goal is to have a <strong>very light weight</strong> wasm module. As a web developer my C knowledge is limited. For now I've implemented the following function (ported from JS) to encode url-safe Base64 encoder, which works perfectly.</p>
  
  <pre><code>char _keyStr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
  char ret_val[200];
  
  char* encode (char *data){
      int len = strlen(data);
      int i = 0;
      int j = 0;
  
      while(i&lt;len){
          char chr1 = data[i++];
          int chr2Out = (i &gt; len - 1)? 1:0;
          char chr2 = data[i++];
          int chr3Out = (i &gt; len - 1)? 1:0;;
          char chr3 = data[i++];
  
  
          char enc1 = chr1 &gt;&gt; 2;
          char enc2 = ((chr1 &amp; 3) &lt;&lt; 4) | (chr2 &gt;&gt; 4);
          char enc3 = ((chr2 &amp; 15) &lt;&lt; 2) | (chr3 &gt;&gt; 6);
          char enc4 = chr3 &amp; 63;
  
          if (chr2Out) {
              enc3 = enc4 = 64;
          } else if (chr3Out) {
              enc4 = 64;
          }
  
          ret_val[j++] = _keyStr[enc1];
          ret_val[j++] = _keyStr[enc2];
          ret_val[j++] = _keyStr[enc3];
          ret_val[j++] = _keyStr[enc4];
  
      }
      ret_val[j] = '\0';
      return ret_val;
  }
  </code></pre>
  
  <p>My next challenge is to be able to sign my JWT payload with HmacSHA256.
  The following JS fiddle, describes what I want to accomplish with C.
  <a href="https://jsfiddle.net/gm7boy2p/813/" rel="noreferrer">https://jsfiddle.net/gm7boy2p/813/</a></p>
  
  <p>I'm struggling with integrating a 3rd party code and complie it with emcc.
  I'm looking for a light weight library or a snippet.</p>
  
  <p>Example code or any help would be appreciated.</p>
  
  <hr>
  
  <p><strong>Update:</strong> After extra research, reading <a href="https://stackoverflow.com/questions/45974738/how-to-link-openssl-with-emscripten?answertab=active#tab-top">this</a> stackoverflow question and <a href="https://developers.google.com/web/updates/2018/03/emscripting-a-c-library" rel="noreferrer">this</a> article, it looks like using openssl or any other external library with WebAssembly is far from trivial. So what I'm looking for now is a standalone C function that I could integrate to my existing code.</p>