What exactly is a "raw binary format with a length of 20"?
This que开发者_高级运维stion arose for me while reading the Sha1-PHP Manpage
It says there:
If the optional raw_output is set to TRUE, then the sha1 digest is instead returned in raw binary format with a length of 20, otherwise the returned value is a 40-character hexadecimal number.
For me intuitively a "binary format" is a series of logical true or false values. Commonly represented by 0 and 1.
If you have 20 of them and you interpret them as number, you have a range of 2^20 different numbers.
For a 40-digit hexadecimal number this would be 16^40, which is about 1,4 * 10^42 times more than 2^20.
If you var_dump the raw output you just get garbled stuff, if you bin2hex them, you end up with such a big hexadecimal number as described above.
So my interpretation of the question's subject is definitely nonsense. So what exactly is a "raw binary format with a length of 20"?
In this case "raw binary" means raw bytes.
A SHA-1 digest is 160 bits long, or 20 bytes.
Many systems use the ASCII presentation format, which is 40 hexadecimal characters, but sometimes you need the raw data.
DNSSEC, for example, uses ASCII hex when storing signatures in a zone file or showing the output of dig
, but "on the wire" the raw format is used instead.
If you have the following hexadecimal string : '8A'
, it takes two characters, which is two bytes.
It's the kind of thing that sha1()
would return by default -- with 40 characters, instead of two, of course.
If you have the 138
value, it is an integer, which takes only one byte of memory -- but it represents the same thing, as the integer 138
is the value of hexadecimal 8A
.
This is the kind of value that would be returned by sha1()
when raw_output
is set to true
-- with 20 bytes, instead of just one.
When using an hexadecimal string to represent an integer, you need two characters to represent values between 0 and 255 -- and those values, when stored as a 8-bits integer, only require 1 byte to be stored.
This is why the hexadecimal string returned by sha1()
by default is 40 bytes -- while the integer value returned in the other case is only 20 bytes.
- The "raw" output is 20 bytes, thus adding up to the 20 * 8 = 160 bits of an SHA-1 digest.
- The default output is a 40 character hex number where each character represents one hex digit (=4 bits), which also adds up to 4 * 40 = 160 bits.
精彩评论