[solved] Storing hash values from SHA256

I am using this code and i can not save the output of the hash function to an array of char
can u help me how to do that?

#include "sha256.h"
#include <Arduino.h>
#include "./printf.h"

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

void setup()
{ String FinalStr="1234567890";
Sha256.init();
  Sha256.print(FinalStr);
  printHash(Sha256.result());
}
void loop()
{
  
  
}

What exactly is the problem?

@AWOL

the sketch just print the values of the hash
i need to store it in a char array
i tried to check what is stored in hash but it was useless and there were different value from what is printed on the serial monitor

there was a thread discussing something similar here

i tried it with no result too

I have tried this function but it gives diff result

String GetHash256(uint8_t* hash) {
    char tmp[16];
    String sHash256 = "";

    int i;
    for (i=0; i<32; i++) {
        sprintf(tmp, "%.2X",hash[i]);
        sHash256.concat(tmp);
    }

    return sHash256 ;
}

any help plz?

I have tried this function but it gives diff result

Different from what? It would be a lot easier to help you if you showed some output.

hello paulS

when i tried saving an array directly from the function sha256.result it gives me the un encoded output of the hash
i need to save the encoded version of the hash

Please post the complete program that you tried along with the output. Then show what you want the output to look like.

mestek86:
hello paulS

when i tried saving an array directly from the function sha256.result it gives me the un encoded output of the hash
i need to save the encoded version of the hash

Then you saved the wrong data.

code

#include "sha256.h"
#include <Arduino.h>
#include "./printf.h"

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

void setup()
{Serial.begin(9600);
  String FinalStr="1234567890";
Sha256.init();
  Sha256.print(FinalStr);
  printHash(Sha256.result());
}
void loop()
{
 
 
}

output

c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646

Where in the code do you store anything in an array?

  printHash(Sha256.result());

What, EXACTLY, is the return type of result()?

    Serial.print("0123456789abcdef"[hash[i]>>4]);

In English, what, exactly, do you think that statement is doing? It seems like you want to store the same value you print, in an array. So, why don't you?

when i tried another code

#include "sha256.h"
#include <Arduino.h>
#include "./printf.h"

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

void copyHash(char* buf, uint8_t* hash) {
  int i, j = 0;
  for (i = 0; i < 32; i++) {
    buf[j++] = "0123456789abcdef"[hash[i]>>4];
    buf[j++] = "0123456789abcdef"[hash[i]&0xf];
  }
  buf[j] = 0;
}


void setup()
{Serial.begin(9600);
  String FinalStr="1234567890";
Sha256.init();
  Sha256.print(FinalStr);
  printHash(Sha256.result());
  char buf[65];
  copyHash(buf, Sha256.result());
  for (int k=0;k<65;k++)
  Serial.print(buf[k]);
}


void loop()
{
 
 
}

output

c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646
2f64612d9642afc666117f4d4fc87c17dba06ad091e1ce611f70020f2278337e

PaulS:
Where in the code do you store anything in an array?

  printHash(Sha256.result());

What, EXACTLY, is the return type of result()?

    Serial.print("0123456789abcdef"[hash[i]>>4]);

In English, what, exactly, do you think that statement is doing? It seems like you want to store the same value you print, in an array. So, why don't you?

cz i am not familiar with c++ or do not know even what to search to know how this statement is written or its meaning

finally solved

#include "sha256.h"
void setup(void)
{char encoded[64];
        Serial.begin(9600);

        // this is actually the RFC4231 4.3 test

        Sha256.init();
        Sha256.print("123");
        uint8_t * result = Sha256.result();

       
        for (int i = 0; i < 32; i++) {
                Serial.print("0123456789abcdef"[result[i] >> 4]);
                Serial.print("0123456789abcdef"[result[i] & 0xf]);
                
                for(int i = 0; i < 64; i+=2)
                    {
                      encoded[i] = "0123456789abcdef"[result[i / 2] >> 4];
                      encoded[i + 1] = "0123456789abcdef"[result[i / 2] & 0xf];
                    }
        }
        Serial.print("\n");
                for(int z = 0; z < 64; z++)
                Serial.print(encoded[z]);
}


void loop(void)
{}

this is the code that worked for me finally
thank u fo every one who tried to help me
special thanks to daknuett for his support after posting my question at his github

https://github.com/daknuett/cryptosuite2/issues/9