SHA-1 Algorithms Work
SHA-1 Algorithms Work
This page was written for people who really want to know exactly how hash algorithms work.
The following is a step-by-step walk through of exactly how hash algorithms work. This is
written mainly for people with very good knowledge about computers, encryption, and logical
operators. I did try to write it so that everyone could understand it, but you might find it
boring/dry/confusing if you aren't really interested in how hash algorithms work and know a
fair amount already.
Important properties:
Each hash is unique but always repeatable.
The word 'cat' will hash to something that no other word hashes too, but it will always hash to
the same thing.
There are many different hash functions but the one I will be concentrating on today is called
the Secure Hash Algorithm 1 or SHA-1.
h0 = 01100111010001010010001100000001
h1 = 11101111110011011010101110001001
h2 = 10011000101110101101110011111110
h3 = 00010000001100100101010001110110
h4 = 11000011110100101110000111110000
A Test
Step 2: Break it into characters
A TEST
01000001001000000101010001100101011100110111010010000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
000000000000000000000000000000000000000000000000
If your original message were 56 characters long, it would be exactly 448 digits long once
converted into binary. After adding the '1' to the end of that, the new message would be 449
characters long. If that were the case you would need to append 511 zero's to make the
message congruent to 448%512.
If your original message were 64 characters long, it would be exactly 512 digits long once
converted into binary. After adding the '1' to the end of that, the new message would be 513
characters long. If that were the case you would need to append 447 zero's to make the
message congruent to 448%512.
01000001001000000101010001100101011100110111010010000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000110000
01000001001000000101010001100101011100110111010010000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000000000000000000000000000000000000000000000000000000000-
00000000000000000000000000110000
0: 01000001001000000101010001100101
1: 01110011011101001000000000000000
2: 00000000000000000000000000000000
3: 00000000000000000000000000000000
4: 00000000000000000000000000000000
5: 00000000000000000000000000000000
6: 00000000000000000000000000000000
7: 00000000000000000000000000000000
8: 00000000000000000000000000000000
9: 00000000000000000000000000000000
10: 00000000000000000000000000000000
11: 00000000000000000000000000000000
12: 00000000000000000000000000000000
13: 00000000000000000000000000000000
14: 00000000000000000000000000000000
15: 00000000000000000000000000110000
0: 01000001001000000101010001100101
2: 00000000000000000000000000000000
8: 00000000000000000000000000000000
13: 00000000000000000000000000000000
The next time through the loop we want words: 14, 9, 3 and 1.
After the fifth time through the loop we will want words: 17, 12, 6 and 4.
Note that word 17 doesn't exist yet, but it will after the first run of the loop. That means that
after seventeen passes through the loop we will be using entirely words that aren't part of the
original sixteen.
Now that we have our words selected we will start by performing what's known as an 'XOR' or
'Exclusive OR' on them. In the end all four words will be XOR'ed together, but you can think of
it as first doing [i-3]XOR[i-8] then XOR'ing that by [i-14] and that again by [i-16]. XOR is one of
a few simple logical operators. All it means is that you compare the two numbers bit by bit
and if exactly one of them has the value '1', output a '1'. However, if both numbers have a '0'
for that bit, or they both have a '1', output a '0'. This works very similarly to a logical 'OR'
which will be used later. The only difference is that an 'OR' will return a '1' so long as either
column has a '1' even if both of them do.
Out:
00000000000000000000000000000000
Out:
00000000000000000000000000000000
Out:
01000001001000000101010001100101
After step nine is complete we will now have 80 words that look like this:
0: 01000001001000000101010001100101
1: 01110011011101001000000000000000
2: 00000000000000000000000000000000
3: 00000000000000000000000000000000
4: 00000000000000000000000000000000
5: 00000000000000000000000000000000
6: 00000000000000000000000000000000
7: 00000000000000000000000000000000
8: 00000000000000000000000000000000
9: 00000000000000000000000000000000
10: 00000000000000000000000000000000
11: 00000000000000000000000000000000
12: 00000000000000000000000000000000
13: 00000000000000000000000000000000
14: 00000000000000000000000000000000
15: 00000000000000000000000000110000
16: 10000010010000001010100011001010
17: 11100110111010010000000000000000
18: 00000000000000000000000001100000
19: 00000100100000010101000110010101
20: 11001101110100100000000000000001
21: 00000000000000000000000011000000
22: 00001001000000101010001100101010
23: 10011011101001000000000001100011
24: 00000100100000010101000000010101
25: 11011111110101110100011001010101
26: 00110111010010000000000000000111
27: 00000000000000000000001100000000
28: 00100100000010101000110010101000
29: 01101110100100000000000111101110
30: 00010110100001000001000111000001
31: 10110010100011110001100111110110
32: 11010000101000111111001010100011
33: 01010110011101100000110000000010
34: 10010000001010100011001100100000
35: 10101000010001010100000111101101
36: 01101101010110000100011100000011
37: 11001010001111000110010011011010
38: 01100110100001010100011000100111
39: 00110111010010000011000110000111
40: 01010010101011011000110011010110
41: 11011110010010000001111011100001
42: 01101000010000010001110000010001
43: 00101000111100011001111110101011
44: 00000011001111011000100100010111
45: 11111100110001001100000110100110
46: 00010000101001100111010111011101
47: 10100001000110010101101011001001
48: 11011101110000010001100111100111
49: 01100001101110100100110110100110
50: 01101000010101000110010111110110
51: 00101110100100110100011011110111
52: 11010010101101011000101100101010
53: 11010011110010011110001000011010
54: 00010100001110111111001110110110
55: 00110101010110011111110100001011
56: 01101001110010001101011001110100
57: 00000110011100000111111010110101
58: 01101100111000100001101111110110
59: 00100110110111011001110100011101
60: 10001110101111000001001010101011
61: 11000101111011001100010100000111
62: 11111111000000100000010100100011
63: 11110110100011011111000110011110
64: 00110011011000101101111011000100
65: 01101100101101101110000110001111
66: 01000001000111000000100101101000
67: 11010001110010111100111001101001
68: 01001001000010010001011101110000
69: 11000100110000011010011011111100
70: 10100110011101011101110100010000
71: 00011001010110101100101010100001
72: 11100101000100110110101101110101
73: 11010100110111011011111001101111
74: 01110100001100011001010100101001
75: 10101111110100111111101000001101
76: 11011000110101010111110100101111
77: 00000111001000100000111010011001
78: 10001011100011011111100111110101
79: 10110111011010010100111100111110
A = h0
B = h1
C = h2
D = h3
E = h4
The first step of function 1 is to set the variable 'f' equal to: (B AND C) OR (!B AND D)
B:
11101111110011011010101110001001
C:
10011000101110101101110011111110
B AND C
Out:
10001000100010001000100010001000
!B:
00010000001100100101010001110110
D:
00010000001100100101010001110110
!B AND D
Out:
00010000001100100101010001110110
B AND C:
10001000100010001000100010001000
!B AND D:
00010000001100100101010001110110
(B AND C) OR (!B AND D)
F:
10011000101110101101110011111110
The second step of function 1 is to set the variable 'k' equal to:
01011010100000100111100110011001
Function 2
For this function we will be using the 'XOR' operation exclusively.
Just to refresh: A logical 'XOR' will output a '1' if either the first or the second of the inputs
are '1' butnot both.
The first step of function 2 is to set the variable 'f' equal to: B XOR C XOR D
Of course by this time our variables have changed. Here's what this step will actually look like
by the time we get to it:
B:
11011100100010001001111001110101
C:
10110101000101000100100011110000
B XOR C
Out:
01101001100111001101011010000101
B XOR C:
01101001100111001101011010000101
D:
01001110101011001011101010110111
(B XOR C) XOR D
F:
00100111001100000110110000110010
The second step of function 2 is to set the variable 'k' equal to:
01101110110110011110101110100001
Function 3
For this function we will be using the 'AND' and 'OR' operations.
The first step of function 3 is to set the variable 'f' equal to: (B AND C) OR (B AND D) OR (C
AND D)
By this time our variables have changed again. Here's what this step will actually look like by
the time we get to it:
B:
01000100110000000111111001110111
C:
00011010100110110011101010111011
B AND C
Out:
00000000100000000011101000110011
B:
01000100110000000111111001110111
D:
01010011011001010110101011100100
B AND D
Out:
01000000010000000110101001100100
C:
00011010100110110011101010111011
D:
01010011011001010110101011100100
C AND D
Out:
00010010000000010010101010100000
B AND C:
00000000100000000011101000110011
B AND D:
01000000010000000110101001100100
(B AND C) OR (B AND D)
Out:
01000000110000000111101001110111
F:
01010010110000010111101011110111
The second step of function 3 is to set the variable 'k' equal to:
10001111000110111011110011011100
Function 4
Function 4 is exactly the same as function 2 except that we will set 'k' equal to
11001010011000101100000111010110.
Out:
110111100110100101110101010010101
Notice that the result of this operation is one bit longer than the two inputs. After each
iteration the new word should be one bit longer than the last. Sometimes this will be a
necessary carrier bit (like the extra place value you need to represent the result of adding the
two single digit numbers 5 and 6 in base 10), and when that's not needed you must simply
prepend a 1. For everything to work out properly we will need to truncate that extra bit
eventually; however, we do not want to do that until the end!
A lrot 5 + F:
110111100110100101110101010010101
E:
11101001001001111110100110101011
A lrot 5 + F + E
Out:
1010100101111110101101010001000000
A lrot 5 + F + E:
1010100101111110101101010001000000
K:
11001010011000101100000111010110
A lrot 5 + F + E + K
Out:
11101110000010111011001011000010110
A lrot 5 + F + E + K:
11101110000010111011001011000010110
Word 79:
10110111011010010100111100111110
A lrot 5 + F + E
Out:
100000100111110001101110010101010100
Now we need to truncate the result so that the next operations will work smoothly. We will
remove as much of the beginning(left) until the number is 32 bits or 'digits' long.
32-bit temp:
00100111110001101110010101010100
The only thing left to do at this point is 're-set' some variables then start the loop over. We
will be setting the following variables as such:
E=D
D=C
C = B Left Rotate 30
B=A
A = temp
h0 = h0 + A
h1 = h1 + B
h2 = h2 + C
h3 = h3 + D
h4 = h4 + E
h0 = 10001111000011000000100001010101
h1 = 10010001010101100011001111100100
h2 = 10100111110111100001100101000110
h3 = 10001011001110000111010011001000
h4 = 10010000000111011111000001000011
Finally the variables are converted into base 16 (hex) and joined together.
8f0c0855
915633e4
a7de1946
8b3874c8
901df043
8f0c0855915633e4a7de19468b3874c8901df043