简体   繁体   中英

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 eg 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM