SHA256 libary help

I have installed the Arduino cryptography library
https://rweather.github.io/arduinolibs/classSHA256.html
(from Tools->Manage libraries)
I also see it here arduinolibs/libraries/Crypto at master · rweather/arduinolibs · GitHub

I downloaded and ran the example from here

And verifications pass.

I'm struggling with this code though and the example isn't really an example and doesn't show its usage. All I want is to define my own function to hash a string and return a string representing its SHA256 hash.

E.g.

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

Can someone help me with this?

The function void testHMAC(Hash *hash, size_t keyLen) in that example appears to do just that, except for actually returning the result.

Lack of comments doesn't make it easy to understand what that function really does, though.

So you suggest to try and construct the output in similar way to lines 166-181 in TestSHA256.ino? Can you give a specific example because I'm not very much experienced with C language

No specific example - I'm also just looking at the code you posted.

It appears to create a hash, store that in the variable result, and do a comparison on that. You have to read the code more carefully, and reference to the manual of the library itself to confirm that's indeed what those functions do.

Ok -
So I was struggling with the code inside function testHash_N() in TestSHA256.ino, which does exactly that: 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. But this creates a reference to memory or something like that. 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..
Thanks for helping

Here's the code I have so far but it doesn't work

 #define OUTPUT_MAX 32

 String h(String& input) { 
 char output[OUTPUT_MAX];
 char* data = input.c_str();  

 Hash *hash;
 int posn, len;
 int size = input.length();
 int inc = input.length();
 uint8_t value[HASH_SIZE];
 hash->reset();
 for (posn = 0; posn < size; posn += inc) {
       len = size - posn;
       if (len > inc)
           len = inc;
       hash->update(input + posn, len);
   }
   hash->finalize(value, sizeof(value));

 return String(output);     
}

My updated code, it compiles but it outputs wrong hash..

#include <Crypto.h>
#include <SHA256.h>
#include <string.h>

#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(&hashvalue);
   size_t posn, len;
   size_t inc = sizeof(&hashvalue);
   uint8_t value[HASH_SIZE];

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

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


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

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


}

void loop()
{
}

Please read the sticky on how to post code, using code tags.

To return the hash (aa char array) you have to return it as pointer. Not through the return statement.