title: C++ - OpenSSL secp256k1 ECDSA - Can't verify using exact copy of hash, whether using string literals or different objects
body: <p>I am trying to sign a hash, create a copy of that hash (specifically client/server related), then verify the signature using the copy of the hash. I don't understand how or why when I have two variables, <code>hash</code> and <code>hashCopy</code>, with the exact same content, that verification can only work on one of the two depending on which one was signed.</p>
  <p>The below code shows what I am talking about.</p>
  <pre><code>int main()
  {
      EC_KEY* myecc = NULL;
      EVP_PKEY* pkey_ = NULL;
  
      myecc = EC_KEY_new_by_curve_name(NID_secp256k1);
      EC_KEY_generate_key(myecc);
      pkey_ = EVP_PKEY_new();
      EVP_PKEY_assign_EC_KEY(pkey_, myecc);
      myecc = EVP_PKEY_get1_EC_KEY(pkey_);
      const EC_GROUP* ecgrp = EC_KEY_get0_group(myecc);
  
      std::string hash = &quot;126420fb81d58bbb7d86c98e1818f4c9d44acf216471e196ed403a608954cf1d&quot;;
      std::string hashCopy = hash;
  
      std::vector&lt;unsigned char&gt; signature;
      unsigned char pchSig[10000];
      unsigned int nSize = 0;
  
      ECDSA_sign(0, (unsigned char*)&amp;hash, sizeof(hash), pchSig, &amp;nSize, myecc);
      signature.resize(nSize);
      memcpy(&amp;signature[0], pchSig, nSize);
  
      if (ECDSA_verify(0, (unsigned char*)&amp;hash, sizeof(hash), &amp;signature[0], signature.size(), myecc) == 1)
      {
          std::cout &lt;&lt; &quot;Original hash variable verify success\n\n&quot;;
      }
      else
      {
          std::cout &lt;&lt; &quot;Original hash variable verify fail\n\n&quot;;
      }
      if (ECDSA_verify(0, (unsigned char*)&amp;hashCopy, sizeof(hashCopy), &amp;signature[0], signature.size(), myecc) == 1)
      {
          std::cout &lt;&lt; &quot;Hash copy variable verify success\n\n&quot;;
      }
      else
      {
          std::cout &lt;&lt; &quot;Hash copy variable verify fail\n\n&quot;;
      }
  } 
  </code></pre>
  <p>This outputs:</p>
  <pre><code>Original hash variable verify success
  
  Hash copy variable verify fail
  </code></pre>
  <p>Why does verifying using <code>hashCopy</code> fail given that it's the exact same? This variable basically represents the variable on the client side, whereas the original <code>hash</code> variable represents the variable on the server side.</p>
  <p>Also, if I just pass through a string literal <code>&quot;1264...&quot;</code> which is the exact same as <code>hash</code> that does not work either.</p>
  <p>I also tried this using a copy of the <code>signature</code> variable, that made no difference as I would expect.</p>
  <p>How do I get the verification of both <code>hash</code> and <code>hashCopy</code> to work without needing to sign both?</p>