|
From: <pe...@us...> - 2004-03-09 00:07:25
|
Update of /cvsroot/neuclear/neuclear-commons/src/test/org/neuclear/commons/crypto/channels In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23642/src/test/org/neuclear/commons/crypto/channels Modified Files: SigningChannelTest.java Log Message: More improvements on the XMLSignature. Now uses the Transforms properly, References properly. All the major elements have been refactored to be cleaner and more correct. Index: SigningChannelTest.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/test/org/neuclear/commons/crypto/channels/SigningChannelTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** SigningChannelTest.java 8 Mar 2004 17:13:54 -0000 1.3 --- SigningChannelTest.java 8 Mar 2004 23:50:34 -0000 1.4 *************** *** 2,16 **** import junit.framework.TestCase; - import org.neuclear.commons.crypto.CryptoTools; import org.neuclear.commons.crypto.CryptoException; ! ! import java.security.*; ! import java.nio.ByteBuffer; ! import java.nio.channels.FileChannel; ! import java.io.IOException; import java.io.File; import java.io.FileInputStream; ! import java.io.BufferedInputStream; /** --- 2,15 ---- import junit.framework.TestCase; import org.neuclear.commons.crypto.CryptoException; + import org.neuclear.commons.crypto.CryptoTools; ! import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; ! import java.io.IOException; ! import java.nio.ByteBuffer; ! import java.nio.channels.FileChannel; ! import java.security.*; /** *************** *** 22,32 **** */ public class SigningChannelTest extends TestCase { ! public SigningChannelTest(String name) throws NoSuchAlgorithmException { super(name); ! kp = CryptoTools.createTinyKeyPair(); } public void testSign() throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { ! for (int i=0;i<TESTSTRINGS.length;i++){ assertSignatureEquals(TESTSTRINGS[i].getBytes()); } --- 21,31 ---- */ public class SigningChannelTest extends TestCase { ! public SigningChannelTest(String name) throws NoSuchAlgorithmException { super(name); ! kp = CryptoTools.createTinyRSAKeyPair(); } public void testSign() throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { ! for (int i = 0; i < TESTSTRINGS.length; i++) { assertSignatureEquals(TESTSTRINGS[i].getBytes()); } *************** *** 35,112 **** public void testFileDigest() throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { ! File cd=new File("src/java/org/neuclear/commons/crypto"); ! File files[]=cd.listFiles(); ! System.out.println("Testing Digests on: "+files.length+" files"); ! for (int i=0;i<files.length;i++){ if (files[i].isFile()) ! assertSignatureEquals(files[i]); } } ! public byte [] getChannelSignature(byte[] data) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { ! SigningChannel ch=new SigningChannel(kp.getPrivate()); ! ByteBuffer buf=ByteBuffer.wrap(data); ch.write(buf); return ch.getSignature(); } ! public byte [] getNormalSignature(byte[] data) throws CryptoException { ! return CryptoTools.sign(kp,data); } ! public boolean verifyChannelSignature(byte[] data,byte[] sig) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { ! VerifyingChannel ch=new VerifyingChannel(kp.getPublic()); ! ByteBuffer buf=ByteBuffer.wrap(data); ch.write(buf); return ch.verify(sig); } ! public boolean verifyNormalSignature(byte[] data,byte[] sig) throws CryptoException { ! return CryptoTools.verify(kp.getPublic(),data,sig); } public void assertSignatureEquals(byte[] data) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { ! byte[] sig=getChannelSignature(data); ! assertEquals("Signature Match",new String(getNormalSignature(data)),new String(sig)); ! assertTrue("Signature Channel Verify",verifyChannelSignature(data,sig)); ! assertTrue("Signature Channel Normal",verifyNormalSignature(data,sig)); } public void assertSignatureEquals(File file) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { ! byte[] sig=getChannelSignature(file); ! assertEquals("Signature Match",new String(getNormalSignature(file)),new String(sig)); ! assertTrue("Signature Channel Verify",verifyChannelSignature(file,sig)); ! assertTrue("Signature Channel Normal",verifyNormalSignature(file,sig)); } public byte[] getChannelSignature(File file) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { ! SigningChannel ch=new SigningChannel(kp.getPrivate()); ! FileChannel fch=new FileInputStream(file).getChannel(); ! fch.transferTo(0,fch.size(),ch); return ch.getSignature(); } ! public boolean verifyChannelSignature(File file,byte[] sig) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { ! VerifyingChannel ch=new VerifyingChannel(kp.getPublic()); ! FileChannel fch=new FileInputStream(file).getChannel(); ! fch.transferTo(0,fch.size(),ch); return ch.verify(sig); } public byte[] getNormalSignature(File file) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException { ! Signature sig=Signature.getInstance("SHA1withRSA"); sig.initSign(kp.getPrivate()); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); ! int numread=0; ! byte buffer[]=new byte[1024]; ! while ((numread=in.read(buffer, 0, buffer.length)) >= 0) { ! sig.update(buffer,0,numread); } in.close(); return sig.sign(); } ! public boolean verifyNormalSignature(File file,byte sigb[]) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException { ! Signature sig=Signature.getInstance("SHA1withRSA"); sig.initVerify(kp.getPublic()); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); ! int numread=0; ! byte buffer[]=new byte[1024]; ! while ((numread=in.read(buffer, 0, buffer.length)) >= 0) { ! sig.update(buffer,0,numread); } in.close(); --- 34,119 ---- public void testFileDigest() throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { ! File cd = new File("src/java/org/neuclear/commons/crypto"); ! File files[] = cd.listFiles(); ! System.out.println("Testing Digests on: " + files.length + " files"); ! for (int i = 0; i < files.length; i++) { if (files[i].isFile()) ! assertSignatureEquals(files[i]); } } ! public byte[] getChannelSignature(byte[] data) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { ! SigningChannel ch = new SigningChannel(kp.getPrivate()); ! ByteBuffer buf = ByteBuffer.wrap(data); ch.write(buf); return ch.getSignature(); } ! ! public byte[] getNormalSignature(byte[] data) throws CryptoException { ! return CryptoTools.sign(kp, data); } ! ! public boolean verifyChannelSignature(byte[] data, byte[] sig) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { ! VerifyingChannel ch = new VerifyingChannel(kp.getPublic()); ! ByteBuffer buf = ByteBuffer.wrap(data); ch.write(buf); return ch.verify(sig); } ! ! public boolean verifyNormalSignature(byte[] data, byte[] sig) throws CryptoException { ! return CryptoTools.verify(kp.getPublic(), data, sig); } + public void assertSignatureEquals(byte[] data) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { ! byte[] sig = getChannelSignature(data); ! assertEquals("Signature Match", new String(getNormalSignature(data)), new String(sig)); ! assertTrue("Signature Channel Verify", verifyChannelSignature(data, sig)); ! assertTrue("Signature Channel Normal", verifyNormalSignature(data, sig)); } + public void assertSignatureEquals(File file) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { ! byte[] sig = getChannelSignature(file); ! assertEquals("Signature Match", new String(getNormalSignature(file)), new String(sig)); ! assertTrue("Signature Channel Verify", verifyChannelSignature(file, sig)); ! assertTrue("Signature Channel Normal", verifyNormalSignature(file, sig)); } public byte[] getChannelSignature(File file) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { ! SigningChannel ch = new SigningChannel(kp.getPrivate()); ! FileChannel fch = new FileInputStream(file).getChannel(); ! fch.transferTo(0, fch.size(), ch); return ch.getSignature(); } ! ! public boolean verifyChannelSignature(File file, byte[] sig) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { ! VerifyingChannel ch = new VerifyingChannel(kp.getPublic()); ! FileChannel fch = new FileInputStream(file).getChannel(); ! fch.transferTo(0, fch.size(), ch); return ch.verify(sig); } + public byte[] getNormalSignature(File file) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException { ! Signature sig = Signature.getInstance("SHA1withRSA"); sig.initSign(kp.getPrivate()); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); ! int numread = 0; ! byte buffer[] = new byte[1024]; ! while ((numread = in.read(buffer, 0, buffer.length)) >= 0) { ! sig.update(buffer, 0, numread); } in.close(); return sig.sign(); } ! ! public boolean verifyNormalSignature(File file, byte sigb[]) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException { ! Signature sig = Signature.getInstance("SHA1withRSA"); sig.initVerify(kp.getPublic()); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); ! int numread = 0; ! byte buffer[] = new byte[1024]; ! while ((numread = in.read(buffer, 0, buffer.length)) >= 0) { ! sig.update(buffer, 0, numread); } in.close(); *************** *** 118,121 **** --- 125,129 ---- * * @throws org.neuclear.commons.crypto.CryptoException + * */ public void testBenchmark() throws CryptoException, NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException { *************** *** 130,153 **** System.out.println("\nNormal Signing benchmarks:"); ! start=System.currentTimeMillis(); ! for (int i=0;i<ITERATIONS;i++){ getNormalSignature(TESTSTRINGS[i % TESTSTRINGS.length].getBytes()); } ! dur=System.currentTimeMillis()-start; ! System.out.println(ITERATIONS+" iterations took: "+dur+"ms"); } /** * Silly Microbenchmark * * @throws org.neuclear.commons.crypto.CryptoException */ public void testFileBenchmark() throws CryptoException, NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException { System.out.println("File SigningChannel benchmarks:"); ! File cd=new File("src/java/org/neuclear/commons/crypto"); ! File files[]=cd.listFiles(); long start = System.currentTimeMillis(); ! for (int i=0;i<ITERATIONS;i++){ ! if (files[i% files.length].isFile()) getChannelSignature(files[i % files.length]); } --- 138,163 ---- System.out.println("\nNormal Signing benchmarks:"); ! start = System.currentTimeMillis(); ! for (int i = 0; i < ITERATIONS; i++) { getNormalSignature(TESTSTRINGS[i % TESTSTRINGS.length].getBytes()); } ! dur = System.currentTimeMillis() - start; ! System.out.println(ITERATIONS + " iterations took: " + dur + "ms"); } + /** * Silly Microbenchmark * * @throws org.neuclear.commons.crypto.CryptoException + * */ public void testFileBenchmark() throws CryptoException, NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException { System.out.println("File SigningChannel benchmarks:"); ! File cd = new File("src/java/org/neuclear/commons/crypto"); ! File files[] = cd.listFiles(); long start = System.currentTimeMillis(); ! for (int i = 0; i < ITERATIONS; i++) { ! if (files[i % files.length].isFile()) getChannelSignature(files[i % files.length]); } *************** *** 156,166 **** System.out.println("\nNormal File signing benchmarks:"); ! start=System.currentTimeMillis(); ! for (int i=0;i<ITERATIONS;i++){ ! if (files[i% files.length].isFile()) getNormalSignature(files[i % files.length]); } ! dur=System.currentTimeMillis()-start; ! System.out.println(ITERATIONS+" iterations took: "+dur+"ms"); } --- 166,176 ---- System.out.println("\nNormal File signing benchmarks:"); ! start = System.currentTimeMillis(); ! for (int i = 0; i < ITERATIONS; i++) { ! if (files[i % files.length].isFile()) getNormalSignature(files[i % files.length]); } ! dur = System.currentTimeMillis() - start; ! System.out.println(ITERATIONS + " iterations took: " + dur + "ms"); } |