Reputation: 1373
I read some docs about md5, it said that its 128 bits, but why is it 32 characters? I can't compute the characters.
EDIT:
SHA-1 produces 160 bits, so how many characters are there?
Upvotes: 137
Views: 154677
Reputation: 1850
I want to provide an example to understand it better. For example, consider this 128 bit MD5 hash
01011101 01000001 01000000 00101010 10111100 01001011 00101010 01110110 10111001 01110001 10011101 10010001 00010000 00010111 11000101 10010010
Then use Hexa decimal encoding which uses 0-9 and a-f (or A-F) which amounts to 16 possible characters. 16 possible characters can be expressed in 4 bits as in 2`4 = 16.
So now read 4 bits from the start and convert them to hexadecimal values.
0101 = 5
1101 = d, 0100 = 4, 0001 = 1 etc which results in 5d41402abc4b2a76b9719d911017c592, which is 32 in length
Upvotes: 1
Reputation: 508
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.
Upvotes: 0
Reputation: 3926
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.
Upvotes: 16
Reputation: 1
To be clear on the bits vs byte, vs characters.
2**8
possible combinations: 256 combinationsWhen you look at a hex character,
[0-9] + [a-f]
: the full range of 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f
2**4
: that means one hex character can store 4 bits in a byte (half a byte).2**8
combinations.[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)
2**8
range.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.
Upvotes: 39
Reputation: 20873
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.
Upvotes: 30
Reputation: 605
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.
Upvotes: 48
Reputation: 33151
They're not actually characters, they're hexadecimal digits.
Upvotes: 2
Reputation: 15094
Those are hexidecimal digits, not characters. One digit = 4 bits.
Upvotes: 3
Reputation: 5903
32 chars as hexdecimal representation, thats 2 chars per byte.
Upvotes: 142