Since 1995 SHA-1 has been in use by the Federal government. Recently the Algorithm has come under some scrutiny because there have been flaws found in the algorithm called collisions (collisions are identical message digests that are produced by the algorithm when it is given different inputs). Although the standard will be phased out of use by 2010 the concepts behind SHA are still important.

SHA-1 produces a unique 160-bit Hash for any input less than 2^64 bits that it is given (except for the aforementioned collisions). Here is the process by which a message digest is computed:

To begin, the message must be padded to make it a multiple of 512, in bits. This is done by appending a 1 followed by 0's until the message is 64-bits less than the next multiple of 512 (i.e. if the message were 386 bits long then we would add zeros until it reached 448 bits long). Then, the 64-bit word representation of the original message length is appended to make the padded message a multiple of 512 (i.e. the 64-bit representation of 386 is appended).

Next, we must initialize the five chaining variables as follows:
Note: 0x stands for hexadecimal value, each value is 32-bits and unsigned (not negative)

H0 = 0x67452301
H1 = 0xEFCDAB89
H2 = 0x98BADCFE
H3 = 0x10325476
H4 = 0xC3D2E1F0

and define the constants,

Ki = 0x5A827999, for i from 0 to 19
Ki = 0x6ED9EBA1, for i from 20 to 39
Ki = 0x8F1BBCDC, for i from 40 to 59
Ki = 0xCA62C1D6B, for i from 60 to 79

and define the non-linear functions,

fi = (B and C) OR ((not B) and D), for i from 0 to 19
fi = B XOR C XOR D, for i from 20 to 39
fi = (B and C) OR (B and D) OR (C and D), for i from 40 to 59
fi = B XORC XOR D, for i from 60 to 79

Next we break up the entire message into 512-bit blocks. Each 512-bit block is represented as Mi (i.e. if the message were 1024 bits with padding then there would be M1 and M2).

Now we break each Mi up into 16:32-bit words. Each word is represented as Wi (i.e. M1 will have W0, W1, W2, ...W15).

Now we transform each Mi from 16:32-bit words to 80:32-bit words. This is done by performing the following process:

For i = 16 to 79 let Wi = (Wi - 3 XOR Wi - 8 XOR Wi - 14 XOR Wi - 16) Shift 1 bit left.

Now that we have 80:32-bit words we can process the expanded Mi to find the new variables that will eventually make up our one-way message digest by doing the following:

Set,
A = H0
B = H1
C = H2
D = H3
E = H4
Then,
For each i from 0 to 79
A = (A shifted left 5 bits) + fi(B,C,D) + E + Wi + Ki
B = A
C = B shifted 30 bits
D = C
E = D

When the process has finished the hash for this block is calculated as:
H0 = H0 + A
H1 = H1 + B
H2 = H2 + C
H3 = H3 + D
H4 = H4 + E

This process is repeated until the last Mi is processed. At that time the Hi's are appended to find the 160-bit Hash value (because each Hi is 32-bits long).