title: How to verify mbedtls signature in node.js?
body: <p>I have seems working way how to verify the signature with mbedtls on ESP32. Now I need to verify the same signature with NodeJS but I had no success with it.</p>
  <p>mbedtls example:</p>
  <pre><code>#include &quot;mbedtls/base64.h&quot;
  #include &quot;mbedtls/pk.h&quot;
  #include &lt;string.h&gt;
  
  unsigned char hard_base_signature[6000] =
      &quot;Y59Z0Jg1WCKKMpKuGYW5nLsyDgPU3eoMwtLd74lejj6bWr2lPh5MKzcfXE18s6L7YG7yB18WNW&quot;
      &quot;/meh2KAegHEDZKqy2OHf0n7bKz17OSIcoSgI9N4Eu116naVtqO5vwYJ3JGqE625JkuQza4sLZh&quot;
      &quot;ccosbd+mNLUEBN7sr+BtlY1rU0hed56GbP6iJ+8x5XEW4HBUQ1ATQQGNu5QtqgBA+&quot;
      &quot;jv8TktXlOVJ8XXKkOUUmVu1NU1TX+hXgNqZC6drtg5+&quot;
      &quot;UtaMS77eF5qctMXyNKTqJ9UGTi1S0pg3TJoVCI7A8M6GLuEsvZ0gNyDDhjY73mR2KtXknEEA4E&quot;
      &quot;TfGS1xG0SE7g==&quot;;
  
  unsigned char public_key_content[6000] =
      &quot;-----BEGIN PUBLIC KEY-----\n&quot;
      &quot;MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0YbMPv02Danre1OGkkkR\n&quot;
      &quot;F093SREmgsZG/o+3oDYIGm8aLESt94GGdJzd97R+usprFtnKRDoo0eICAVY1NiIi\n&quot;
      &quot;m9840J9+q6MOlQJlvxQy1A3+K1Vg/XsEwRTNOR3yweC6KdVeD5ITKn4nNNBdhUdu\n&quot;
      &quot;fR80DOK7i4RjNVwYAzGYiwrkcMgBVu8BVKl/P0712Gi/58yAat9qfOvn8Q6rsy3T\n&quot;
      &quot;8YGS24KCN6xQPBt+bNXnMDBwxlzky7N2rfiv3Zid0R32PivBNCsNQmK+SP0zyJ2h\n&quot;
      &quot;TmI6VQ4GSBObfgK4dnaDfGlHfR1RNapzpd/PfdYw4XSjDF98IQJqyd83MjTXQK+a\n&quot;
      &quot;UwIDAQAB\n&quot;
      &quot;-----END PUBLIC KEY-----\n\0&quot;;
  
  void app_main() {
    int ret = 0;
    /*
     * Read the RSA public key
     */
    mbedtls_pk_context pk_public;
    mbedtls_pk_init(&amp;pk_public);
    if ((ret = mbedtls_pk_parse_public_key(&amp;pk_public, public_key_content,
                                           strlen((char *)public_key_content) +
                                               1) != 0)) {
      printf(&quot;failed: %d\n&quot;, ret);
      return;
    }
    unsigned char correct_message[] = &quot;This is a test..&quot;;
    unsigned char incorrect_message[] = &quot;This is a test!!&quot;;
  
    static unsigned char hard_signature[6000];
  
    size_t hard_signature_len = 0;
  
    mbedtls_base64_decode(hard_signature, 6000, &amp;hard_signature_len,
                          hard_base_signature,
                          strlen((char *)hard_base_signature));
  
    printf(&quot;Verify result 1: %d\n&quot;,
           mbedtls_pk_verify(&amp;pk_public, MBEDTLS_MD_MD5, correct_message,
                             strlen((char *)correct_message), hard_signature,
                             hard_signature_len)); // &lt;- Verify result 1: 0
  
    printf(&quot;Verify result 2: %d\n&quot;,
           mbedtls_pk_verify(&amp;pk_public, MBEDTLS_MD_MD5, incorrect_message,
                             strlen((char *)incorrect_message), hard_signature,
                             hard_signature_len)); // &lt;- Verify result 2: -17280
  }
  </code></pre>
  <p>NodeJS example:</p>
  <pre><code>import crypto from &quot;crypto&quot;;
  import { TextEncoder } from &quot;util&quot;;
  
  const publicKeyContent =
    &quot;-----BEGIN PUBLIC KEY-----\n&quot; +
    &quot;MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0YbMPv02Danre1OGkkkR\n&quot; +
    &quot;F093SREmgsZG/o+3oDYIGm8aLESt94GGdJzd97R+usprFtnKRDoo0eICAVY1NiIi\n&quot; +
    &quot;m9840J9+q6MOlQJlvxQy1A3+K1Vg/XsEwRTNOR3yweC6KdVeD5ITKn4nNNBdhUdu\n&quot; +
    &quot;fR80DOK7i4RjNVwYAzGYiwrkcMgBVu8BVKl/P0712Gi/58yAat9qfOvn8Q6rsy3T\n&quot; +
    &quot;8YGS24KCN6xQPBt+bNXnMDBwxlzky7N2rfiv3Zid0R32PivBNCsNQmK+SP0zyJ2h\n&quot; +
    &quot;TmI6VQ4GSBObfgK4dnaDfGlHfR1RNapzpd/PfdYw4XSjDF98IQJqyd83MjTXQK+a\n&quot; +
    &quot;UwIDAQAB\n&quot; +
    &quot;-----END PUBLIC KEY-----\n&quot;;
  
  const signature =
    &quot;Y59Z0Jg1WCKKMpKuGYW5nLsyDgPU3eoMwtLd74lejj6bWr2lPh5MKzcfXE18s6L7YG7yB18WNW&quot; +
    &quot;/meh2KAegHEDZKqy2OHf0n7bKz17OSIcoSgI9N4Eu116naVtqO5vwYJ3JGqE625JkuQza4sLZh&quot; +
    &quot;ccosbd+mNLUEBN7sr+BtlY1rU0hed56GbP6iJ+8x5XEW4HBUQ1ATQQGNu5QtqgBA+&quot; +
    &quot;jv8TktXlOVJ8XXKkOUUmVu1NU1TX+hXgNqZC6drtg5+&quot; +
    &quot;UtaMS77eF5qctMXyNKTqJ9UGTi1S0pg3TJoVCI7A8M6GLuEsvZ0gNyDDhjY73mR2KtXknEEA4E&quot; +
    &quot;TfGS1xG0SE7g==&quot;;
  const publicKey = crypto.createPublicKey({
    key: publicKeyContent,
    type: &quot;spki&quot;,
    format: &quot;pem&quot;,
  });
  
  console.log(
    &quot;Verify result 1:&quot;,
    crypto.verify(
      &quot;RSA-MD5&quot;,
      new TextEncoder().encode(&quot;This is a test..&quot;),
      publicKey,
      Buffer.from(signature, &quot;base64&quot;)
    )
  ); // &lt;- Verify result 1: false
  
  console.log(
    &quot;Verify result 2:&quot;,
    crypto.verify(
      &quot;MD5&quot;,
      new TextEncoder().encode(&quot;This is a test..&quot;),
      publicKey,
      Buffer.from(signature, &quot;base64&quot;)
    )
  ); // &lt;- Verify result 2: false
  </code></pre>
  <p>I think the issue with md5 digest - I miss it somewhere. Unfortunately, I'm very new in crypto things.
  Could you help me and point on my mistake. Thanks!</p>