Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Little endian data and sha 256

I have to generate sha256 hashes of data that is in little endian form. I would like to know if I have to convert it to big endian first, before using the sha 256 algorithm. Or if, the algorithm is "endian-agnostic".

EDIT: Sorry, I think I wasnt clear. What I would like to know is the following: The sha256 algorithm requires to pad the end of a message with certain bits. The first step is to add a 1 at the end of the message. Then, to pad it with zero up to the end. At the very end, you must add the length of the message in bits. What I would like to know is if this padding can be performed in little endian. For example, for a 640 bit message, I could write the last word as 0x280 (in big endian), or 0x8002000 (in little endian). Can this padding be done in little endian?

like image 360
Anon21 Avatar asked Jun 07 '11 18:06

Anon21


People also ask

What is SHA-256 algorithm used for?

SHA-256 stands for Secure Hash Algorithm 256-bit and it's used for cryptographic security. Cryptographic hash algorithms produce irreversible and unique hashes. The larger the number of possible hashes, the smaller the chance that two values will create the same hash.

What is little endian storage data format?

What Does Little-Endian Mean? The little-endian convention is a type of addressing that refers to the order of data stored in memory. In this convention, the least significant bit (or "littlest" end) is first stored at address 0, and subsequent bits are stored incrementally.

How is little endian memory stored?

Little Endian Byte Order: The least significant byte (the "little end") of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the next three bytes in memory.

Can SHA-256 be used for compression?

SHA-256 is an iterated hash function producing a 256-bit output. The SHA-256 compression function takes a 512-bit message block and a 256-bit intermediate hash value as an input and produces a 256-bit new intermediate hash value as an output. The algorithm of the SHA-256 compression function is described below.


2 Answers

SHA256 is endian-agnostic if all you want is a good hash. But if you are writing SHA256 and want to the same results with a correct implementation then you must play games on little endian hardware. SHA256 combines arithmetic addition (mod 2*32) and boolean operation thus is not endian-agnostic internally.

like image 149
Peter Butler Avatar answered Sep 28 '22 00:09

Peter Butler


The SHA-256 implementation itself should take care of padding - you shouldn't have to deal with that unless you're implementing your own specialized SHA-256 code. If you are, note that the padding rules specified in the "pre-processing step" say that the length is a 64-bit big-endian integer. See SHA-2 - Wikipedia

It's hard to even figure out what "endian-agnostic" would mean, but the order of all the bits, bytes and words for a hash algorithm matter a whole lot, so I sure wouldn't use that term.

like image 36
nealmcb Avatar answered Sep 28 '22 01:09

nealmcb