Cryptography Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.00

Message Digest - SHA1 Algorithm

Part:   1  2  3  4 

(Continued from previous part...)

Task 3. Preparing Processing Functions. SHA1 requires 80 processing functions defined as:

   f(t;B,C,D) = (B AND C) OR ((NOT B) AND D)         ( 0 <= t <= 19) 
   f(t;B,C,D) = B XOR C XOR D                        (20 <= t <= 39) 
   f(t;B,C,D) = (B AND C) OR (B AND D) OR (C AND D)  (40 <= t <= 59) 
   f(t;B,C,D) = B XOR C XOR D                        (60 <= t <= 79) 

Task 4. Preparing Processing Constants. SHA1 requires 80 processing constant words defined as:

   K(t) = 0x5A827999         ( 0 <= t <= 19) 
   K(t) = 0x6ED9EBA1         (20 <= t <= 39) 
   K(t) = 0x8F1BBCDC         (40 <= t <= 59) 
   K(t) = 0xCA62C1D6         (60 <= t <= 79) 

Task 5. Initializing Buffers. SHA1 algorithm requires 5 word buffers with the following initial values:

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

Task 6. Processing Message in 512-bit Blocks. This is the main task of SHA1 algorithm, which loops through the padded and appended message in blocks of 512 bits each. For each input block, a number of operations are performed. This task can be described in the following pseudo code slightly modified from the RFC 3174's method 1:

Input and predefined functions: 
   M[1, 2, ..., N]: Blocks of the padded and appended message
   f(0;B,C,D), f(1,B,C,D), ..., f(79,B,C,D): Defined as above
   K(0), K(1), ..., K(79): Defined as above
   H0, H1, H2, H3, H4, H5: Word buffers with initial values

Algorithm:
   For loop on k = 1 to N

     (W(0),W(1),...,W(15)) = M[k] /* Divide M[k] into 16 words */

     For t = 16 to 79 do:
         W(t) = (W(t-3) XOR W(t-8) XOR W(t-14) XOR W(t-16)) <<< 1

     A = H0, B = H1, C = H2, D = H3, E = H4

     For t = 0 to 79 do:
         TEMP = A<<<5 + f(t;B,C,D) + E + W(t) + K(t)
         E = D, D = C, C = B<<<30, B = A, A = TEMP
     End of for loop

     H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E
   End of for loop
   
Output: 
   H0, H1, H2, H3, H4, H5: Word buffers with final message digest

Step 5. Output. The contents in H0, H1, H2, H3, H4, H5 are returned in sequence the message digest.

SHA1 Implementation in Java

Sun provides SHA1 algorithm in Java under their JCE (Java Cryptography Extension) package, which is included in JDK 1.5.

Sun's implementation of SHA1 can be accessed through a generic class called MessageDigest. Here are the main methods of MessageDigest class:

  • getInstance("SHA1") - Returns a message digest object represents a specific implementation of SHA1 algorithm from the default provider, Sun.
  • getProvider() - Returns the provider name of the current object.
  • update(bytes) - Updates the input message by appending a byte array at the end.
  • digest() - Performs SHA1 algorithm on the current input message and returns the message digest as a byte array. This method also resets the input message to an empty byte string.
  • reset() - Resets the input message to an empty byte string.

(Continued on next part...)

Part:   1  2  3  4 

Dr. Herong Yang, updated in 2007
Cryptography Tutorials - Herong's Tutorial Notes - Message Digest - SHA1 Algorithm