|
From: Pelle B. <pe...@us...> - 2004-03-31 19:00:18
|
Update of /cvsroot/neuclear/neuclear-commons/src/test/org/neuclear/commons/crypto/streams In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6205/src/test/org/neuclear/commons/crypto/streams Added Files: CryptoStreamTest.java DigestOutputStreamTest.java SignatureStreamTest.java Log Message: Added various Streams for simplified crypto operations. --- NEW FILE: DigestOutputStreamTest.java --- package org.neuclear.commons.crypto.streams; import junit.framework.TestCase; import org.neuclear.commons.crypto.CryptoTools; import java.io.IOException; import java.security.NoSuchAlgorithmException; /* $Id: DigestOutputStreamTest.java,v 1.1 2004/03/31 18:48:28 pelle Exp $ $Log: DigestOutputStreamTest.java,v $ Revision 1.1 2004/03/31 18:48:28 pelle Added various Streams for simplified crypto operations. */ /** * User: pelleb * Date: Mar 31, 2004 * Time: 11:16:58 AM */ public class DigestOutputStreamTest extends TestCase { public DigestOutputStreamTest(String name) { super(name); } public void testStrings() throws NoSuchAlgorithmException, IOException { assertDigestEquals(""); assertDigestEquals("1"); assertDigestEquals("12"); assertDigestEquals("12345678890--"); assertDigestEquals("00000000000000000000000000000000000000000000000"); assertDigestEquals("the quick brown fox jumped over the lazy dog"); } public void assertDigestEquals(String test) throws NoSuchAlgorithmException, IOException { assertEquals(new String(CryptoTools.digest(test)), getStreamDigest(test)); } public String getStreamDigest(String data) throws NoSuchAlgorithmException, IOException { DigestOutputStream dig = new DigestOutputStream(); dig.write(data.getBytes()); return new String(dig.getDigest()); } } --- NEW FILE: CryptoStreamTest.java --- package org.neuclear.commons.crypto.streams; import junit.framework.TestCase; import org.neuclear.commons.crypto.CryptoException; import org.neuclear.commons.crypto.CryptoTools; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.KeyPair; import java.security.NoSuchAlgorithmException; /* $Id: CryptoStreamTest.java,v 1.1 2004/03/31 18:48:27 pelle Exp $ $Log: CryptoStreamTest.java,v $ Revision 1.1 2004/03/31 18:48:27 pelle Added various Streams for simplified crypto operations. */ /** * User: pelleb * Date: Mar 31, 2004 * Time: 11:16:58 AM */ public class CryptoStreamTest extends TestCase { public CryptoStreamTest(String name) throws NoSuchAlgorithmException { super(name); rsa = CryptoTools.createTinyRSAKeyPair(); dsa = CryptoTools.createTinyDSAKeyPair(); } public void testStrings() throws GeneralSecurityException, IOException, CryptoException { assertEncryptDecrypt(""); assertEncryptDecrypt("1"); assertEncryptDecrypt("12"); assertEncryptDecrypt("12345678890--"); assertEncryptDecrypt("00000000000000000000000000000000000000000000000"); assertEncryptDecrypt("the quick brown fox jumped over the lazy dog"); } public void assertEncryptDecrypt(String test) throws GeneralSecurityException, IOException, CryptoException { final byte[] bytes = test.getBytes("UTF-8"); final byte[] encrypted = encrypt(bytes); assertNotNull(encrypted); final byte[] decrypted = decrypt(encrypted); assertNotNull(decrypted); assertEquals(bytes, decrypted); assertBack2BackOutputStreams(bytes); assertBack2BackInputStreams(bytes); } public byte[] encrypt(byte data[]) throws GeneralSecurityException, IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PasswordEncryptingOutputStream sos = new PasswordEncryptingOutputStream(bos, "super secret squirrel".toCharArray()); sos.write(data); sos.close(); return bos.toByteArray(); } public byte[] decrypt(byte data[]) throws GeneralSecurityException, IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PasswordDecryptingOutputStream sos = new PasswordDecryptingOutputStream(bos, "super secret squirrel".toCharArray()); sos.write(data); sos.close(); return bos.toByteArray(); } public void assertBack2BackOutputStreams(byte data[]) throws GeneralSecurityException, IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PasswordDecryptingOutputStream eos = new PasswordDecryptingOutputStream(bos, "super secret squirrel".toCharArray()); PasswordEncryptingOutputStream sos = new PasswordEncryptingOutputStream(eos, "super secret squirrel".toCharArray()); sos.write(data); sos.close(); assertEquals(data, bos.toByteArray()); } public void assertBack2BackInputStreams(byte data[]) throws GeneralSecurityException, IOException { ByteArrayInputStream bis = new ByteArrayInputStream(data); PasswordEncryptingInputStream eos = new PasswordEncryptingInputStream(bis, "super secret squirrel".toCharArray()); PasswordDecryptingInputStream sos = new PasswordDecryptingInputStream(eos, "super secret squirrel".toCharArray()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b = sos.read(); while (b != -1) { bos.write(b); b = sos.read(); } assertEquals(data, bos.toByteArray()); } public void assertEquals(byte a[], byte b[]) { assertTrue((a == null) == (b == null)); if (a == null) return; assertEquals(a.length, b.length); for (int i = 0; i < a.length; i++) { assertEquals(a[i], b[i]); } } private KeyPair rsa; private KeyPair dsa; } --- NEW FILE: SignatureStreamTest.java --- package org.neuclear.commons.crypto.streams; import junit.framework.TestCase; import org.neuclear.commons.crypto.CryptoException; import org.neuclear.commons.crypto.CryptoTools; import java.io.IOException; import java.security.InvalidKeyException; import java.security.KeyPair; import java.security.NoSuchAlgorithmException; import java.security.SignatureException; /* $Id: SignatureStreamTest.java,v 1.1 2004/03/31 18:48:28 pelle Exp $ $Log: SignatureStreamTest.java,v $ Revision 1.1 2004/03/31 18:48:28 pelle Added various Streams for simplified crypto operations. */ /** * User: pelleb * Date: Mar 31, 2004 * Time: 11:16:58 AM */ public class SignatureStreamTest extends TestCase { public SignatureStreamTest(String name) throws NoSuchAlgorithmException { super(name); rsa = CryptoTools.createTinyRSAKeyPair(); dsa = CryptoTools.createTinyDSAKeyPair(); } public void testStrings() throws NoSuchAlgorithmException, IOException, CryptoException, SignatureException, InvalidKeyException { assertSignatureEquals(""); assertSignatureEquals("1"); assertSignatureEquals("12"); assertSignatureEquals("12345678890--"); assertSignatureEquals("00000000000000000000000000000000000000000000000"); assertSignatureEquals("the quick brown fox jumped over the lazy dog"); } public void assertSignatureEquals(String test) throws NoSuchAlgorithmException, IOException, CryptoException, SignatureException, InvalidKeyException { final byte[] bytes = test.getBytes("UTF-8"); assertSignature(rsa, bytes); // assertSignature(dsa,bytes); I have to disable this as CryptoTools converts the signature output for DSA in a way that is required of XMLSignature } private void assertSignature(KeyPair kp, final byte[] bytes) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException, CryptoException { final byte[] ssig = getStreamSignature(kp, bytes); assertEquals(CryptoTools.sign(kp, bytes), ssig); assertEquals(CryptoTools.verify(kp.getPublic(), bytes, ssig), getStreamVerifier(kp, bytes, ssig)); ssig[0] = (byte) (ssig[0] & 1); // tamper with signature assertFalse(getStreamVerifier(kp, bytes, ssig)); } public byte[] getStreamSignature(KeyPair kp, byte data[]) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { SigningOutputStream sig = new SigningOutputStream(kp); sig.write(data); return sig.sign(); } public boolean getStreamVerifier(KeyPair kp, byte data[], byte sig[]) throws NoSuchAlgorithmException, IOException, InvalidKeyException, SignatureException { VerifyingOutputStream ver = new VerifyingOutputStream(kp, sig); ver.write(data); return ver.verify(); } public void assertEquals(byte a[], byte b[]) { assertTrue((a == null) == (b == null)); assertEquals(a.length, b.length); for (int i = 0; i < a.length; i++) { assertEquals(a[i], b[i]); } } private KeyPair rsa; private KeyPair dsa; } |