Siba/src/org/dclermonte/siba/gui/CheckPanel.java

212 lines
5.9 KiB
Java

/*
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
* Didier Clermonté <dclermonte@april.org>
*
* This file is part of Siba.
*
* Siba is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dclermonte.siba.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;
/**
*
* @author papa
*/
public class CheckPanel extends JPanel
{
private static final long serialVersionUID = -1580485684838045920L;
private JTextField textField;
private File choosenFile;
/**
* 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.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
try
{
choosenDirectory();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (NoSuchAlgorithmException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
this.textField = new JTextField();
add(this.textField, "3, 2, fill, default");
this.textField.setColumns(10);
add(btnNewButton, "4, 2, left, top");
JButton btnNewButton_1 = new JButton("Check");
btnNewButton_1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent arg0)
{
try
{
check();
}
catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
add(btnNewButton_1, "3, 4");
}
final void check() throws IOException, NoSuchAlgorithmException
{
FileReader fileReader = new FileReader(this.choosenFile);
char[] md5 = new char[32];
byte[] md5byte = new byte[32];
char[] fileToCheck = new char[(int) (this.choosenFile.length()) - 32];
String md5String = new String();
for (int index = 0; index < 32; index++)
{
md5[index] = (char) fileReader.read();
md5byte[index] = (byte) md5[index];
md5String = md5String + md5[index];
}
fileReader.read();
String fileNameToString = new String();
for (int index = 36; index < (this.choosenFile.length() + 2); index++)
{
fileToCheck[index - 36] = (char) fileReader.read();
fileNameToString = fileNameToString + fileToCheck[index - 36];
}
fileReader.close();
File fileToCheck1 = new File(this.choosenFile.getParent() + "/" + fileNameToString);
byte[] bytedirectoryToSave = new byte[(int) fileToCheck1.length()];
FileInputStream fileInputStream = new FileInputStream(fileToCheck1);
for (int index1 = 0; index1 < (fileToCheck1.length()); index1++)
{
bytedirectoryToSave[index1] = (byte) fileInputStream.read();
}
fileInputStream.close();
byte[] hash = null;
StringBuilder hashString = new StringBuilder();
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
hash = messageDigest.digest(bytedirectoryToSave);
for (int index = 0; index < hash.length; index++)
{
String hex = Integer.toHexString(hash[index]);
if (hex.length() == 1)
{
hashString.append('0');
hashString.append(hex.charAt(hex.length() - 1));
}
else
{
hashString.append(hex.substring(hex.length() - 2));
}
}
if (md5String.equals(hashString.toString()))
{
CheckDialog.main("Vérification OK");
}
else
{
CheckDialog.main("Il y a une erreur");
}
}
/**
*
* @throws IOException
* @throws NoSuchAlgorithmException
*/
final void choosenDirectory() throws IOException, NoSuchAlgorithmException
{
final JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Fichiers md5", "md5");
fc.setAcceptAllFileFilterUsed(false);
fc.addChoosableFileFilter(filter);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = fc.showDialog(CheckPanel.this, "Sauvegarde à vérifier");
if (returnVal == JFileChooser.APPROVE_OPTION)
{
this.choosenFile = fc.getSelectedFile();
this.textField.setText(this.choosenFile.getName());
}
}
}