Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* Copyright (C) 2011 [Gobierno de Espana]
* This file is part of "Cliente @Firma".
* "Cliente @Firma" is free software; you can redistribute it and/or modify it under the terms of:
* - the GNU General Public License as published by the Free Software Foundation;
* either version 2 of the License, or (at your option) any later version.
* - or The European Software License; either version 1.1 or (at your option) any later version.
* You may contact the copyright holder at: [email protected]
*/
package es.gob.afirma.standalone.ui.hash;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import es.gob.afirma.core.AOCancelledOperationException;
import es.gob.afirma.core.misc.AOUtil;
import es.gob.afirma.core.misc.Base64;
import es.gob.afirma.core.misc.Platform;
import es.gob.afirma.core.ui.AOUIFactory;
import es.gob.afirma.standalone.AutoFirmaUtil;
import es.gob.afirma.standalone.SimpleAfirmaMessages;
import es.gob.afirma.standalone.ui.CommonWaitDialog;
import es.gob.afirma.standalone.ui.preferences.PreferencesManager;
/** Genera huellas digitales de directorios (conjunto de ficheros).
* @author Juliana Marulanda. */
public final class CreateHashFiles extends JDialog implements KeyListener {
private static final long serialVersionUID = -7224732001218823361L;
private static final int SIZE_WAIT = 50000000; //Tamano en bytes
private static final String[] HASH_ALGOS = new String[] {
"SHA-256", //$NON-NLS-1$
"SHA-1", //$NON-NLS-1$
"SHA-384", //$NON-NLS-1$
"SHA-512" //$NON-NLS-1$
};
private final JComboBox hashAlgorithms = new JComboBox<>(HASH_ALGOS);
private final JTextField selectedFile = new JTextField();
private final JButton examineButton = new JButton();
private final JButton generateButton = new JButton();
private final JCheckBox recursive = new JCheckBox(
SimpleAfirmaMessages.getString("CreateHashFiles.16") //$NON-NLS-1$
);
/** Fichero a evitar. */
private final static Set FILES_TO_AVOID = new HashSet<>(
Arrays.asList(
".fseventsd", //$NON-NLS-1$
".Spotlight-V100", //$NON-NLS-1$
".Trashes", //$NON-NLS-1$
"._.Trashes", //$NON-NLS-1$
".DS_Store", //$NON-NLS-1$
".desktop", //$NON-NLS-1$
"thumbs.db", //$NON-NLS-1$
"$Recycle.Bin" //$NON-NLS-1$
)
);
boolean isRecursiveSelected = false;
/** Inicia el proceso de creación de huella digital de directorios.
* @param parent Componente padre para la modalidad. */
public static void startHashCreation(final Frame parent) {
final JDialog dialog = new CreateHashFiles(parent);
dialog.setSize(600, 250);
dialog.setResizable(false);
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
}
/** Crea un diálogo para la creación de huellas digitales de
* directorios.
* @param parent Componente padre para la modalidad. */
private CreateHashFiles(final Frame parent) {
super(parent);
setTitle(SimpleAfirmaMessages.getString("CreateHashFiles.0")); //$NON-NLS-1$
setModalityType(ModalityType.APPLICATION_MODAL);
createUI(parent);
}
/** Crea todos los elementos necesarios para generar una huella digital de
* directorios.
* @param parent Componente padre para la modalidad. */
void createUI(final Frame parent) {
final Container c = getContentPane();
final GridBagLayout gbl = new GridBagLayout();
c.setLayout(gbl);
final GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.gridy = 0;
gbc.insets = new Insets(10, 10, 0, 10);
setLayout(new GridBagLayout());
setLocationRelativeTo(parent);
setIconImage(
AutoFirmaUtil.getDefaultDialogsIcon()
);
getAccessibleContext().setAccessibleDescription(
SimpleAfirmaMessages.getString("CreateHashFiles.1") //$NON-NLS-1$
);
// Label con el nombre del fichero
final JLabel label = new JLabel(SimpleAfirmaMessages.getString("CreateHashFiles.11")); //$NON-NLS-1$
this.examineButton.getAccessibleContext().setAccessibleDescription(SimpleAfirmaMessages.getString("CreateHashFiles.10")); //$NON-NLS-1$
// Boton examinar
this.examineButton.setText(SimpleAfirmaMessages.getString("CreateHashFiles.10")); //$NON-NLS-1$
this.examineButton.setMnemonic('X');
this.examineButton.addKeyListener(this);
this.examineButton.addActionListener(
ae -> openSelectedFile()
);
this.examineButton.setEnabled(true);
this.selectedFile.setEditable(false);
this.selectedFile.setFocusable(false);
this.selectedFile.addKeyListener(this);
// Label con el algoritmo
final JLabel labelAlg = new JLabel(SimpleAfirmaMessages.getString("CreateHashDialog.2")); //$NON-NLS-1$
// ComboBox con los algoritmos de generacion
this.hashAlgorithms.setSelectedItem(
PreferencesManager.get(PreferencesManager.PREFERENCE_CREATE_HASH_DIRECTORY_ALGORITHM)
);
this.hashAlgorithms.addKeyListener(this);
this.hashAlgorithms.addActionListener(
e -> PreferencesManager.put(
PreferencesManager.PREFERENCE_CREATE_HASH_DIRECTORY_ALGORITHM,
getSelectedHashAlgorithm()
)
);
this.recursive.addKeyListener(this);
this.recursive.addActionListener(actionEvent -> {
final AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
CreateHashFiles.this.isRecursiveSelected = abstractButton.getModel().isSelected();
}
);
this.recursive.setMnemonic('R');
// Boton de generacion de la huella
this.generateButton.setText(SimpleAfirmaMessages.getString("CreateHashDialog.4")); //$NON-NLS-1$
this.generateButton.setMnemonic('G');
this.generateButton.addKeyListener(this);
this.generateButton.addActionListener(ae -> {
doHashProcess(
parent,
getFileTextField().getText(),
getSelectedHashAlgorithm(),
CreateHashFiles.this.isRecursiveSelected
);
CreateHashFiles.this.setVisible(false);
CreateHashFiles.this.dispose();
}
);
this.generateButton.setEnabled(false);
// Panel donde se anade el boton de generar
final JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
final JButton exitButton = new JButton(
SimpleAfirmaMessages.getString("CreateHashDialog.16") //$NON-NLS-1$
);
exitButton.setMnemonic('C');
exitButton.addKeyListener(this);
exitButton.addActionListener(
e -> {
CreateHashFiles.this.setVisible(false);
CreateHashFiles.this.dispose();
}
);
exitButton.getAccessibleContext().setAccessibleDescription(
SimpleAfirmaMessages.getString("CreateHashDialog.17") //$NON-NLS-1$
);
// En Mac OS X el orden de los botones es distinto
if (Platform.OS.MACOSX.equals(Platform.getOS())) {
panel.add(exitButton);
panel.add(this.generateButton);
}
else {
panel.add(this.generateButton);
panel.add(exitButton);
}
add(label, gbc);
gbc.gridy++;
add(this.selectedFile, gbc);
gbc.weightx = 0.0;
add(this.examineButton, gbc);
gbc.gridy++;
add(labelAlg, gbc);
gbc.weightx = 1.0;
gbc.gridy++;
add(this.hashAlgorithms, gbc);
gbc.gridy++;
add(this.recursive, gbc);
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(panel, gbc);
}
static void doHashProcess(final Frame parent,
final String dir,
final String hashAlgorithm,
final boolean recursive) {
// Se crea la ventana de espera.
final CommonWaitDialog dialog = new CommonWaitDialog(
parent,
SimpleAfirmaMessages.getString("CreateHashFiles.18"), //$NON-NLS-1$
SimpleAfirmaMessages.getString("CreateHashFiles.20") //$NON-NLS-1$
);
// Arrancamos el proceso en un hilo aparte
final SwingWorker