title: Nonce function of blockchain in C. Concatenation of unsigned char array in C
body: <p>I try to add a nonce to my blockchain program. But when I test a program performance by trying to repeat the result (I need to be able to do to verify my chain), I do not get an identical result.</p>
  
  <p>First I have a function which transform a <code>structure</code> to <code>unsigned array pointer</code> :</p>
  
  <pre><code>struct  Test{
      unsigned char data[4];
      unsigned char nonce[4];
      unsigned char hash[32];
  }*prev,*next; 
  
  unsigned char *toStringTest(struct Test data)
  {
      unsigned char *str=malloc(sizeof(unsigned char)*sizeof(data));
      memcpy(str,&amp;data,sizeof(data));
      return str;
  }
  </code></pre>
  
  <p>Then, I have a program which give me a hash and nonce:</p>
  
  <p>In this function:</p>
  
  <ol>
  <li>I concatenated unsigned char pointer from <code>toStringTest()</code> with unsigned char arrat <code>nonce</code>.</li>
  <li>I calculated hash of this concatenation.</li>
  <li>If the hash starts by <code>0x00</code> I save hash and nonce into <code>next</code> block. If not, I repeat the function.  </li>
  </ol>
  
  <pre><code>void hash_with_nonce(struct Test* message,struct Test* new_message){
      unsigned char nonce[4]; //number only used once
      unsigned char buffer[32];
      while(1){
          RAND_bytes(nonce, 4); //this function puts 4 cryptographically strong pseudo-random bytes into nonce. 
  
          unsigned char* str=toStringTest(*message); 
  
          int len = sizeof(unsigned char)*sizeof(*str)+sizeof(unsigned char)*sizeof(nonce);
          unsigned char* message_with_nonce = malloc(len);  
          memcpy(message_with_nonce,str,sizeof(*str));
          memcpy(message_with_nonce+sizeof(unsigned char)*sizeof(*str),nonce,sizeof(nonce));
          //I concatenated toStringTest(*message) with nonce
  
          SHA256(message_with_nonce, sizeof(message_with_nonce), buffer); //calculation of hash
  
          free(message_with_nonce);   
          unsigned char var[1] = {0x00}; //rule for nonce decision, I want what hash start by 0x00
          if((int *)var[0] == (int *)buffer[0]){ 
              memcpy(new_message-&gt;hash,buffer, 32); 
              memcpy(new_message-&gt;nonce, nonce,sizeof(nonce));
              return;
          }
      }
  
  }
  </code></pre>
  
  <p>This is my main:</p>
  
  <pre><code>int main(int argc, char **argv)
  {
      unsigned char hash[32];
  
      prev=malloc(sizeof(struct Test));
      RAND_bytes(prev-&gt;data, 4);
      RAND_bytes(prev-&gt;nonce, 4);
      SHA256("",sizeof(""),prev-&gt;hash);
  
      next=malloc(sizeof(struct Test));
      RAND_bytes(next-&gt;data, 4);
      //I just have filled this block with random data  
  
      hash_with_nonce(prev,next);
  
  
      unsigned char* str=toStringTest(*prev); 
  
      int len = sizeof(unsigned char)*sizeof(*str)+sizeof(unsigned char)*sizeof(next-&gt;nonce);
      unsigned char* message_with_nonce = malloc(len);  
  
      memcpy(message_with_nonce,str,sizeof(*str));
      memcpy(message_with_nonce+sizeof(unsigned char)*sizeof(*str),next-&gt;nonce,sizeof(next-&gt;nonce));
      SHA256(message_with_nonce, sizeof(message_with_nonce), hash);
  }
  </code></pre>
  
  <p>prev and next are just 2 blocks which I use to check if function <code>hash_with_nonce</code> is working.</p>
  
  <p>A problem is that the <code>unsigned char hash[32]</code> of the main is not the identical to <code>next-&gt;hash</code>. SHA256() and RAND_bytes() are openssl functions.  </p>
  
  <p>To check if 2 hashs are same, I have this function:</p>
  
  <pre><code>void PrintHex(unsigned char data[], int size) 
  {
      unsigned char tmp[size];
      for (int i=0; i&lt;size; i++)
      { 
          sprintf(tmp, "%02x",data[i]); 
          printf("%s", tmp);
      }
      printf("\n");
  }
  </code></pre>