2016-03-29 23:25:18 +02:00
|
|
|
package org.dclermonte.siba.gui;
|
|
|
|
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
import javax.swing.JLabel;
|
|
|
|
import javax.swing.JButton;
|
|
|
|
import javax.swing.JFileChooser;
|
|
|
|
|
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import java.awt.event.MouseEvent;
|
2016-04-07 00:54:03 +02:00
|
|
|
import java.io.ByteArrayInputStream;
|
2016-03-29 23:25:18 +02:00
|
|
|
import java.io.File;
|
2016-04-07 00:54:03 +02:00
|
|
|
import java.io.FileFilter;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
2016-03-29 23:25:18 +02:00
|
|
|
import com.jgoodies.forms.layout.FormLayout;
|
|
|
|
import com.jgoodies.forms.layout.ColumnSpec;
|
|
|
|
import com.jgoodies.forms.layout.FormSpecs;
|
|
|
|
import com.jgoodies.forms.layout.RowSpec;
|
|
|
|
import com.jgoodies.forms.layout.Sizes;
|
|
|
|
import javax.swing.JTextField;
|
2016-04-07 00:54:03 +02:00
|
|
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
2016-03-29 23:25:18 +02:00
|
|
|
|
|
|
|
public class CheckPanel extends JPanel
|
|
|
|
{
|
|
|
|
private JTextField textField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the panel.
|
|
|
|
*/
|
|
|
|
public CheckPanel()
|
|
|
|
{
|
|
|
|
setLayout(new FormLayout(new ColumnSpec[] {
|
|
|
|
ColumnSpec.decode("2dlu"),
|
|
|
|
new ColumnSpec(ColumnSpec.LEFT, Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true), Sizes.constant("60dlu", true)), 1),
|
|
|
|
new ColumnSpec(ColumnSpec.LEFT, Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true), Sizes.constant("100dlu", true)), 1),
|
|
|
|
ColumnSpec.decode("30dlu"),},
|
|
|
|
new RowSpec[] {
|
|
|
|
FormSpecs.LINE_GAP_ROWSPEC,
|
|
|
|
RowSpec.decode("25px"),
|
|
|
|
FormSpecs.RELATED_GAP_ROWSPEC,
|
|
|
|
FormSpecs.DEFAULT_ROWSPEC,}));
|
|
|
|
|
|
|
|
JLabel lblNewLabel = new JLabel("Sauvegarde à vérifier");
|
|
|
|
add(lblNewLabel, "2, 2, left, center");
|
|
|
|
|
|
|
|
JButton btnNewButton = new JButton("...");
|
|
|
|
btnNewButton.addMouseListener(new MouseAdapter() {
|
|
|
|
@Override
|
|
|
|
public void mouseClicked(MouseEvent e) {
|
2016-04-07 00:54:03 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
repertoireChoisi();
|
|
|
|
}
|
|
|
|
catch (IOException e1)
|
|
|
|
{
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
catch (NoSuchAlgorithmException e1)
|
|
|
|
{
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
2016-03-29 23:25:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
textField = new JTextField();
|
|
|
|
add(textField, "3, 2, fill, default");
|
|
|
|
textField.setColumns(10);
|
|
|
|
add(btnNewButton, "4, 2, left, top");
|
|
|
|
|
|
|
|
JButton btnNewButton_1 = new JButton("Check");
|
|
|
|
add(btnNewButton_1, "3, 4");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-07 00:54:03 +02:00
|
|
|
final void repertoireChoisi() throws IOException, NoSuchAlgorithmException {
|
2016-03-29 23:25:18 +02:00
|
|
|
final JFileChooser fc = new JFileChooser();
|
2016-04-07 00:54:03 +02:00
|
|
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("Fichiers md5","md5");
|
|
|
|
fc.addChoosableFileFilter(filter);
|
|
|
|
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
|
|
|
|
2016-03-29 23:25:18 +02:00
|
|
|
int returnVal = fc.showDialog(CheckPanel.this,"Sauvegarde à vérifier");
|
|
|
|
if (returnVal == JFileChooser.APPROVE_OPTION){
|
|
|
|
File file = fc.getSelectedFile();
|
2016-04-07 00:54:03 +02:00
|
|
|
textField.setText(file.getPath());
|
|
|
|
FileReader fileReader = new FileReader(file);
|
|
|
|
char[] md5 = new char[32];
|
|
|
|
byte[] md5byte = new byte[32];
|
|
|
|
char[] fileToCheck = new char[(int) (file.length())-32];
|
|
|
|
for (int index=0;index<32;index++){
|
|
|
|
md5[index]= (char) fileReader.read();
|
|
|
|
md5byte[index]=(byte) md5[index] ;
|
|
|
|
}
|
|
|
|
fileReader.read();
|
|
|
|
String fileNameToString = new String();
|
|
|
|
for (int index=36;index<file.length()+3;index++){
|
|
|
|
fileToCheck[index-36]= (char) fileReader.read();
|
|
|
|
}
|
|
|
|
fileNameToString= String.copyValueOf(fileToCheck);
|
|
|
|
File fileToCheck1 = new File(fileNameToString);
|
|
|
|
fileReader.close();
|
|
|
|
byte[] bytedirectoryToSave = new byte[(int) fileToCheck.length] ;
|
|
|
|
FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
|
for ( int index1 = 0 ;index1< fileNameToString.length();index1++)
|
|
|
|
{
|
|
|
|
bytedirectoryToSave[index1] = (byte) fileInputStream.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
byte[] hash = null;
|
|
|
|
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
|
|
|
hash = messageDigest.digest(bytedirectoryToSave);
|
|
|
|
if (messageDigest.isEqual(hash, md5byte)){
|
|
|
|
System.out.println("gagné");}
|
|
|
|
else {
|
|
|
|
System.out.println("Perdu");
|
|
|
|
}
|
2016-03-29 23:25:18 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 00:54:03 +02:00
|
|
|
}
|
2016-03-29 23:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|