|
From: <pe...@us...> - 2004-03-08 17:30:38
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/channels In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32741/src/java/org/neuclear/commons/crypto/channels Modified Files: AbstractCryptoChannel.java AbstractEncodingChannel.java AbstractSignatureChannel.java DigestChannel.java SigningChannel.java VerifyingChannel.java Added Files: Base32EncodingChannel.java CipherChannel.java Log Message: Added CipherChannel and the beginnings of a Base32EncodingChannel. The AbstractCryptoChannel now is implemented with a pipe. You can get a readable channel with the source() method. To pipe a ReadableByteChannel or another instance of AbstractCryptoChannel into the channel you can now use the pipe() methods. --- NEW FILE: Base32EncodingChannel.java --- package org.neuclear.commons.crypto.channels; import java.io.IOException; import java.nio.ByteBuffer; /** * Created by IntelliJ IDEA. * User: pelleb * Date: Mar 8, 2004 * Time: 11:38:48 AM * To change this template use File | Settings | File Templates. */ public class Base32EncodingChannel extends AbstractEncodingChannel { public Base32EncodingChannel() throws IOException { chunk=new byte[5]; } int encode(ByteBuffer buffer) throws IOException { int size = buffer.limit()-buffer.position(); int chunkSize= (size<5)?size:5; buffer.get(chunk,0,chunkSize); //TODO finish method return chunkSize; } final byte chunk[]; } --- NEW FILE: CipherChannel.java --- package org.neuclear.commons.crypto.channels; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.BadPaddingException; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.ReadableByteChannel; import java.nio.channels.Pipe; import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.security.SignatureException; /** * Created by IntelliJ IDEA. * User: pelleb * Date: Mar 8, 2004 * Time: 10:06:40 AM * To change this template use File | Settings | File Templates. */ public class CipherChannel extends AbstractCryptoChannel{ public CipherChannel(final Cipher cipher) throws IOException { this.cipher = cipher; } public int write(ByteBuffer buffer) throws IOException { if (closed) throw new ClosedChannelException(); final int size = buffer.limit()-buffer.position(); final int count; if (!buffer.isDirect()) { bytes=buffer.array(); count=buffer.limit(); } else { if (bytes==null) bytes=new byte[size]; count=Math.min(size,bytes.length); buffer.get(bytes,0,count); } write(cipher.update(bytes,0,count)); return count; } public void close() throws IOException { try { write(cipher.doFinal()); super.close(); } catch (IllegalBlockSizeException e) { throw new IOException(e.getLocalizedMessage()); } catch (BadPaddingException e) { throw new IOException(e.getLocalizedMessage()); } } private final Cipher cipher; private byte[] bytes; } Index: AbstractCryptoChannel.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/channels/AbstractCryptoChannel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** AbstractCryptoChannel.java 5 Mar 2004 23:43:06 -0000 1.1 --- AbstractCryptoChannel.java 8 Mar 2004 17:13:54 -0000 1.2 *************** *** 3,6 **** --- 3,10 ---- import java.io.IOException; import java.nio.channels.WritableByteChannel; + import java.nio.channels.Pipe; + import java.nio.channels.ReadableByteChannel; + import java.nio.channels.Channels; + import java.nio.ByteBuffer; /* *************** *** 24,27 **** --- 28,36 ---- $Id$ $Log$ + Revision 1.2 2004/03/08 17:13:54 pelle + Added CipherChannel and the beginnings of a Base32EncodingChannel. + The AbstractCryptoChannel now is implemented with a pipe. You can get a readable channel with the source() method. + To pipe a ReadableByteChannel or another instance of AbstractCryptoChannel into the channel you can now use the pipe() methods. + Revision 1.1 2004/03/05 23:43:06 pelle New Channels package with nio based channels for various crypto related tasks such as digests, signing, verifying and encoding. *************** *** 32,40 **** /** ! * User: pelleb ! * Date: Mar 5, 2004 ! * Time: 11:06:24 PM */ public abstract class AbstractCryptoChannel implements WritableByteChannel { public boolean isOpen() { return !closed; --- 41,51 ---- /** ! * An abstract Channel class used to build various crypto related Channels */ public abstract class AbstractCryptoChannel implements WritableByteChannel { + protected AbstractCryptoChannel() throws IOException { + pipe=Pipe.open(); + } + public boolean isOpen() { return !closed; *************** *** 43,48 **** --- 54,107 ---- public void close() throws IOException { closed = true; + pipe.sink().close(); + } + + /** + * Gets the Readable channel for reading the output of this channel. + * @return + */ + public Pipe.SourceChannel source(){ + return pipe.source(); } + /** + * Used by sub classes to write byte arrays to the output. + * @param data + * @return + * @throws IOException + */ + protected int write(byte data[]) throws IOException { + int count=0; + int written=0; + final ByteBuffer buffer = ByteBuffer.wrap(data); + while( (written=pipe.sink().write(buffer))>0) {count+=written ;}; + return count; + } + + /** + * Read and process all data from a given pipe. + * This closes the input channel when end of stream is reached. + * @param channel + * @throws IOException + */ + public void pipe(ReadableByteChannel channel) throws IOException { + ByteBuffer buffer=ByteBuffer.allocate(128); + while(channel.read(buffer)>=0){ + while(write(buffer)>0){}; + } + channel.close(); + close(); + } + + /** + * Convenience method for linking together multiple AbstractCryptoChannels into a Crypto pipeline. + * @param channel + * @throws IOException + */ + public void pipe(AbstractCryptoChannel channel) throws IOException { + pipe(channel.source()); + } protected boolean closed = false; + private final Pipe pipe; + } Index: AbstractEncodingChannel.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/channels/AbstractEncodingChannel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** AbstractEncodingChannel.java 5 Mar 2004 23:43:06 -0000 1.1 --- AbstractEncodingChannel.java 8 Mar 2004 17:13:54 -0000 1.2 *************** *** 26,29 **** --- 26,34 ---- $Id$ $Log$ + Revision 1.2 2004/03/08 17:13:54 pelle + Added CipherChannel and the beginnings of a Base32EncodingChannel. + The AbstractCryptoChannel now is implemented with a pipe. You can get a readable channel with the source() method. + To pipe a ReadableByteChannel or another instance of AbstractCryptoChannel into the channel you can now use the pipe() methods. + Revision 1.1 2004/03/05 23:43:06 pelle New Channels package with nio based channels for various crypto related tasks such as digests, signing, verifying and encoding. *************** *** 38,46 **** * TODO This should probably be blocking. */ ! public abstract class AbstractEncodingChannel extends AbstractCryptoChannel implements ReadableByteChannel { ! protected AbstractEncodingChannel(int inChunkSize, int outChunkSize) { ! this.outChunkSize = outChunkSize; ! this.inChunkSize = inChunkSize; ! buf = ByteBuffer.allocate(outChunkSize); } --- 43,48 ---- * TODO This should probably be blocking. */ ! public abstract class AbstractEncodingChannel extends AbstractCryptoChannel { ! protected AbstractEncodingChannel() throws IOException { } *************** *** 51,63 **** } ! public int read(ByteBuffer buffer) throws IOException { ! if (closed) ! throw new ClosedChannelException(); ! return 0; ! } - protected final int outChunkSize; - protected final int inChunkSize; - protected final ByteBuffer buf; } --- 53,59 ---- } ! abstract int encode(ByteBuffer buffer) throws IOException; ! } Index: AbstractSignatureChannel.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/channels/AbstractSignatureChannel.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** AbstractSignatureChannel.java 6 Mar 2004 20:50:28 -0000 1.2 --- AbstractSignatureChannel.java 8 Mar 2004 17:13:54 -0000 1.3 *************** *** 28,31 **** --- 28,36 ---- $Id$ $Log$ + Revision 1.3 2004/03/08 17:13:54 pelle + Added CipherChannel and the beginnings of a Base32EncodingChannel. + The AbstractCryptoChannel now is implemented with a pipe. You can get a readable channel with the source() method. + To pipe a ReadableByteChannel or another instance of AbstractCryptoChannel into the channel you can now use the pipe() methods. + Revision 1.2 2004/03/06 20:50:28 pelle Added Unit tests for DigestChannel and SigningChannel. *************** *** 47,59 **** */ public abstract class AbstractSignatureChannel extends AbstractCryptoChannel { ! AbstractSignatureChannel(Signature sig) { this.sig = sig; } ! AbstractSignatureChannel(String alg) throws NoSuchAlgorithmException { this(Signature.getInstance(alg)); } ! AbstractSignatureChannel() throws NoSuchAlgorithmException { this("SHA1withRSA"); } --- 52,64 ---- */ public abstract class AbstractSignatureChannel extends AbstractCryptoChannel { ! AbstractSignatureChannel(Signature sig) throws IOException { this.sig = sig; } ! AbstractSignatureChannel(String alg) throws NoSuchAlgorithmException, IOException { this(Signature.getInstance(alg)); } ! AbstractSignatureChannel() throws NoSuchAlgorithmException, IOException { this("SHA1withRSA"); } Index: DigestChannel.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/channels/DigestChannel.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** DigestChannel.java 6 Mar 2004 20:50:28 -0000 1.2 --- DigestChannel.java 8 Mar 2004 17:13:54 -0000 1.3 *************** *** 1,4 **** --- 1,6 ---- package org.neuclear.commons.crypto.channels; + import org.neuclear.commons.crypto.CryptoException; + import java.io.IOException; import java.nio.ByteBuffer; *************** *** 27,30 **** --- 29,37 ---- $Id$ $Log$ + Revision 1.3 2004/03/08 17:13:54 pelle + Added CipherChannel and the beginnings of a Base32EncodingChannel. + The AbstractCryptoChannel now is implemented with a pipe. You can get a readable channel with the source() method. + To pipe a ReadableByteChannel or another instance of AbstractCryptoChannel into the channel you can now use the pipe() methods. + Revision 1.2 2004/03/06 20:50:28 pelle Added Unit tests for DigestChannel and SigningChannel. *************** *** 44,57 **** */ public class DigestChannel extends AbstractCryptoChannel { ! public DigestChannel() throws NoSuchAlgorithmException { this("SHA1"); } ! public DigestChannel(MessageDigest digest) throws NoSuchAlgorithmException { this.digest = digest; } ! public DigestChannel(String alg) throws NoSuchAlgorithmException { this(MessageDigest.getInstance(alg)); } --- 51,64 ---- */ public class DigestChannel extends AbstractCryptoChannel { ! public DigestChannel() throws NoSuchAlgorithmException, IOException { this("SHA1"); } ! public DigestChannel(MessageDigest digest) throws IOException { this.digest = digest; } ! public DigestChannel(String alg) throws NoSuchAlgorithmException, IOException { this(MessageDigest.getInstance(alg)); } *************** *** 69,72 **** --- 76,85 ---- } + public void close() throws IOException { + bytes=digest.digest(); + write(bytes); + super.close(); + } + /** * Call this to get the Digest *************** *** 74,82 **** * @return */ ! public byte[] getDigest() { ! return digest.digest(); } private final MessageDigest digest; private byte[] bytes; } --- 87,102 ---- * @return */ ! public byte[] getDigest() throws CryptoException { ! try { ! close(); ! source().close(); ! return bytes; ! } catch (IOException e) { ! throw new CryptoException(e); ! } } private final MessageDigest digest; private byte[] bytes; + } Index: SigningChannel.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/channels/SigningChannel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SigningChannel.java 5 Mar 2004 23:43:06 -0000 1.1 --- SigningChannel.java 8 Mar 2004 17:13:54 -0000 1.2 *************** *** 24,27 **** --- 24,32 ---- $Id$ $Log$ + Revision 1.2 2004/03/08 17:13:54 pelle + Added CipherChannel and the beginnings of a Base32EncodingChannel. + The AbstractCryptoChannel now is implemented with a pipe. You can get a readable channel with the source() method. + To pipe a ReadableByteChannel or another instance of AbstractCryptoChannel into the channel you can now use the pipe() methods. + Revision 1.1 2004/03/05 23:43:06 pelle New Channels package with nio based channels for various crypto related tasks such as digests, signing, verifying and encoding. *************** *** 38,59 **** public class SigningChannel extends AbstractSignatureChannel { ! public SigningChannel(Signature sig, PrivateKey key) throws InvalidKeyException { super(sig); sig.initSign(key); } ! public SigningChannel(String alg, PrivateKey key) throws NoSuchAlgorithmException, InvalidKeyException { super(alg); sig.initSign(key); } ! public SigningChannel(PrivateKey key) throws NoSuchAlgorithmException, InvalidKeyException { sig.initSign(key); } public byte[] getSignature() throws SignatureException, IOException { - byte signature[] = sig.sign(); close(); return signature; } } --- 43,76 ---- public class SigningChannel extends AbstractSignatureChannel { ! public SigningChannel(Signature sig, PrivateKey key) throws InvalidKeyException, IOException { super(sig); sig.initSign(key); } ! public SigningChannel(String alg, PrivateKey key) throws NoSuchAlgorithmException, InvalidKeyException, IOException { super(alg); sig.initSign(key); } ! public SigningChannel(PrivateKey key) throws NoSuchAlgorithmException, InvalidKeyException, IOException { sig.initSign(key); } + public void close() throws IOException { + try { + signature = sig.sign(); + write(signature); + } catch (SignatureException e) { + throw new IOException(e.getLocalizedMessage()); + } + super.close(); + } + public byte[] getSignature() throws SignatureException, IOException { close(); + source().close(); return signature; } + + private byte[] signature; } Index: VerifyingChannel.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/channels/VerifyingChannel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** VerifyingChannel.java 5 Mar 2004 23:43:06 -0000 1.1 --- VerifyingChannel.java 8 Mar 2004 17:13:54 -0000 1.2 *************** *** 24,27 **** --- 24,32 ---- $Id$ $Log$ + Revision 1.2 2004/03/08 17:13:54 pelle + Added CipherChannel and the beginnings of a Base32EncodingChannel. + The AbstractCryptoChannel now is implemented with a pipe. You can get a readable channel with the source() method. + To pipe a ReadableByteChannel or another instance of AbstractCryptoChannel into the channel you can now use the pipe() methods. + Revision 1.1 2004/03/05 23:43:06 pelle New Channels package with nio based channels for various crypto related tasks such as digests, signing, verifying and encoding. *************** *** 36,57 **** public class VerifyingChannel extends AbstractSignatureChannel { ! public VerifyingChannel(Signature sig, PublicKey key) throws InvalidKeyException { super(sig); sig.initVerify(key); } ! public VerifyingChannel(String alg, PublicKey key) throws NoSuchAlgorithmException, InvalidKeyException { super(alg); sig.initVerify(key); } ! public VerifyingChannel(PublicKey key) throws NoSuchAlgorithmException, InvalidKeyException { sig.initVerify(key); } public boolean verify(byte signature[]) throws SignatureException, IOException { - boolean verified = sig.verify(signature); close(); ! return verified; } } --- 41,62 ---- public class VerifyingChannel extends AbstractSignatureChannel { ! public VerifyingChannel(Signature sig, PublicKey key) throws InvalidKeyException, IOException { super(sig); sig.initVerify(key); } ! public VerifyingChannel(String alg, PublicKey key) throws NoSuchAlgorithmException, InvalidKeyException, IOException { super(alg); sig.initVerify(key); } ! public VerifyingChannel(PublicKey key) throws NoSuchAlgorithmException, InvalidKeyException, IOException { sig.initVerify(key); } public boolean verify(byte signature[]) throws SignatureException, IOException { close(); ! source().close(); ! return sig.verify(signature); } } |