Search in sources :

Example 6 with ShadowAlgorithmHandler

use of org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler in project core by jcryptool.

the class StartHandler method execute.

public IStatus execute(final ActionView view, final ActionItem startItem, final Shell shell, IProgressMonitor monitor) {
    ActionCascade ac = view.getActionCascade();
    final TableViewer viewer = view.getViewer();
    // $NON-NLS-1$
    LogUtil.logInfo("Running Action Cascade " + ac.getName());
    boolean start = false;
    for (final ActionItem a : ac.getAllItems()) {
        int steps = ac.getSize();
        // Start the execution at the first selected element
        if (a == startItem) {
            start = true;
            monitor.beginTask(a.getActionName(), steps);
        } else
            steps--;
        if (start) {
            if (monitor.isCanceled()) {
                return Status.CANCEL_STATUS;
            }
            try {
                boolean executed = false;
                // Set focus on moved row. Just look and feel ...
                display.asyncExec(new Runnable() {

                    public void run() {
                        viewer.setSelection(new StructuredSelection(a), true);
                    }
                });
                OperationsPlugin op = OperationsPlugin.getDefault();
                CommandInfo[] commands = op.getAlgorithmsManager().getShadowAlgorithmCommands();
                // Try to find an CryptoAlgorithm-Plug-in (Classic or Modern)
                for (int i = 0, length = commands.length; i < length; i++) {
                    if (commands[i].getHandler() != null && a.getPluginId().equals(commands[i].getCommandId())) {
                        ((ShadowAlgorithmHandler) commands[i].getHandler()).run(convert(a));
                        executed = true;
                    }
                }
                // Try to find a FlexiProvider algorithm
                if (!executed) {
                    // $NON-NLS-1$
                    LogUtil.logInfo("Trying to execute FlexiProvider algorithm");
                    for (RegistryType rt : RegistryType.values()) {
                        if (rt.getName().equals(a.getPluginId())) {
                            AlgorithmDescriptor descriptor = new AlgorithmDescriptor(a.getActionName(), rt, null);
                            if (a.getParam("algorithm type").equals(RegistryType.BLOCK_CIPHER.getName())) {
                                // $NON-NLS-1$
                                descriptor = new BlockCipherDescriptor(descriptor.getAlgorithmName(), a.getParam("mode"), a.getParam("padding scheme"), null, // $NON-NLS-1$ //$NON-NLS-2$
                                null);
                            } else if (a.getParam("algorithm type").equals(RegistryType.SECURE_RANDOM.getName())) {
                                // $NON-NLS-1$
                                // TODO Get property "alphabet" from item
                                byte[][] alphabet = null;
                                if (alphabet == null) {
                                    descriptor = new SecureRandomDescriptor(descriptor.getAlgorithmName(), // $NON-NLS-1$
                                    Integer.parseInt(a.getParam("random size")));
                                }
                            }
                            final IFlexiProviderOperation operation = new IntegratorOperation(descriptor);
                            operation.setInput(Messages.InputType);
                            operation.setOutput(Messages.StartHandler_6);
                            // $NON-NLS-1$
                            operation.setSignature(a.getParam("signature"));
                            if ("encrypt".equals(a.getActionType())) {
                                // $NON-NLS-1$
                                operation.setOperation(OperationType.ENCRYPT);
                            } else if ("decrypt".equals(a.getActionType())) {
                                // $NON-NLS-1$
                                operation.setOperation(OperationType.DECRYPT);
                            } else if ("sign".equals(a.getActionType())) {
                                // $NON-NLS-1$
                                operation.setOperation(OperationType.SIGN);
                            } else if ("verify".equals(a.getActionType())) {
                                // $NON-NLS-1$
                                operation.setOperation(OperationType.VERIFY);
                            } else {
                                operation.setOperation(OperationType.UNKNOWN);
                            }
                            // $NON-NLS-1$
                            String alias = a.getParam("key alias");
                            if (alias != null) {
                                operation.setKeyStoreAlias(new KeyStoreAlias(alias));
                            }
                            // $NON-NLS-1$
                            String password = a.getParam("key password");
                            if (password != null) {
                                operation.setPassword(password.toCharArray());
                            }
                            display.asyncExec(new Runnable() {

                                public void run() {
                                    PerformOperationManager.getInstance().firePerformOperation(operation);
                                }
                            });
                            executed = true;
                        }
                    }
                }
                if (!executed) {
                    throw new Exception(Messages.StartHandler_2 + a.getPluginId());
                }
            } catch (final Exception e) {
                if (ActionsUIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.P_IGNORE_ERRORS)) {
                    // $NON-NLS-1$
                    LogUtil.logError(ActionsUIPlugin.PLUGIN_ID, a.getActionName() + ": " + e.getMessage(), e, false);
                    if (!ActionsUIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.P_DONT_SHOW_MESSAGES)) {
                        display.syncExec(new Runnable() {

                            public void run() {
                                MessageDialog.openWarning(shell, Messages.StartHandler_3, e.getMessage());
                            }
                        });
                    }
                } else {
                    display.asyncExec(new Runnable() {

                        public void run() {
                            MessageDialog.openError(shell, Messages.StartHandler_3, e.getMessage() + // $NON-NLS-1$
                            Messages.StartHandler_4);
                        }
                    });
                    return Status.CANCEL_STATUS;
                }
            } finally {
                monitor.worked(ac.getSize() - steps);
            }
        }
    }
    monitor.done();
    return Status.OK_STATUS;
}
Also used : KeyStoreAlias(org.jcryptool.crypto.keystore.backend.KeyStoreAlias) RegistryType(org.jcryptool.crypto.flexiprovider.types.RegistryType) ShadowAlgorithmHandler(org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler) ActionItem(org.jcryptool.actions.core.types.ActionItem) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) AlgorithmDescriptor(org.jcryptool.crypto.flexiprovider.descriptors.algorithms.AlgorithmDescriptor) ActionCascade(org.jcryptool.actions.core.types.ActionCascade) OperationsPlugin(org.jcryptool.core.operations.OperationsPlugin) ExecutionException(org.eclipse.core.commands.ExecutionException) SecureRandomDescriptor(org.jcryptool.crypto.flexiprovider.descriptors.algorithms.SecureRandomDescriptor) CommandInfo(org.jcryptool.core.operations.CommandInfo) BlockCipherDescriptor(org.jcryptool.crypto.flexiprovider.descriptors.algorithms.BlockCipherDescriptor) IntegratorOperation(org.jcryptool.crypto.flexiprovider.integrator.IntegratorOperation) TableViewer(org.eclipse.jface.viewers.TableViewer) IFlexiProviderOperation(org.jcryptool.crypto.flexiprovider.descriptors.IFlexiProviderOperation)

Aggregations

ShadowAlgorithmHandler (org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler)6 CommandInfo (org.jcryptool.core.operations.CommandInfo)5 ExecutionException (org.eclipse.core.commands.ExecutionException)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 AbstractHandler (org.eclipse.core.commands.AbstractHandler)2 Command (org.eclipse.core.commands.Command)2 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)2 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)2 ICommandService (org.eclipse.ui.commands.ICommandService)2 IHandlerService (org.eclipse.ui.handlers.IHandlerService)2 TreeObject (org.jcryptool.core.views.content.structure.TreeObject)2 TreeParent (org.jcryptool.core.views.content.structure.TreeParent)2 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 PaletteDrawer (org.eclipse.gef.palette.PaletteDrawer)1 PaletteEntry (org.eclipse.gef.palette.PaletteEntry)1 PaletteRoot (org.eclipse.gef.palette.PaletteRoot)1 SelectionToolEntry (org.eclipse.gef.palette.SelectionToolEntry)1