131

I read some docs about md5, it said that its 128 bits, but why is it 32 characters? I can't compute the characters.

  • 1 byte is 8 bits
  • if 1 character is 1 byte
  • then 128 bits is 128/8 = 16 bytes right?

EDIT:

SHA-1 produces 160 bits, so how many characters are there?

Scott Arciszewski
  • 33,610
  • 16
  • 89
  • 206
hash_jr90
  • 1,313
  • 2
  • 9
  • 4
  • Can you post a link to the docs you reference? – Don Roby Jun 11 '11 at 17:06
  • @don: Sorry, its my lecturer notes. But ba__friend has answered my question, and i test it here: http://www.miraclesalad.com/webtools/md5.php, and its really hexadecimal characters only, it makes much more sense to me now. I will vote his answer as best answer – hash_jr90 Jun 11 '11 at 17:08
  • I think my answer goes into more detail on this than any others. [If you're new to computer programming and asking this question and want a through answer](http://stackoverflow.com/a/41618070/124486) – Evan Carroll Jan 12 '17 at 16:19
  • 11
    It's not a silly question. Once upon a time you also didn't know how many HEX chars represented one byte. – David Klempfner Apr 17 '18 at 02:02
  • A quick python one liner to calculate the bit length of a hash is `((1 << (n*4))-1).bit_length()` where n is the length of the hash in hexadecimal – Wouterr Mar 09 '20 at 13:36

9 Answers9

133

32 chars as hexdecimal representation, thats 2 chars per byte.

ba__friend
  • 5,783
  • 2
  • 27
  • 20
  • 1
    1 byte is represented by a two digit hexdecimal number, like 255 = ff. – ba__friend Jun 11 '11 at 17:09
  • 1
    so 1 byte is 2 chars which means 16 bits is 2 chars then 128/16 = 8. So 8 of 2 chars = 16 chars is required then? Why 32? – Koray Tugay Jul 13 '14 at 09:23
  • 24
    Because each hex character can be represented by 4 bits. So if it is 128 bits it is 128/4 = 32 hex characters. Even though each "char" will be encoded as a utf8 or ascii which will make the hex representation size 32*8= 256 bits. – Gaston Sanchez Oct 14 '14 at 19:49
  • 4
    This is a really poor answer. My answer goes into details on this: http://stackoverflow.com/a/41618070/124486 – Evan Carroll Jan 12 '17 at 16:18
  • @KorayTugay "so 1 byte is 2 chars which means 16 bits is 2 chars" - 1 byte != 16 bits. – David Klempfner Apr 17 '18 at 02:04
46

I wanted summerize some of the answers into one post.

First, don't think of the MD5 hash as a character string but as a hex number. Therefore, each digit is a hex digit (0-15 or 0-F) and represents four bits, not eight.

Taking that further, one byte or eight bits are represented by two hex digits, e.g. b'1111 1111' = 0xFF = 255.

MD5 hashes are 128 bits in length and generally represented by 32 hex digits.

SHA-1 hashes are 160 bits in length and generally represented by 40 hex digits.

For the SHA-2 family, I think the hash length can be one of a pre-determined set. So SHA-512 can be represented by 128 hex digits.

Again, this post is just based on previous answers.

RepentantFan
  • 585
  • 4
  • 2
36

A hex "character" (nibble) is different from a "character"

To be clear on the bits vs byte, vs characters.

  • 1 byte is 8 bits (for our purposes)
  • 8 bits provides 2**8 possible combinations: 256 combinations

When you look at a hex character,

  • 16 combinations of [0-9] + [a-f]: the full range of 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f
  • 16 is less than 256, so one one hex character does not store a byte.
  • 16 is 2**4: that means one hex character can store 4 bits in a byte (half a byte).
  • Therefore, two hex characters, can store 8 bits, 2**8 combinations.
  • A byte represented as a hex character is [0-9a-f][0-9a-f] and that represents both halfs of a byte (we call a half-byte a nibble).

When you look at a regular single-byte character, (we're totally going to skip multi-byte and wide-characters here)

  • It can store far more than 16 combinations.
  • The capabilities of the character are determined by the encoding. For instance, the ISO 8859-1 that stores an entire byte, stores all this stuff
  • All that stuff takes the entire 2**8 range.
  • If a hex-character in an md5() could store all that, you'd see all the lowercase letters, all the uppercase letters, all the punctuation and things like ¡°ÀÐàð, whitespace like (newlines, and tabs), and control characters (which you can't even see and many of which aren't in use).

So they're clearly different and I hope that provides the best break down of the differences.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • 1
    What does this "In which order is actually spec or platform dependent" actually mean? Would love to see this explained a bit more. – KumarM Jan 11 '19 at 23:43
  • 1
    @KumarM I'm going to delete that because I don't think it's actually relevant to the conversation at all, and it's badly worded. – Evan Carroll Jan 11 '19 at 23:47
29

MD5 yields hexadecimal digits (0-15 / 0-F), so they are four bits each. 128 / 4 = 32 characters.

SHA-1 yields hexadecimal digits too (0-15 / 0-F), so 160 / 4 = 40 characters.

(Since they're mathematical operations, most hashing functions' output is commonly represented as hex digits.)

You were probably thinking of ASCII text characters, which are 8 bits.

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
14

One hex digit = 1 nibble (four-bits)

Two hex digits = 1 byte (eight-bits)

MD5 = 32 hex digits

32 hex digits = 16 bytes ( 32 / 2)

16 bytes = 128 bits (16 * 8)

The same applies to SHA-1 except it's 40 hex digits long.

I hope this helps.

Community
  • 1
  • 1
Rain
  • 3,416
  • 3
  • 24
  • 40
9

That's 32 hex characters - 1 hex character is 4 bits.

Brett Thomas
  • 1,694
  • 3
  • 15
  • 21
3

Those are hexidecimal digits, not characters. One digit = 4 bits.

eaolson
  • 14,717
  • 7
  • 43
  • 58
2

They're not actually characters, they're hexadecimal digits.

Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
0

For clear understanding, copy the MD5 calculated 128 bit hash value in the Binary to Hex convertor and see the length of the Hex value. You will get 32 characters Hex characters.

Alok Singh
  • 488
  • 2
  • 6