The Cryptography Management Kit includes basic sample source code for SHA-1 algorithm. The following samples help to illustrate the depth and quality of this:
Typical Pages:
- for (i = 20; i < 40; i++)
{
- x = ((a << 5) | (a >> 27)) + (b ^ c ^ d)
- +e + W[i] + 0x6ed9ebal;
- e=d;
- d=c;
- c = (b << 30) | (b >> 2);
- b= a;
- a= x;
}
- for (i = 40; i < 60; i++)
{
- x = ((a << 5) | (a >> 27)) + (b ^ c ^ d)
- +e + W[i] + 0x8f1bbcdc;
- e=d;
- d=c;
- c = (b << 30) | (b >> 2);
- b= a;
- a= x;
}
- for (i = 60; i < 80; i++)
{
-
x = ((a << 5) | (a >> 27)) + (b ^ c ^ d)
- +e + W[i] + 0xca62c1d6;
- e=d;
- d=c;
- c = (b << 30) | (b >> 2);
- b= a;
- a= x;
}
state[0] += a; state[1] += b; state[2] += c;
state[3] += d; state[4] += e;
}
state[0] += a; state[1] += b; state[2] += c;
state[3] += d; state[4] += e;
}
void Prepare_SH1 (
- unsigned int Key_Length,
unsigned char Key[],
unsigned char Pad,
unsigned char KeyBlock[64],
unsigned char Work_Area[96])
{
- int i;
- unsigned int state[7];
- unsigned char buffer [64];
- for (i = 0; i < 64; i++) KeyBlock[i] = 9;
- if (Key_Length > 64)
{- Hash_Init (state) ;
- SHA1_Update (state, buffer, Key, Key_Length) ;
- SHA1_Final (state, buffer, KeyBlock) ;
}
else- for (i = 0; i < (int) Key Length; i++)
- ReyBlock[i] = Key[i];
for (i = 0; i < 64; i++) KeyBlock[i] ^= Pad;
}
The next section shows how the various routines are combined into a convenient
implementation of the SHA-1 algorithm.
Notice how the message can be specified either in its entirety or as a sequence of
segments. Notice also how a segment length of zero is valid.