Compute SHA256 of string and output to string In my Arduino project I want to compute the SHA256 hash of a string and store the result to a string, and do this recursively many times (i.e. compute hash of hash of hash etc...). So my goal is to have a reliable function as follows:


String h(String input) {
...
return output;
}


where for example h("abc") would return "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad".

I downloaded this library 
(from Tools->Manage libraries, it is also in github) and then ran the example in TestSHA256.ino which passes all the checks. However I'm stuggling to make my function work since the library doesn't have a specific example on how to compute and return a SHA256 hash.

I have been looking in function testHash_N() in TestSHA256.ino, which appears to create a hash (from struct TestHashVector data) and compare it with the precomputed hash (from struct TestHashVector hash[]).

The hash seems to be computed in lines 89-96 in TestSHA256.ino.  I just want to take the actual hash string result, so for testVectorSHA256_1  in the example, I would need to have a string = "ba7816bf....15ad" as the result.

I understand that this is related to memory referencing in Arduino but I'm not experienced so I'd be grateful if you helped me with this.

EDIT: My code snippet so far is as follows, but it doesn't output the correct hash..

#include &lt;Crypto.h&gt;
#include &lt;SHA256.h&gt;
#include &lt;string.h&gt;

#define HASH_SIZE 32
#define BLOCK_SIZE 64

char hex[256];

char *hashvalue;

SHA256 sha256;

byte buffer[128];


char *btoh(char *dest, uint8_t *src, int len) {
  char *d = dest;
  while( len-- ) sprintf(d, "%02x", (unsigned char)*src++), d += 2;
  return dest;
}

char* h(Hash *hash, char* hashvalue)
{
    size_t size = sizeof(&amp;hashvalue);
    size_t posn, len;
    size_t inc = sizeof(&amp;hashvalue);
    uint8_t value[HASH_SIZE];

    hash-&gt;reset();
    for (posn = 0; posn &lt; size; posn += inc) {
        len = size - posn;
        if (len &gt; inc)
            len = inc;
        hash-&gt;update(&amp;hashvalue + posn, len);
    }
    hash-&gt;finalize(value, sizeof(value));

    return(btoh(hex, value, 32));
}


void setup()
{
    Serial.begin(115200);

    bool tmp;
    char *testvalue = "abc";
    Serial.println(h(&amp;sha256,*testvalue));


}

void loop()
{
}
I suggest the following function. That replaces a string by its hash.
It's done in place in order to save memory. Make sure the buffer you
give it is at least 65&nbsp;bytes long. Warning: not tested.

#include &lt;Crypto.h&gt;
#include &lt;SHA256.h&gt;

#define HASH_SIZE 32

SHA256 sha256;

/*
 * Replace a string by its hash, in place.
 * `data' should point to a buffer at least 65 bytes in length.
 */
hash_in_place(char *data)
{
    uint8_t value[HASH_SIZE];

    sha256.reset();
    sha256.update(data, strlen(data));
    sha256.finalize(value, sizeof(value));

    // Convert to hex into the original buffer.
    data[0] = '\0';
    for (size_t i = 0; i &lt; HASH_SIZE; i++) {
        data += sprintf("%02x", value[i]);
    }
}