Question

I realized converting a String into a hexarray now need to convert the new array into a new string,because the function Sha256.write needs a char, which would be the way?

    char hexstring[] = "020000009ecb752aac3e6d2101163b36f3e6bd67d0c95be402918f2f00000000000000001036e4ee6f31bc9a690053320286d84fbfe4e5ee0594b4ab72339366b3ff1734270061536c89001900000000";
    int i;
    int n;

    uint8_t  bytearray[80];
    Serial.println("Starting...");
    char tmp[3];
    tmp[2] = '\0';
    int j = 0;

    //GET ARRAY
    for(i=0;i<strlen(hexstring);i+=2) {
        tmp[0] = hexstring[i];
        tmp[1] = hexstring[i+1];
        bytearray[j] = strtol(tmp,0,16);
        j+=1;
     }

    for(i=0;i<80;i+=1) {
       Serial.println( bytearray[i]);
     }


      int _batchSize;
      unsigned char hash[32];
      SHA256_CTX ctx;
      int idx;
      Serial.println("SHA256...");
      for(_batchSize = 100000; _batchSize > 0; _batchSize--){
         bytearray[76] = nonce;
          // Sha256.write(bytearray);
         sha256_init(&ctx);
         sha256_update(&ctx,bytearray,80);
         sha256_final(&ctx,hash); //
         sha256_init(&ctx);
         sha256_update(&ctx,hash,32);
         sha256_final(&ctx,hash); //are this corrent? i'm need in bytes too        
       //  print_hash(hash);
         int zeroBytes = 0;
         for (int i = 31; i >= 28; i--, zeroBytes++)
             if(hash[i] > 0)
                break;
             if(zeroBytes == 4){ // SOLUTION TRUE, NOW I'M NEED THIS IN STRING
                printf("0x");
                for (n = 0; n < 32; n++)
                   Serial.println(printf("%02x", hash[n]));  //ERROR :(       
              }
         //increase
         if(++nonce == 4294967295)
            nonce = 0;

      }      

  }


}   

output array on Serial port:

2
0
0
0
158
203
117
42
172
62
109
33
1
22
59
54
243
230
189
103
208
201
91
228
2
145
143
47
0
0
0
0
0
0
0
0
16
54
228
238
111
49
188
154
105
0
83
50
2
134
216
79
191
228
229
238
5
148
180
171
114
51
147
102
179
255
23
52
39
0
97
83
108
137
0
25
0
0
0
0

how to convert this to a hexstring char back?

UPDATED

this solutions works for me, thanks all!

void printHash(uint8_t* hash) {
  int id;
  for (id=0; id<32; id++) {
    Serial.print("0123456789abcdef"[hash[id]>>4]);
    Serial.print("0123456789abcdef"[hash[id]&0xf]);
  }
  Serial.println();
}
Was it helpful?

Solution

Skip to the section Addressing your code... at bottom for most relevant content
(this stuff up here is barely useful blither)

The purpose of your function:

Sha256.write((char *)bytearray);

I believe is to write more data to the running hash. (from this) Therefore, I am not sure in the context of your question how to convert this to a hex-string char back? how this relates to the way you are using it.

Let me offer another approach for the sake of illustrating how you might go about returning the array of ints back into the form of a "hexadecimal string":

From Here

Here is a code fragment that will calculate the digest for the string "abc"

   SHA256_CTX ctx;
   u_int8_t results[SHA256_DIGEST_LENGTH];
   char *buf;
   int n;

   buf = "abc";
   n = strlen(buf);
   SHA256_Init(&ctx);
   SHA256_Update(&ctx, (u_int8_t *)buf, n);
   SHA256_Final(results, &ctx);

   /* Print the digest as one long hex value */
   printf("0x");
   for (n = 0; n < SHA256_DIGEST_LENGTH; n++)
           printf("%02x", results[n]);
   putchar('\n'); 

resulting in:

"0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad".   

In this example The array I believe you want, is contained in u_int8_t results

There is not enough description in your post to be sure this will help, let me know in the comments, and I will try to address further questions.

Added after your edit:

Continuing from the example above, to put the array contents of results back into a string, you can do something like this:

char *newString;
newString = malloc(sizeof(char)*SHA256_DIGEST_LENGTH*2);
memset(newString, 0, sizeof(char)*SHA256_DIGEST_LENGTH*2);
strcat(newString, "0x");
for(i=0;i<SHA256_DIGEST_LENGTH;i++)
{
    sprintf(newString, "%s%02x\n", newString, results[i]);
}
//use newString for stuff...
free(newString);  

Addressing your code, and your question directly:

Your code block:

  for(_batchSize = 100000; _batchSize > 0; _batchSize--){
     bytearray[76] = _batchSize;
     Sha256.write((char *)bytearray); //here are the error
  }    

is not necessary if all you want to do is to convert an array of int into a "hexadecimal string"

Your int array, defined as:

int bytearray[80];  

Already contains all the necessary values at this point, as you illustrated with your latest edit. If you want to return this data to a "hexadecimal string" form, then this will do that for you: (replacing result with your bytearray)

char *newString;
newString = malloc(sizeof(char)*SHA256_DIGEST_LENGTH*2);//if these defines are not in your environment
memset(newString, 0, sizeof(char)*SHA256_DIGEST_LENGTH*2);//then replace them with appropriate value for length
strcat(newString, "0x");
for(i=0;i<sizeof(bytearray)/sizeof(bytearray[0]);i++)
{
    sprintf(newString, "%s%02x\n", newString, bytearray[i]);
}
//use newString for stuff...
free(newString);  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top