1

An SHA1 digest should be 160 bits long. Still it is normally represented as a string with 40 characters. Considering 8-bits-bytes and that 1 char corresponds to 1 byte, it seems to me the SHA1 digest should have 20 bytes and it's hex representation 40 bytes.

For example, using OpenSSL I could get the following results (after manually removing extra information added):

PLAIN MESSAGE: The only possible revolution is inside us

openssl dgst -sha1 -hex dgsttxt &> sha1_hex

32 64 66 61 33 35 66 62 35 37 34 65 36 62 65 36 32 33 62 37 63 36 31 61 63 61 32 63 61 31 65 66 39 30 36 62 39 63 38 34

openssl dgst -sha1 -binary dgsttxt &> sha1_binary
2D FA 35 FB 57 4E 6B E6 23 B7 C6 1A CA 2C A1 EF 90 6B 9C 84

Applying a wc in each file I get

wc sha1_binary sha1_hex 
 0  1 20 sha1_binary
 0  1 40 sha1_hex
 0  2 60 total

So I have two questions:

  1. Why are there 20 more characters in the hex dump?
  2. How are those extra bits inserted? I could note each byte in the hex dump starts with either 3 or 6. Is there a particular reason for that?

I have already seen a similar question here but I am not sure if I am too stupid to understand the answers or if they are really poor. Any help is appreciated.

Community
  • 1
  • 1
Marcos Valle
  • 77
  • 2
  • 11

1 Answers1

1

160 bits / 8 = 20 bytes; a byte in hex is 2 characters (00 to FF) and 2 * 20 = 40 hex characters.

The longer output is the hexadecimally encoded version of the hexadecimal encoded hash.

Quite what the point of that is, who knows.

var s = "32 64 66 61 33 35 66 62 35 37 34 65 36 62 65 36 32 33 62 37 63 36 31 61 63 61 32 63 61 31 65 66 39 30 36 62 39 63 38 34".split(" ");

for (var i = 0; i < s.length; i++)
{
    document.write( String.fromCharCode(parseInt(s[i], 16)) );
}
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • So that is what happens when I get 2D FA 35 FB 57 4E 6B E6 23 B7 C6 1A CA 2C A1 EF 90 6B 9C 84 There are in fact 20*2 = 40 hex digits. Now how does that become 32 64 66 61 33 35 66 62 35 37 34 65 36 62 65 36 32 33 62 37 63 36 31 61 63 61 32 63 61 31 65 66 39 30 36 62 39 63 38 34 There are 40*2 = 80 hex, right? – Marcos Valle Oct 18 '14 at 16:56
  • Oh, things got more clear now! Still why would OpenSSL use this double hexadecimal encoding as the default option? That does seem pointless, doesn't it? Thank you very much for the example, it was extremely helpful. – Marcos Valle Oct 18 '14 at 18:56
  • I put a baby steps answer to the similar question here: https://stackoverflow.com/a/45178066/5552415 – Ger Dec 12 '17 at 19:39