Score:2

What are the first bits of a bitstring, for instance generated using SHA-256?

ve flag

I have a string that generates from SHA-256

x = fea5f97f9ca1e1a0a2ae344f4e12a3ab0c4d9221e6bb5d70bc567e39f8fbc3d5

What's are the first 10 bits are of the value of x?

et flag
The output seems to be hexadecimal. Each character here is stored in 4 bits. You need 2 & 1/2 characters to get 10 bits
Score:2
ng flag

The question's title (now) asks "What are the first bits of a bitstring". That's unambiguous if the bits are presented individually in a chronological order, or in writing in a context where there is a conventional reading order, like left to right.

But the body of the question is about "a string that generates from sha256". That's to be read as SHA-256, defined by FIPS 180-4. It's an algorithm which outputs a "bit string" of $256$ bits, and they are not immediately identifiable.

The body of the question has x = followed by $64$ characters all either digits or letters from a to f (in uniform case, here lowercase), which is $16=2^4$ characters. That hints the result of SHA-256 is encoded in hexadecimal, with each character encoding 4 bits (notice $256=64\times4$). This is one of several common representations of bitstrings as characters.

There are several different and incompatible ways to convert hexadecimal to bits, but fortunately, in the case of SHA-256, one is specified in 3.1 subparagraph 2 on page marked 7 of FIPS 180-4 (a must read). In summary, the most significant bits are first, be it at the nibble (4-bit), byte (8-bit), or word (32-bit in case of SHA-256) level.

Thus to find the first $i$ bits for the SHA-256 hash given, we can

  • Check that $0\le i\le256$, otherwise what's asked is undefined.
  • Take the first $\lceil i/4\rceil$ characters (where $\lceil r\rceil$ with $r\in\mathbb R$ is the smallest integer at least $r$); here $i=10$, thus $\lceil i/4\rceil=3$, thus we take fea
  • Write each of these characters (in reading order) as 4 binary digits per big-endian convention, per
    0 -> 0000     4 -> 0100     8 -> 1000     c -> 1100
    1 -> 0001     5 -> 0101     9 -> 1001     d -> 1101
    2 -> 0010     6 -> 0110     a -> 1010     e -> 1110
    3 -> 0011     7 -> 0111     b -> 1011     f -> 1111
    
  • That results in a string of $4\,\lceil i/4\rceil$ bits (here the 12 bits 111111101010), of which we keep the first $i$ bits (here 1111111010).

This applies to all hashes of the SHA family (replacing $256$ with their output width). Absent other specification, it's reasonable to apply it to other standard hashes which output width is a multiple of $32$ (or even $8$ or $4$) bits. That's debatable for MD5, since it uses little-endian convention in byte order within a $32$-bit word. And I would not blindly extend it to other quantities used in cryptography and also represented in hexadecimal, such as integers as used in RSA.

Score:0
in flag

Since this is about into programming and needs to be closed or migrated, here in Python with big-endian;

def LSBBits( hexData, lsbs):

    scale = 16 ## equals to hexadecimal

    inBinary = bin(int(my_hexdata, scale))[2:]
    inBinary = inBinary.zfill(256)
    return inBinary[len(inBinary)-lsbs:]

def MSBBits( hexData, mbsb):

    scale = 16 ## equals to hexadecimal

    inBinary = bin(int(my_hexdata, scale))[2:]
    inBinary = inBinary.zfill(256)
    return inBinary[:mbsb]

my_hexdata = "fea5f97f9ca1e1a0a2ae344f4e12a3ab0c4d9221e6bb5d70bc567e39f8fbc3d5"


print( LSBBits(my_hexdata, 10))
print( MSBBits(my_hexdata, 10))

And the program outputs.

1111010101
1111111010

Big-and little-endianness is another story. NIST assume big-endianness.

fgrieu avatar
ng flag
I'm afraid this answer is about the off-topic part of the question.
kelalaka avatar
in flag
@fgrieu I've expected a migration... Yes, due to an artifact, the zfill was causing it, removed, thank.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.