## Implementing SHA-1 in Java?!?

Hi all,

I'm trying to implement the SHA-1 algorithm in Java 2, Micro Edition but are having major problems getting it to work.

The first part with message padding works great. The problem is in the hashing routine. I got the following code:

On section d of the FIPS PUB 180-1 they say:

(D) For t=0 to 79 do
TEMP = S^5(A) + f(B,C,D) + E + Wt + Kt
E = D; D = C; C = S^30(B); B = A; A = TEMP;

I've implemented this using the following code:
PHP Code:
``` for (count = 0; count < 80; count++) {    Wtemp = S(A, 5) + F(count, B, C, D) + E + W[count] + K(count);    E = D; D = C; C = S(B, 30); B = A; A = Wtemp; }  ```
For the function S I have:
PHP Code:
``` int S(int WORD, int BITS){    return (((WORD) << (BITS)) | ((WORD) >> (32-(BITS))));  ```
For the function F I have:
PHP Code:
``` int F(int pos, int B, int C, int D) {    if (pos < 20)        tempByte = (B & C) | ((~B) & D);    else if (pos < 40)       tempByte = B ^ C ^ D;    else if (pos < 60)       tempByte = (B & C) | (B & D) | (C & D);    else       tempByte = B ^ C ^ d;    return tempByte; }  ```
Using the test vector showed on the FIPS PUB 180-1 (Message = "abc") I get the first 2 rounds correct but the remaining 78 rounds are completely out of whack.

Anyone have any ideas????????????

Mounir