Why is a SHA-1 Hash 40 characters long if it is only 160 bit?

后端 未结 6 481
自闭症患者
自闭症患者 2020-12-23 10:00

The title of the question says it all. I have been researching SHA-1 and most places I see it being 40 Hex Characters long which to me is 640bit. Could it not be represented

6条回答
  •  Happy的楠姐
    2020-12-23 10:28

    My answer only differs from the previous ones in my theory as to the EXACT origin of the OP's confusion, and in the baby steps I provide for elucidation.

    A character takes up different numbers of bytes depending on the encoding used (see here). There are a few contexts these days when we use 2 bytes per character, for example when programming in Java (here's why). Thus 40 Java characters would equal 80 bytes = 640 bits, the OP's calculation, and 10 Java characters would indeed encapsulate the right amount of information for a SHA-1 hash.

    Unlike the thousands of possible Java characters, however, there are only 16 different hex characters, namely 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. But these are not the same as Java characters, and take up far less space than the encodings of the Java characters 0 to 9 and A to F. They are symbols signifying all the possible values represented by just 4 bits:

    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
    

    Thus each hex character is only half a byte, and 40 hex characters gives us 20 bytes = 160 bits - the length of a SHA-1 hash.

提交回复
热议问题