|
From: <pe...@us...> - 2004-03-05 23:57:35
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/channels In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29887/src/java/org/neuclear/commons/crypto/channels Added Files: AbstractCryptoChannel.java AbstractEncodingChannel.java AbstractSignatureChannel.java DigestChannel.java SigningChannel.java VerifyingChannel.java Log Message: New Channels package with nio based channels for various crypto related tasks such as digests, signing, verifying and encoding. DigestsChannel, SigningChannel and VerifyingChannel are complete, but not tested. AbstractEncodingChannel will be used for a Base64/Base32 Channel as well as possibly an xml canonicalization channel in the xmlsig library. --- NEW FILE: AbstractCryptoChannel.java --- package org.neuclear.commons.crypto.channels; import java.io.IOException; import java.nio.channels.WritableByteChannel; /* NeuClear Distributed Transaction Clearing Platform (C) 2003 Pelle Braendgaard This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: AbstractCryptoChannel.java,v 1.1 2004/03/05 23:43:06 pelle Exp $ $Log: AbstractCryptoChannel.java,v $ 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. DigestsChannel, SigningChannel and VerifyingChannel are complete, but not tested. AbstractEncodingChannel will be used for a Base64/Base32 Channel as well as possibly an xml canonicalization channel in the xmlsig library. */ /** * User: pelleb * Date: Mar 5, 2004 * Time: 11:06:24 PM */ public abstract class AbstractCryptoChannel implements WritableByteChannel { public boolean isOpen() { return !closed; } public void close() throws IOException { closed = true; } protected boolean closed = false; } --- NEW FILE: AbstractEncodingChannel.java --- package org.neuclear.commons.crypto.channels; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.ReadableByteChannel; /* NeuClear Distributed Transaction Clearing Platform (C) 2003 Pelle Braendgaard This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: AbstractEncodingChannel.java,v 1.1 2004/03/05 23:43:06 pelle Exp $ $Log: AbstractEncodingChannel.java,v $ 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. DigestsChannel, SigningChannel and VerifyingChannel are complete, but not tested. AbstractEncodingChannel will be used for a Base64/Base32 Channel as well as possibly an xml canonicalization channel in the xmlsig library. */ /** * This is an AbstractChannel for encodings such as base64 etc. * You write raw data to it and read the encoded data with read(). * 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); } public int write(ByteBuffer buffer) throws IOException { if (closed) throw new ClosedChannelException(); return 0; } 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; } --- NEW FILE: AbstractSignatureChannel.java --- package org.neuclear.commons.crypto.channels; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.security.NoSuchAlgorithmException; import java.security.Signature; import java.security.SignatureException; /* NeuClear Distributed Transaction Clearing Platform (C) 2003 Pelle Braendgaard This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: AbstractSignatureChannel.java,v 1.1 2004/03/05 23:43:06 pelle Exp $ $Log: AbstractSignatureChannel.java,v $ 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. DigestsChannel, SigningChannel and VerifyingChannel are complete, but not tested. AbstractEncodingChannel will be used for a Base64/Base32 Channel as well as possibly an xml canonicalization channel in the xmlsig library. */ /** * User: pelleb * Date: Mar 5, 2004 * Time: 11:08:47 PM */ 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"); } public int write(ByteBuffer buffer) throws IOException { if (closed) throw new ClosedChannelException(); final byte[] bytes = buffer.array(); try { sig.update(buffer.array()); } catch (SignatureException e) { throw new IOException(e.getLocalizedMessage()); } return bytes.length; } protected final Signature sig; } --- NEW FILE: DigestChannel.java --- package org.neuclear.commons.crypto.channels; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /* NeuClear Distributed Transaction Clearing Platform (C) 2003 Pelle Braendgaard This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: DigestChannel.java,v 1.1 2004/03/05 23:43:06 pelle Exp $ $Log: DigestChannel.java,v $ 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. DigestsChannel, SigningChannel and VerifyingChannel are complete, but not tested. AbstractEncodingChannel will be used for a Base64/Base32 Channel as well as possibly an xml canonicalization channel in the xmlsig library. */ /** * WritableByteChannel for producing SHA! Digests from ByteBuffers */ 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)); } public int write(ByteBuffer buffer) throws IOException { if (closed) throw new ClosedChannelException(); final byte[] bytes = buffer.array(); digest.update(bytes); return bytes.length; } /** * Call this to get the Digest * * @return */ public byte[] getDigest() { return digest.digest(); } private final MessageDigest digest; } --- NEW FILE: SigningChannel.java --- package org.neuclear.commons.crypto.channels; import java.io.IOException; import java.security.*; /* NeuClear Distributed Transaction Clearing Platform (C) 2003 Pelle Braendgaard This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: SigningChannel.java,v 1.1 2004/03/05 23:43:06 pelle Exp $ $Log: SigningChannel.java,v $ 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. DigestsChannel, SigningChannel and VerifyingChannel are complete, but not tested. AbstractEncodingChannel will be used for a Base64/Base32 Channel as well as possibly an xml canonicalization channel in the xmlsig library. */ /** * User: pelleb * Date: Mar 5, 2004 * Time: 11:18:36 PM */ 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; } } --- NEW FILE: VerifyingChannel.java --- package org.neuclear.commons.crypto.channels; import java.io.IOException; import java.security.*; /* NeuClear Distributed Transaction Clearing Platform (C) 2003 Pelle Braendgaard This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: VerifyingChannel.java,v 1.1 2004/03/05 23:43:06 pelle Exp $ $Log: VerifyingChannel.java,v $ 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. DigestsChannel, SigningChannel and VerifyingChannel are complete, but not tested. AbstractEncodingChannel will be used for a Base64/Base32 Channel as well as possibly an xml canonicalization channel in the xmlsig library. */ /** * Channel that reads data and verifies it against a public key */ 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; } } |