Patryk
Patryk

Reputation: 24120

How to convert SHA1 to array[5] of unsigned long ints?

For the purposes of library/subsystem that I use I have to convert SHA1 (calculated with use of e.g. opensll) to 5 element array of unsigned long ints (32 bits variables) or create the aforementioned SHA1-5-long-array by myself.

Reason:

SHA1 (160 bits) = 5 x unsigned long int (32 bits)

I think that first solution will be better so here is my question: how should I go around this task? Read byte/bit by byte/bit and then create unsigned long ints from it and put it in the array or is there a different solution?

Upvotes: 0

Views: 427

Answers (1)

dmg
dmg

Reputation: 7716

SHA1 produces a 20 byte hash-value. In openssl it returns an unsigned char*. I'm guessing you can use a union of unsigned char[20] and uint32_t[5] and use the chars for easy byte access:

union mysha1{
    uint32_t shaint[5];
    unsigned char shachar[20];
};

Add to that a bunch of operators (indexing for example) and you are good to go.

If you wanna keep it crude and simple you can do a memcpy between the SHA1 output and your uint32_t[5], though.

Upvotes: 2

Related Questions