Go Down

Topic: [solved] Storing hash values from SHA256 (Read 395 times) previous topic - next topic

mestek86

Feb 10, 2019, 12:48 am Last Edit: Feb 11, 2019, 01:02 am by mestek86
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?

Code: [Select]


#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()
{
  
  
}




AWOL


mestek86

@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

mestek86

there was a thread discussing something similar here


https://forum.arduino.cc/index.php?topic=506340.30


i tried it with no result too

mestek86

I have tried this function but it gives diff result

Code: [Select]

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?

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

mestek86

#6
Feb 10, 2019, 09:15 pm Last Edit: Feb 10, 2019, 09:16 pm by 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

gfvalvo

Please post the complete program that you tried along with the output. Then show what you want the output to look like.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

PaulS

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.
The art of getting good answers lies in asking good questions.

mestek86

code
Code: [Select]



#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

PaulS

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

Code: [Select]
  printHash(Sha256.result());
What, EXACTLY, is the return type of result()?

Code: [Select]
    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?
The art of getting good answers lies in asking good questions.

mestek86

when i tried another code

Code: [Select]



#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

mestek86

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

Code: [Select]
  printHash(Sha256.result());
What, EXACTLY, is the return type of result()?

Code: [Select]
    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

mestek86

finally solved

Code: [Select]

#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](/daknuett) for his support after posting my question at his github

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

Go Up