Converting sha256 resultant hash into equivalent decimal number string.

I am developing a secure communication between android and arduino with sha256. Doing so i want to hash first the input of the user and then convert each hash[0](up tp hash[31]) into decimal so that at the end i have along string of of total hashe pieces like this 21de5812f71df17b25a5124129ba1ccf5ba67121c548c9f8127f68d9d11293f8 and further id possible i want to store it in EPROM.I searched out a lot bud did not succed.Any help would be appreciated and thanks in advance.this is my code

#include <SoftwareSerial.h>
#include <sha256.h>
//#include <SD.h>
#include <EEPROM.h>
#include <Servo.h>
//#include <Encoder.h>
#include <avr/sleep.h>

#define KEY_LENGTH 32
#define EPROM_GEN_CONFIG_ADDR 0
#define EPROM_LOCK_KEY_ADDR 1
#define EPROM_UNLOCK_KEY_ADDR 33
#define HC_05_TXD_ARDUINO_RXD 2
#define HC_05_RXD_ARDUINO_TXD 3

SoftwareSerial BTSerial(HC_05_TXD_ARDUINO_RXD, HC_05_RXD_ARDUINO_TXD);
byte value;
char paddedKey[KEY_LENGTH];
char rpaddedKey[KEY_LENGTH];
void setup() {
   Serial.begin(9600);
   BTSerial.begin(9600);
  // put your setup code here, to run once:
if(EEPROM.read(EPROM_GEN_CONFIG_ADDR)!=1){
    Serial.println("Please enter the master key");
    while(!Serial.available()){
      delay(50);
    }
    for(int i=0;i<KEY_LENGTH;i++){
      paddedKey[i]=(char)Serial.read();
      delay(50);
      Serial.print(paddedKey[i]);
    }
   
    Serial.println();
    String toHash(paddedKey);
     uint8_t *hash;
     Sha256.init();
     Sha256.print(toHash);
     hash = Sha256.result();
   //  writeKeyToEEPROM(hash,EPROM_LOCK_KEY_ADDR);
   // EEPROM.write(EPROM_GEN_CONFIG_ADDR,1); 
           
  for(int i=0;i<KEY_LENGTH;i++){
     Serial.print(hash[1],HEX);
     EEPROM.write(EPROM_LOCK_KEY_ADDR+i,hash[i]); 
    }  
}

void loop() {
  // put your main code here, to run repeatedly:
 if (BTSerial.available()) {
  Serial.println("Data");
  while(BTSerial.available()>0){
  char c=(char)BTSerial.read();
  Serial.print(c);
  }
  }
  delay(100);
}

Please read and follow the instructions in the "How to use this forum" post.
Post ALL the code (not a snippet) and describe what it is supposed to do, and what it does instead.

Do not double post.

i posted all my code sir i apologized for did silly mistakes...all i have to convert the from hash[0] to hash[31] in to decimal string like this one which is hexadecimal string

21de5812f71df17b25a5124129ba1ccf5ba67121c548c9f8127f68d9d11293f8

all i have to convert the from hash[0] to hash[31] in to decimal string like this one which is hexadecimal string

Why? It will take less space to store the 32 elements of the hash array in EEPROM than to store all those characters.

mtg:
iall i have to convert the from hash[0] to hash[31] in to decimal string like this one which is hexadecimal string

21de5812f71df17b25a5124129ba1ccf5ba67121c548c9f8127f68d9d11293f8

I don't understand why you want a 'decimal' representation. Decimal will take about 50% more space to store than hexadecimal which itself will take twice as much space as storing in raw form.

P.S. Which SHA256 library are you using?

this is the lib that i want to use.
Also i dont know the length of each resultant hash[0]......hash[31].
Want i want actually is to store the resultant hash[0]....hash[31] in EPROM and then on demand i have to retrieve it from EPROM in hexadecimal format...on the android side i get the hash of same string in hexadecimal format but when i obtain the same string hash in arduino it quite different from android one.
Please help...

Also i dont know the length of each resultant hash[0]......hash[31].

Yes, you do. It's 32 elements. Each element is one byte.

could you tell me sir in which format the byte is mean is in integer or char or in which format each hash[] byte is.
i convert each hash[] to HEX through serial.print but this one is different one that is obtain in android...i thing i am doing some thing wrong either at arduino side or android side.

This is how you would store your 32 byte array in EEPROM

#include <EEPROM.h>
#include "sha256.h"

uint8_t hashedSecret[32];  //Variable to store in EEPROM
const uint16_t hashAddress = 0;      //Location we want the data to be put

void setup() {
  // generate hash here
  Sha256.init();
  Sha256.print("This is a message to hash");
  memcpy(hashAddress, Sha256.result(),32); // get a local copy for later use (EDIT THIS IS A BOFUS LINE... READER EXERCISE TO FIX)

  EEPROM.put(hashAddress, hashedSecret); //  //One simple call to save it

  // when you will want to read it from memory
  // EEPROM.get(hashAddress, hashedSecret); //  //One simple call to get it back
}

void loop() {}

.

mtg:
GitHub - Cathedrow/Cryptosuite: Cryptographic suite for Arduino (SHA, HMAC-SHA)
this is the lib that i want to use.

In your code you are placing the resultant hash in a String object constructed from a pointer to an array of bytes. This will store bytes of the hash up to the first byte containing zero which could occur anywhere. It may be before the end of the hash in which case you will have a truncated result or it may be after the end of the hash in which case you you will have apparently random bytes (their value will depend on what else is going on in your code ) appended to your String.

The library does not use the String class. Do not use the String class. Modern cryptography works with bytes and not directly with characters. For the task you have described you most definitely do not want characters and do not need characters. You need bytes or uint8_t .

#include <EEPROM.h>
#include "sha256.h"
uint8_t hashedSecret[32];  //Variable to store in EEPROM
const uint16_t hashAddress = 0;      //Location we want the data to be put

void setup() {
 Serial.begin(9600);
  // generate hash here
  Sha256.init();
  Sha256.print("This is a message to hash");
  memcpy(hashAddress, Sha256.result(),32); // get a local copy for later use 

  EEPROM.put(hashAddress, hashedSecret); //  //One simple call to save it

  // when you will want to read it from memory
  // EEPROM.get(hashAddress, hashedSecret); //  //One simple call to get it back

     for(int i=0;i<32;i++){
    Serial.print(hashedSecret[i]);
   }
}
void loop() {}

As i stores the hase into EProm and get it back which is stored in hashedSecret and now i want to display it in serial so for that i use for loop but i have no data mean my serial print show nothing.
Please guide me i think i am doing some thing wrong.
Thank you so much for all the members of this forum.

Try with

for(int i=0;i<32;i++){
    Serial.print(hashedSecret[i],HEX);Serial.print(“-“);
}

Or better to get all binary data on 2 hex digits

for(int i=0;i<32;i++){
   if (hashedSecret[i] < 0x10) Serial.print(0);
   Serial.print(hashedSecret[i], HEX);// EDIT CORRECTION 
}

Again sir no result in Serial monitor...Nothing is showing in Serial monitor.

#include <EEPROM.h>
#include "sha256.h"
uint8_t hashedSecret[32];  //Variable to store in EEPROM
const uint16_t hashAddress = 0;      //Location we want the data to be put

void setup() {
 Serial.begin(9600);
 // generate hash here
  Sha256.init();
  Sha256.print("This is a message to hash");
  memcpy(hashAddress, Sha256.result(),32); // get a local copy for later use 
  EEPROM.put(hashAddress, hashedSecret); //  //One simple call to save it
  // when you will want to read it from memory
  EEPROM.get(hashAddress, hashedSecret); //  //One simple call to get it back
for(int i=0;i<32;i++){
    Serial.print(hashedSecret[i],HEX);Serial.print("-");
 } 
}
void loop() {}

What’s the speed you setup for the serial console? 9600?
What arduino do you have?

const uint16_t hashAddress = 0;      //Location we want the data to be put
//...
  memcpy(hashAddress, Sha256.result(),32); // get a local copy for later use

Seems to me not so clever.

Whandall:

const uint16_t hashAddress = 0;      //Location we want the data to be put

//...
  memcpy(hashAddress, Sha256.result(),32); // get a local copy for later use




Seems to me not so clever.

Curious why? (Don’t know the library but had the impression Sha256.result was returning a pointer to a static buffer that would be overwritten at the next call)

ÉDIT - with my glasses on see indeed why it’s stupid :slight_smile:
Copy paste error with hashedSecret

let’s call that an on purpose mistake to see if OP was following :slight_smile:

void * memcpy ( void * destination, const void * source, size_t num );

Copy block of memory
Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.

Zero is the address of the 32 general purpose registers IIRC, which will be overwritten with a hash.

Do you think that is clever?

Yeah - don’t remind me I need glasses :slight_smile:

I used the same baud rate in coding and in serial monitor.
I playing for the first with EEPROM and pointers that's if i made any silly mistakes then i apologized for it strongly.

HI
I successfully hash a string and stored it in EEPROM and then show it in Serial.monitor as HEX values like a long String

21DE5812F71DF17B25A5124129BA1CCF5BA67121C548C9F8127F68D9D11293F8

Now i want to store the Serial.print(value,HEX) in a string or in an array.i searched out a lot but did not get any success.
Here is my arduino code.Any help would appreciated.

#include <EEPROM.h>
#include "sha256.h"
uint8_t hashedSecret[32];  
uint8_t *hashAddress = 0;   

void setup() {
 Serial.begin(9600);
 // generate hash here
  Sha256.init();
  Sha256.print("12345678901234567890123456789077");
  hashAddress=Sha256.result();
   for(int i=0;i<32;i++){
    EEPROM.write(i,*hashAddress);
    hashAddress++;
   }
for(int i=0;i<32;i++){
  *hashAddress=EEPROM.read(i);
    Serial.print(*hashAddress,HEX);
 } 
}
void loop() {}