Using Sha1 library

I am using this sh1 function. Instead of printing the hash value I want to store it in a char array or string for further processing. Is there any way to do that.

#include “sha1.h”
#include <stdlib.h>

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

}

void loop() {

uint8_t *hash;

Sha1.init();
Sha1.print(“This is a message to hash”);
hash = Sha1.result();

for (int j=0;j<20;j++)

Serial.print(“0123456789abcdef”[hash>>4]);
Serial.print(“0123456789abcdef”[hash&0xf]);

}

Serial.println();

delay(1000);

}

I want to store it in a char array or string for further processing. Is there any way to do that.

Yes.

(please try to remember to use code tags when posting code.)

You already have it in a byte array called “hash”. Why not just use that? If you want to treat them as chars then just cast them. Byte and char are the same width.

How to do that? Can you show me in a line.

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

By the way, what's this line supposed to do? Is that supposed to concatenate the string to some value? I don't think it will work that way. That may need to be in two separate print statements. I've never seen syntax like that in a Serial.print.

Show you how to do what? You asked to put it in a char array, it is already in a byte array.

Is it the cast?

char* hashAsCharArray = (char*) hash;

But it would be much easier to just use it as is. What are you trying to do with it that you can't?

For example "855553" sha1 ouput is "d70e2fec1502abecaa5a05b6ed039369c617837e". That code print the correct value. Problem is that I need this whole output value for further processing. I can't exactly get this whole value.

For example I am getting 215 (ASCII, DEC) for hex d7. However 215 is a int value I cant use it as a char.

http://forum.arduino.cc/index.php?topic=119848.0 contain sha1 library with the exception serial.print function.

Delta_G:

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

By the way, what's this line supposed to do? Is that supposed to concatenate the string to some value? I don't think it will work that way. That may need to be in two separate print statements. I've never seen syntax like that in a Serial.print.

It just prints a single hex digit, no concatenation involved.

However 215 is a int value I cant use it as a char.

No, but you can use it as an unsigned char.

D7 hexadecimal and 215 decimal are exactly the same thing. There has been no conversion or change. They both are stored as 0b11010111. I guess I don't understand the problem.

Maybe instead of the very ambiguous, "I want to do further processing." maybe you could tell us what it is that you want to do. You obviously have some confusion as to how values are stored in a computer.

Is the problem that you need to convert this to ASCII? That isn't hard to do. There are the functions atoi and itoa that convert ascii to an integer and vice versa.

Remember that there is nothing special about an ASCII character. It is just a number. 'A' == 65 == 0x41 == 0b01000001. They're all the same to the computer.

I know this.I want to do this.

hash = Sha1.result();

Char buffer[40];

Now if I use itoa(hash,buffer,10); It give an error invalid conversion from 'uint8_t*' to 'int'

Now if I use itoa(hash,buffer,10); It give an error
Code: [Select]

invalid conversion from 'uint8_t*' to 'int'

I don't like guessing games.
Show us the code.

#include "sha1.h"
#include <stdlib.h>

char buffer [33];


void setup(){
  Serial.begin(9600);
  
}

void loop() {
 
  uint8_t *hash;
  
  Sha1.init();
  Sha1.print("This is a message to hash");
  hash = Sha1.result();
 
  itoa(hash,buffer,10);
     
   
  }
 
  Serial.println();
 
 delay(1000);
  
}

http://www.cplusplus.com/reference/cstdlib/itoa/

want to store it in a char array or string for further processing. Is there any way to do that.

Have you thought about a simple memcpy?

No. Can I get a example?

faisaliut:
No. Can I get a example?

There was one on the page I linked.

I think I got it. He wants an ascii representation of the string in hex format. Why he didn’t just start out saying that is beyond me.

itoa would be the wrong function then since it would give it to you in decimal.

I don’t know of a similar version that spits out hex in ascii, but this should do the trick.

char digitArray[]= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

char buffer[40];


for (int i = 0; i<40; i+=2){
  
  buffer[i] = digitArray[(hash[i/2] & 0xF0) >> 4];
  buffer[i+1] = digitArray[(hash[i/2] & 0x0F)];
}

Disclaimer: Totally untested code that I “think” will work. It splits each byte into two parts and uses the lookup table digitArray to find the right ascii digit to fill in.

Delta_G:
I think I got it. He wants an ascii representation of the string in hex format. Why he didn’t just start out saying that is beyond me.

itoa would be the wrong function then since it would give it to you in decimal.

I don’t know of a similar version that spits out hex in ascii, but this should do the trick.

char digitArray[]= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

char buffer[40];

for (int i = 0; i<20; i+=2){
 
  buffer[i] = digitArray[(hash[i/2] & 0xF0) >> 4];
  buffer[i+1] = digitArray[(hash[i/2] & 0x0F)];
}




Disclaimer: Totally untested code that I "think" will work. It splits each byte into two parts and uses the lookup table digitArray to find the right ascii digit to fill in.

It’s a string, Jim, but not as we know it.

Finally I manage to make you understand. Thanks Delta_G for your cooperation. I still didn't run the code. Hope it will work

faisaliut:
Finally I manage to make you understand.

It would have helped if instead of being ambiguous and vague about what you wanted to do you had simply said, I need this string of data converted to ascii in hex format. Starting out all you said was that you wanted it in a char array which for all intents and purposes it already was.