Coverage details for base.jdbs.cryptography.asymmetric.RSAAsymmetricCipher

LineHitsSource
1 /*******************************************************************************
2  *InternetCafe is a software solution that helps the management of Cybercafes
3  * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4  * Copyright (C) 2006 Guido Angelo Ingenito
5  
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *******************************************************************************/
20 package base.jdbs.cryptography.asymmetric;
21  
22 import java.io.BufferedInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.security.Signature;
27  
282import org.apache.log4j.Logger;
29  
303public class RSAAsymmetricCipher implements IAsymmetricCipher {
31     
321    private static final transient Logger logger = Logger.getLogger(RSAAsymmetricCipher.class.getName());
33     
340    public void sign(File input, File output, base.jdbs.cryptography.asymmetric.PrivateKey privateKey) {
350        
360        try{
37             
380            /* Create a Signature object and initialize it with the private key */
390            Signature rsa = Signature.getInstance("SHA1withRSA");
400            rsa.initSign(privateKey.getValue());
410    
420            /* Update and sign the data */
430            FileInputStream fileInputStream = new FileInputStream(input);
440            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
450            byte[] buffer = new byte[1024];
460            int numRead;
470            while (bufferedInputStream.available() != 0) {
480                numRead = bufferedInputStream.read(buffer);
490                rsa.update(buffer, 0, numRead);
500            };
51             
520            bufferedInputStream.close();
53             
540            /* Now that all the data to be signed has been read in,
550             generate a signature for it */
560            byte[] realSig = rsa.sign();
570        
58             /* Save the signature in a file */
590            FileOutputStream sigfos = new FileOutputStream(output);
600            sigfos.write(realSig);
610            sigfos.close();
620        } catch (Exception e) {
630            logger.error("Caught exception " + e.toString());
640        }
650    }
660    
67     public boolean checkSignature(File signedInputFile, File dataFile, PublicKey publicKey) {
68         try{
69         /* input the signature bytes */
700        FileInputStream sigfis = new FileInputStream(signedInputFile);
710        byte[] sigToVerify = new byte[sigfis.available()];
720        sigfis.read(sigToVerify );
73  
740        sigfis.close();
75  
76         /* create a Signature object and initialize it with the public key */
770        Signature sig = Signature.getInstance("SHA1withRSA");
780        sig.initVerify(publicKey.getValue());
79  
80         /* Update and verify the data */
81  
820        FileInputStream datafis = new FileInputStream(dataFile);
830        BufferedInputStream bufin = new BufferedInputStream(datafis);
84  
850        byte[] buffer = new byte[1024];
86         int len;
870        while (bufin.available() != 0) {
880            len = bufin.read(buffer);
890            sig.update(buffer, 0, len);
900            };
91  
920        bufin.close();
93  
94  
950        boolean verifies = sig.verify(sigToVerify);
96  
970        System.out.println("signature verifies: " + verifies);
98  
990            return verifies;
100  
1010    } catch (Exception e) {
1020        System.err.println("Caught exception " + e.toString());
103     }
1040    return false;
105     }
106 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.