John Smith
John Smith

Reputation: 8851

Is it possible to deal with individual bits in C#? Trying to implement a SHA256 generator

Just doing this for fun and I was reading the pseudo-code on Wikipedia and it says when pre-processing to append the bit '1' to the message and then append enough '0' bits to the resulting message length modulus 512 is 448. Then append the length of the message in bits as a 64-bit big-endian integer.

Okay. I'm not sure how to append just a '1' bit but I figure it could be possible to just append 128 (1000 0000) but that wouldn't work in the off chance the resulting message length modulus 512 was already 448 without all those extra 0's. In which case I'm not sure how to append just a 1 because I'd need to deal with at least bytes. Is it possible in C#?

Also, is there a built-in way to append a big-endian integer because I believe my system is little-endian by default.

Upvotes: 0

Views: 148

Answers (2)

Craig Tullis
Craig Tullis

Reputation: 10507

You might check out the BitArray class in System.Collections. One of the ctor overloads takes an array of bytes, etc.

Upvotes: 1

hobbs
hobbs

Reputation: 240274

It's defined in such a way that you only need to deal with bytes if the message is an even number of bytes. If the message length (mod 64) is 56, then append one byte of 0b10000000, folowed by 63 0 bytes, followed by the length. Otherwise, append one byte of 0b10000000, followed by 0 to 62 0 bytes, followed by the length.

Upvotes: 2

Related Questions