Line | Hits | Source |
---|---|---|
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 | ||
28 | 2 | import org.apache.log4j.Logger; |
29 | ||
30 | 3 | public class RSAAsymmetricCipher implements IAsymmetricCipher { |
31 | ||
32 | 1 | private static final transient Logger logger = Logger.getLogger(RSAAsymmetricCipher.class.getName()); |
33 | ||
34 | 0 | public void sign(File input, File output, base.jdbs.cryptography.asymmetric.PrivateKey privateKey) { |
35 | 0 | |
36 | 0 | try{ |
37 | ||
38 | 0 | /* Create a Signature object and initialize it with the private key */ |
39 | 0 | Signature rsa = Signature.getInstance("SHA1withRSA"); |
40 | 0 | rsa.initSign(privateKey.getValue()); |
41 | 0 | |
42 | 0 | /* Update and sign the data */ |
43 | 0 | FileInputStream fileInputStream = new FileInputStream(input); |
44 | 0 | BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); |
45 | 0 | byte[] buffer = new byte[1024]; |
46 | 0 | int numRead; |
47 | 0 | while (bufferedInputStream.available() != 0) { |
48 | 0 | numRead = bufferedInputStream.read(buffer); |
49 | 0 | rsa.update(buffer, 0, numRead); |
50 | 0 | }; |
51 | ||
52 | 0 | bufferedInputStream.close(); |
53 | ||
54 | 0 | /* Now that all the data to be signed has been read in, |
55 | 0 | generate a signature for it */ |
56 | 0 | byte[] realSig = rsa.sign(); |
57 | 0 | |
58 | /* Save the signature in a file */ | |
59 | 0 | FileOutputStream sigfos = new FileOutputStream(output); |
60 | 0 | sigfos.write(realSig); |
61 | 0 | sigfos.close(); |
62 | 0 | } catch (Exception e) { |
63 | 0 | logger.error("Caught exception " + e.toString()); |
64 | 0 | } |
65 | 0 | } |
66 | 0 | |
67 | public boolean checkSignature(File signedInputFile, File dataFile, PublicKey publicKey) { | |
68 | try{ | |
69 | /* input the signature bytes */ | |
70 | 0 | FileInputStream sigfis = new FileInputStream(signedInputFile); |
71 | 0 | byte[] sigToVerify = new byte[sigfis.available()]; |
72 | 0 | sigfis.read(sigToVerify ); |
73 | ||
74 | 0 | sigfis.close(); |
75 | ||
76 | /* create a Signature object and initialize it with the public key */ | |
77 | 0 | Signature sig = Signature.getInstance("SHA1withRSA"); |
78 | 0 | sig.initVerify(publicKey.getValue()); |
79 | ||
80 | /* Update and verify the data */ | |
81 | ||
82 | 0 | FileInputStream datafis = new FileInputStream(dataFile); |
83 | 0 | BufferedInputStream bufin = new BufferedInputStream(datafis); |
84 | ||
85 | 0 | byte[] buffer = new byte[1024]; |
86 | int len; | |
87 | 0 | while (bufin.available() != 0) { |
88 | 0 | len = bufin.read(buffer); |
89 | 0 | sig.update(buffer, 0, len); |
90 | 0 | }; |
91 | ||
92 | 0 | bufin.close(); |
93 | ||
94 | ||
95 | 0 | boolean verifies = sig.verify(sigToVerify); |
96 | ||
97 | 0 | System.out.println("signature verifies: " + verifies); |
98 | ||
99 | 0 | return verifies; |
100 | ||
101 | 0 | } catch (Exception e) { |
102 | 0 | System.err.println("Caught exception " + e.toString()); |
103 | } | |
104 | 0 | return false; |
105 | } | |
106 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |