0

How to use hashing function

void sha2( const unsigned char *input, int ilen,
           unsigned char output[32] )
{
    sha2_context ctx;

    sha2_starts( &ctx );
    sha2_update( &ctx, input, ilen );
    sha2_finish( &ctx, output );

    memset( &ctx, 0, sizeof( sha2_context ) );
}

I tried

sha2(InpData, sizeof(InpData), OutData)

but as a result I get trashed data.

2
  • What's the point of the memset? Commented May 17, 2013 at 17:26
  • @CarlNorum The only point of the memset would be not to leave SHA-2 context on the stack that could be used to reverse engineer the original string. The hash is resilient to reversing, the context state may not be. Of course in this case there's the whole issue of the original string not being overwritten too... :) Commented May 17, 2013 at 17:33

2 Answers 2

1

Without seeing the declaration of InpData we can't know for sure, (Please, post a short, compilable example), but the probability is that InpData is a pointer of some sort, as opposed to an array. The sizeof() operator, when applied to a pointer, will not evaluate to the size of the pointed-at allocated storage, but to the size of the pointer: typically 4 or 8 bytes.

Sign up to request clarification or add additional context in comments.

Comments

0

Just call it with the data, the length and the buffer where you want your output;

char* data = "my data to hash";
int datalen = strlen(data);       // or if binary data, the size of your input
unsigned char buffer[32];         // The buffer to receive the result

sha2(data,datalen,buffer);

Note that the data received in the buffer is binary data (32*8=256 bits if it's SHA-256), so you'll want to hex/base64/etc. encode it before displaying the result to a screen.

EDIT: Extremely simple and hackish hex encoding entirely without clean code points :)

char* hexencode(void* data, int len)
{
  char* outdata = malloc(len*2+1);
  char* tmp = outdata;
  unsigned char* indata = (char*)data;
  while(len--)
  {
    *tmp++ = "0123456789abcdef"[(*indata)>>4];
    *tmp++ = "0123456789abcdef"[(*indata++)&0xf];
  }
  *tmp = 0;
  return outdata;
}

7 Comments

it dosent help, still getting trash data but a little bit longer im getting : ĹWç=pÓK1Ău!SŕĘDđˇá; ŞR ň|éG?-RcşáUË6ŕÚíÄÇÎ]«ts While correct hash is : d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
Read the note at the bottom, your desired result is hex encoded binary, while the result from sha2 is just plain binary.
How to simply encode it to asci in C, sry for newbie ask, im new in C?
@user2304765 Added a simple hex encoding. If you're not getting the correct result, we need to know what you're trying to encode to try to repeat the result, so you'll need to add some code.
still dosent work, here are warnings wklej.to/JuJ2f , source: wklej.to/ljb6k
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.