|
From: <pe...@us...> - 2004-03-06 22:09:01
|
Update of /cvsroot/neuclear/neuclear-commons/src/test/org/neuclear/commons/crypto/channels In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20550/src/test/org/neuclear/commons/crypto/channels Modified Files: SigningChannelTest.java Log Message: Now verify works as well Index: SigningChannelTest.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/test/org/neuclear/commons/crypto/channels/SigningChannelTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SigningChannelTest.java 6 Mar 2004 20:50:28 -0000 1.1 --- SigningChannelTest.java 6 Mar 2004 21:53:51 -0000 1.2 *************** *** 29,33 **** public void testSign() throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { for (int i=0;i<TESTSTRINGS.length;i++){ ! assertSignatureEquals(TESTSTRINGS[i]); } --- 29,33 ---- public void testSign() throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { for (int i=0;i<TESTSTRINGS.length;i++){ ! assertSignatureEquals(TESTSTRINGS[i].getBytes()); } *************** *** 44,83 **** } ! public String getChannelSignature(String data) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { SigningChannel ch=new SigningChannel(kp.getPrivate()); ! ByteBuffer buf=ByteBuffer.wrap(data.getBytes()); ch.write(buf); ! return new String(ch.getSignature()); } ! public String getNormalSignature(String data) throws CryptoException { ! return new String(CryptoTools.sign(kp,data.getBytes())); } ! public boolean verifyChannelSignature(String data,String sig) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { VerifyingChannel ch=new VerifyingChannel(kp.getPublic()); ! ByteBuffer buf=ByteBuffer.wrap(data.getBytes()); ch.write(buf); ! return ch.verify(sig.getBytes()); } ! public boolean verifyNormalSignature(String data,String sig) throws CryptoException { ! return CryptoTools.verify(kp.getPublic(),data.getBytes(),sig.getBytes()); } ! public void assertSignatureEquals(String data) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException, CryptoException { ! String sig=getChannelSignature(data); ! assertEquals("Signature Match",getNormalSignature(data),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 { ! String sig=getChannelSignature(file); ! assertEquals("Signature Match",getNormalSignature(file),sig); } ! public String 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 new String(ch.getSignature()); } ! public String getNormalSignature(File file) throws NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException { Signature sig=Signature.getInstance("SHA1withRSA"); sig.initSign(kp.getPrivate()); --- 44,91 ---- } ! 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()); *************** *** 90,94 **** } in.close(); ! return new String(sig.sign()); } --- 98,115 ---- } 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(); ! return sig.verify(sigb); } *************** *** 103,107 **** long start = System.currentTimeMillis(); for (int i = 0; i < ITERATIONS; i++) { ! getChannelSignature(TESTSTRINGS[i % TESTSTRINGS.length]); } long dur = System.currentTimeMillis() - start; --- 124,128 ---- long start = System.currentTimeMillis(); for (int i = 0; i < ITERATIONS; i++) { ! getChannelSignature(TESTSTRINGS[i % TESTSTRINGS.length].getBytes()); } long dur = System.currentTimeMillis() - start; *************** *** 111,115 **** start=System.currentTimeMillis(); for (int i=0;i<ITERATIONS;i++){ ! getNormalSignature(TESTSTRINGS[i % TESTSTRINGS.length]); } dur=System.currentTimeMillis()-start; --- 132,136 ---- start=System.currentTimeMillis(); for (int i=0;i<ITERATIONS;i++){ ! getNormalSignature(TESTSTRINGS[i % TESTSTRINGS.length].getBytes()); } dur=System.currentTimeMillis()-start; |