|
The
Sha1 Encryption Algorithm In Java
By: T.Anderson
Lately,
security and encryption gurus have been bandying about the fact
that the MD5 Hash is a crackable hash since some folks claim they've
been able to generate
collsions in only 45 minutes. So, we decided to dig up the Sha1
algorithm, which is a tougher
algorithm to break, and implement it in Java.
The
driver class below is a simple set of instructions that executes
the FIPS Sha1 test cases and shows you how to call the sha1 functions
that we've included in the class. This code is based entirely on
the C code routines that Steve Reid released several years ago
into the public forum.
Our driver below simply executes the Sha1 hashing function, wrapping
around it some simple tests to check pass/fail of the algorithm
against the standard FIPS test cases. We did this implementation
for fun and to provide a Java based implementation of the Sha1 algorithm.
If you need a validated version of Sha1, check out the approved
list.
Click
Here to download the JAR file, .Java files are included in the
JAR. Please remember, all code found on Razertech.com is offered
without any warrenty and is the copyright property of Razertech.com,
unless expressly stated otherwise. Feel free to reuse the Sha1 Java
code provided here under LGPL.
Main
Driver Code
public
static void main(String[] args) {
int i;
long beginTime;
long endTime;
long elapsed;
double avg;
sha1 s = new sha1();
System.out.println("...................SHA-1
Test PROGRAM.");
System.out.println("This code runs the FIPS Test Strings through
the code.");
System.out.println("");
System.out.println("Test #1: 'abc'");
String z = "abc";
s.init();
s.addBytes(z);
s.finish();
if (s.digout().equals("a9993e364706816aba3e25717850c26c9cd0d89d"))
{
System.out.println("SHA1 Test 1 PASS");
}
else {
System.out.println("SHA1 Test 1 FAIL");
System.out.println(s.digout());
System.out.println("a9993e364706816aba3e25717850C26c9Cd0d89d");
} // end else
System.out.println(" ");
System.out.println("Test #2: 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'");
z = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
s.init();
s.updateASCII(z);
s.finish();
if
(s.digout().equals("84983e441c3bd26ebaae4aa1f95129e5e54670f1"))
{
System.out.println("SHA1 Test 2 PASS");
}
else {
System.out.println("SHA1 Test 2 FAIL");
System.out.println(s.digout());
System.out.println("84983e441c3bd26ebaae4aa1f95129e5e54670f1");
} // end else
System.out.println("");
System.out.println("Test #3: 1 million 'a' characters.");
s.init();
for (i = 0; i < 1000000; i++) s.addBytes((byte) 'a');
s.finish();
if (s.digout().equals("34aa973cd4c4daa4f61eeb2bdbad27316534016f"))
{
System.out.println("SHA1 Test 3 PASS");
}
else {
System.out.println("SHA1 Test 3 FAIL");
System.out.println(s.digout());
System.out.println("34AA973CD4C4DAA4F61EEB2BDBAD27316534016F");
} // end else
System.out.println("");
System.out.println("Begin Benchmark 1,000,000 hashes");
z = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
beginTime = System.currentTimeMillis();
for (int x = 0; x < 1000000; x++) {
s.init();
s.updateASCII(z);
s.finish();
} // end for
endTime = System.currentTimeMillis();
elapsed = endTime - beginTime;
System.out.print("Test Complete....total elapsed time (milis):");
System.out.println(elapsed);
System.out.print("Average time (milis):");
avg = (double)elapsed/1000000;
System.out.println(avg);
}
} // end testDriver class
|