This tutorial helps you to understand
- What is SHA1?
- SHA1 Algorithm Overview
- SHA1 Implementation in Java
- SHA1 Implementation in PHP
- SHA1 Implementation in Perl
What is SHA1
SHA1 (Secure Hash Algorithm 1) is message-digest algorithm, which takes an input message of any length < 2^64 bits and produces a 160-bit output as the message digest.
Based on the SHA1 RFC document, the SHA-1 is called secure because it is computationally infeasible to find a message which corresponds to a given message digest, or to find two different messages which produce the same message digest. Any change to a message in transit will, with very high probability, result in a different message digest, and the signature will fail to verify.
The original specification of the algorithm was published in 1993 as the Secure Hash Standard, FIPS PUB 180, by US government standards agency NIST (National Institute of Standards and Technology). This version is now often referred to as "SHA0".
SHA-0 was withdrawn by the NSA shortly after publication and was superseded by the revised version, published in 1995 in FIPS PUB 180-1 and commonly referred to as "SHA1".
SHA1 Algorithm Overview
SHA1 algorithm is well described in RFC 3174 - US Secure Hash Algorithm 1 (SHA1). Below is a quick overview of the algorithm.
SHA1 algorithm consists of 6 tasks:
Task 1. Appending Padding Bits. The original message is "padded" (extended) so that its length (in bits) is congruent to 448, modulo 512. The padding rules are:
- The original message is always padded with one bit "1" first.
- Then zero or more bits "0" are padded to bring the length of the message up to 64 bits fewer than a multiple of 512.
Task 2. Appending Length. 64 bits are appended to the end of the padded message to indicate the length of the original message in bytes. The rules of appending length are:
- The length of the original message in bytes is converted to its binary format of 64 bits. If overflow happens, only the low-order 64 bits are used.
- Break the 64-bit length into 2 words (32 bits each).
- The low-order word is appended first and followed by the high-order word.
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.
Here is a sample Java program to show you how to use the MessageDigest class to perform some tests on SHA1 algorithms.
*/
import java.security.*;
class JceSha1Test {
public static void main(String[] a) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Message digest object info: ");
System.out.println(" Algorithm = "+md.getAlgorithm());
System.out.println(" Provider = "+md.getProvider());
System.out.println(" toString = "+md.toString());
String input = "";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") =");
System.out.println(" "+bytesToHex(output));
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") =");
System.out.println(" "+bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") =");
System.out.println(" "+bytesToHex(output));
} catch (Exception e) {
System.out.println("Exception: "+e);
}
}
public static String bytesToHex(byte[] b) {
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
StringBuffer buf = new StringBuffer();
for (int j=0; j<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]);
}
return buf.toString();
}
}
If you run this sample program with JDK 1.5, you should get the following output:
Message digest object info:
Algorithm = SHA1
Provider = SUN version 1.5
toString = SHA1 Message Digest from SUN, <initialized>
SHA1("") =
DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
SHA1("abc") =
A9993E364706816ABA3E25717850C26C9CD0D89D
SHA1("abcdefghijklmnopqrstuvwxyz") =
32D10C7B8CF96570CA04CE37F2A19D84240D3A89
SHA1 Implementation in PHP
If you are interested in using SHA1 in PHP, you can use the built-in function sha1(). Here is a sample program showing you how to use sha1() function:
<?php # PhpSha1Test.php
#
$input = "";
$output = sha1($input);
print("\n");
print("SHA1(\"".$input."\") =\n");
print(" $output\n");
$input = "abc";
$output = sha1($input);
print("\n");
print("SHA1(\"".$input."\") =\n");
print(" $output\n");
$input = "abcdefghijklmnopqrstuvwxyz";
$output = sha1($input);
print("\n");
print("SHA1(\"".$input."\") =\n");
print(" $output\n");
?>
If you run this sample program with PHP 5, you should get:
SHA1("") = da39a3ee5e6b4b0d3255bfef95601890afd80709 SHA1("abc") = a9993e364706816aba3e25717850c26c9cd0d89d SHA1("abcdefghijklmnopqrstuvwxyz") = 32d10c7b8cf96570ca04ce37f2a19d84240d3a89 SHA1 Implementation in Perl
If you are interested in using SHA1 in Perl, you can look a very interesting implementation by John Allen in 8 lines of perl5. Here is a copy of John's code, stored in PerlSha1In8Lines.pl:
#!/usr/bin/perl -iD9T4C`>_-JXF8NMS^$#)4=L/2X?!:@GF9;MGKH8\;O-S*8L'6 @A=unpack"N*",unpack u,$^I;@K=splice@A,5,4;sub M{($x=pop)-($m=1+~0)*int$x/$m}; sub L{$n=pop;($x=pop)<<$n|2**$n-1&$x>>32-$n}@F=(sub{$b&($c^$d)^$d},$S=sub{$b^$c ^$d},sub{($b|$c)&$d|$b&$c},$S);do{$l+=$r=read STDIN,$_,64;$r++,$_.="\x80"if$r< 64&&!$p++;@W=unpack N16,$_."\0"x7;$W[15]=$l*8 if$r<57;for(16..79){push@W,L$W[$_ -3]^$W[$_-8]^$W[$_-14]^$W[$_-16],1}($a,$b,$c,$d,$e)=@A;for(0..79){$t=M&{$F[$_/ 20]}+$e+$W[$_]+$K[$_/20]+L$a,5;$e=$d;$d=$c;$c=L$b,30;$b=$a;$a=$t}$v='a';@A=map{ M$_+${$v++}}@A}while$r>56;printf'%.8x'x5 ."\n",@A To test this Perl program on Windows, I did the following in a command window:
>copy con empty.txt ^Z 1 file(s) copied. >perl PerlSha1In8Lines.pl < empty.txt da39a3ee5e6b4b0d3255bfef95601890afd80709 >copy con abc.txt abc^Z 1 file(s) copied. >perl PerlSha1In8Lines.pl < abc.txt a9993e364706816aba3e25717850c26c9cd0d89d >copy con a_to_z.txt abcdefghijklmnopqrstuvwxyz^Z 1 file(s) copied. >perl PerlSha1In8Lines.pl < a_to_z.txt 32d10c7b8cf96570ca04ce37f2a19d84240d3a89 The output proves that John's program works perfectly. Note that:
- "copy con file_name" command allows to copy enter data from ***board into a new file.
- ^Z stands for (Ctrl-Z). It sends an end-of-file signal to the "copy" command.
Conclusions:
- SHA1 is a message digest algorithm producing 160 bits of data.
- Most modern programming languages provides SHA1 algorithm as built-in functions.
![]()


.de
Reply With Quote
The login name, suid, and generated password are displayed to the administrator executing the procedure. The output of the command shows all 10 accounts that have not transitioned are reset (and locked).