1 package test.base.jdbs.cryptography.asymmetric;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.GregorianCalendar;
7
8 import org.apache.log4j.Logger;
9 import org.apache.log4j.xml.DOMConfigurator;
10
11 import base.jdbs.cryptography.asymmetric.AsymmetricKeyRing;
12 import base.jdbs.cryptography.asymmetric.KeyPair;
13 import base.jdbs.cryptography.asymmetric.RSAAsymmetricCipher;
14 import base.jdbs.cryptography.asymmetric.RSAKeyPairFactory;
15 import base.user.Document;
16 import base.user.User;
17 import base.user.UserCredential;
18 import base.user.UserFactory;
19 import base.user.UserGender;
20 import base.util.FileUtil;
21 import junit.framework.TestCase;
22
23 public class RSAAsymmetricCipherTest extends TestCase {
24
25 private static final transient Logger logger = Logger.getLogger(RSAAsymmetricCipherTest.class.getName());
26
27 private static final boolean RUN_SILENTLY = true;
28
29 private static final String INPUT_FILE_NAME = "signatureTest.txt";
30 private static final String OUTPUT_FILE_NAME = "signatureTest.signed.txt";
31
32 private RSAAsymmetricCipher RSAAsymmetricCipher = new RSAAsymmetricCipher();
33 private AsymmetricKeyRing asymmetricKeyRing;
34 private KeyPair keyPair;
35
36
37
38
39 @Override
40 protected void setUp() throws Exception {
41 DOMConfigurator.configure("log4jConfiguration.xml");
42 asymmetricKeyRing = new AsymmetricKeyRing(testUser());
43 keyPair = RSAKeyPairFactory.newKeyPair(asymmetricKeyRing,"Test Key Pair");
44 asymmetricKeyRing.addKeyPair(keyPair);
45 }
46
47 private User testUser(){
48
49 String name = "Guido Angelo";
50 String surname = "Ingenito";
51 String gender = UserGender.USER_GENDER[UserGender.MALE];
52 GregorianCalendar birthday = new GregorianCalendar(1981,12,24);
53 String nickname = "skunk";
54 String password = "password";
55 String credential = UserCredential.USER_CREDENTIAL[UserCredential.ADMINISTRATOR];
56 Document document = null;
57 String imagePath = "";
58
59 return UserFactory.newUser(name, surname, gender, birthday.getTime(), nickname, password, credential, document, imagePath);
60 }
61
62
63
64
65 public void testSign() throws IOException {
66 if(RUN_SILENTLY){
67 File inputFile = new File(INPUT_FILE_NAME);
68 inputFile.createNewFile();
69 inputFile.deleteOnExit();
70 File outputFile = new File(OUTPUT_FILE_NAME);
71 outputFile.createNewFile();
72 outputFile.deleteOnExit();
73
74 FileWriter fileWriter = new FileWriter(inputFile);
75 fileWriter.append("THIS IS A TEST STRING TO BE SIGNED!");
76 fileWriter.close();
77
78 RSAAsymmetricCipher.sign(inputFile, outputFile, keyPair.getPrivateKey());
79
80 logger.debug("Clear File Content:\n" + FileUtil.fileContent(inputFile));
81 logger.debug("Signed File Content:\n" + FileUtil.fileContent(outputFile));
82
83 boolean result = RSAAsymmetricCipher.checkSignature(outputFile,inputFile,keyPair.getPublicKey());
84
85 assertTrue(result);
86
87 }
88 else{
89 File inputFile = null;
90 File outputFile = null;
91
92 javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
93 chooser.setDialogTitle("Select the input file to be signed...");
94 chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
95 chooser.setApproveButtonText("Select");
96 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION){
97 inputFile = new File(chooser.getSelectedFile().getAbsolutePath());
98 outputFile = new File(chooser.getSelectedFile().getAbsolutePath()+".signed");
99 }
100 if(inputFile != null && outputFile != null){
101 RSAAsymmetricCipher.sign(inputFile, outputFile, keyPair.getPrivateKey());
102 boolean result = RSAAsymmetricCipher.checkSignature(outputFile,inputFile, keyPair.getPublicKey());
103 assertTrue(result);
104 }
105 }
106 }
107 }