Search in sources :

Example 1 with MessageDigest

use of de.flexiprovider.api.MessageDigest in project core by jcryptool.

the class IntegratorHandler method execute.

/**
 * runs the action: setup the algorithm and executed the specified operation
 */
@Override
public Object execute(ExecutionEvent event) {
    AlgorithmDescriptor descriptor = getAlgorithmDescriptor(FLEXIPROVIDER_ALGORITHM_NAME);
    if (descriptor == null) {
        MessageBox messageBox = new MessageBox(getActiveWorkbenchWindow().getShell());
        // $NON-NLS-1$
        messageBox.setText(Messages.getString("DummyAction.error"));
        // $NON-NLS-1$
        messageBox.setMessage(Messages.getString("DummyAction.8"));
        messageBox.open();
        return (null);
    }
    String readableNameExtension;
    switch(algorithmType) {
        case TYPE_RANDOM_NUMBER_GENERATOR:
            // $NON-NLS-1$
            readableNameExtension = Messages.getString("DummyAction.random_number_generator");
            break;
        case TYPE_SIGNATURE:
            // $NON-NLS-1$
            readableNameExtension = Messages.getString("DummyAction.signature");
            break;
        case TYPE_MESSAGE_DIGEST:
            // $NON-NLS-1$
            readableNameExtension = Messages.getString("DummyAction.message_digest");
            break;
        case TYPE_MESSAGE_AUTHTIFICATION_CODE:
            // $NON-NLS-1$
            readableNameExtension = Messages.getString("DummyAction.message_authentification_code");
            break;
        default:
            // $NON-NLS-1$
            readableNameExtension = Messages.getString("DummyAction.encryption");
    }
    // Get which key lengths are valid for this algorithm
    int[] validKeyLengths = null;
    IMetaKeyGenerator keyGen = AlgorithmsXMLManager.getInstance().getSecretKeyGenerator(FLEXIPROVIDER_ALGORITHM_NAME);
    if (keyGen != null) {
        if (keyGen.getLengths().getLengths() != null) {
            validKeyLengths = new int[keyGen.getLengths().getLengths().size()];
            for (int i = 0; i < validKeyLengths.length; i++) validKeyLengths[i] = keyGen.getLengths().getLengths().get(i);
        } else {
            validKeyLengths = new int[1];
            validKeyLengths[0] = keyGen.getLengths().getDefaultLength();
        }
    }
    wizard = new IntegratorWizard(READABLE_ALGORITHM_NAME + readableNameExtension, READABLE_ALGORITHM_NAME, HEADER_DESCRIPTION, SHOW_OPERATION_GROUP, SHOW_PADDING_GROUP, SHOW_KEY, SHOW_KEY_SOURCE_GROUP, validKeyLengths, SHOW_SIGNATURE_GROUP, SHOW_RANDOM_GROUP, SHOW_MESSAGE_DIGEST_GROUP, algorithmType);
    WizardDialog dialog = new WizardDialog(getActiveWorkbenchWindow().getShell(), wizard);
    dialog.setHelpAvailable(true);
    switch(dialog.open()) {
        case Window.OK:
            if (algorithmType == TYPE_CIPHER_BLOCK) {
                descriptor = new BlockCipherDescriptor(descriptor.getAlgorithmName(), AlgorithmsXMLManager.getInstance().getMode(wizard.getMode().getDescription()).getID(), AlgorithmsXMLManager.getInstance().getPaddingScheme(wizard.getPadding().getPaddingSchemeName()).getID(), null, descriptor.getAlgorithmParameterSpec());
            }
            if (algorithmType == TYPE_RANDOM_NUMBER_GENERATOR) {
                if (wizard.doFilter())
                    descriptor = new SecureRandomDescriptor(descriptor.getAlgorithmName(), wizard.getRandomSize(), wizard.getFilter());
                else
                    descriptor = new SecureRandomDescriptor(descriptor.getAlgorithmName(), wizard.getRandomSize());
            }
            IntegratorOperation operation = new IntegratorOperation(descriptor);
            // $NON-NLS-1$
            operation.setEntryName("");
            // $NON-NLS-1$
            operation.setInput(Messages.getString("InputType"));
            // $NON-NLS-1$
            operation.setOutput("<Editor>");
            operation.setSignature(wizard.signature());
            operation.setUseCustomKey(wizard.useCustomKey());
            if (wizard.useCustomKey())
                operation.setKeyBytes(wizard.getCustomKey());
            try {
                if (// $NON-NLS-1$
                SHOW_KEY != null && !SHOW_KEY.equals("") && !wizard.useCustomKey())
                    operation.setKeyStoreAlias(wizard.getKey());
                if (wizard.encrypt()) {
                    // explicit encrypt
                    if (descriptor.getType() == RegistryType.SIGNATURE) {
                        operation.setOperation(OperationType.VERIFY);
                    } else {
                        operation.setOperation(OperationType.ENCRYPT);
                    }
                } else {
                    // implicit decrypt
                    if (descriptor.getType() == RegistryType.SIGNATURE) {
                        operation.setOperation(OperationType.SIGN);
                    } else {
                        operation.setOperation(OperationType.DECRYPT);
                    }
                }
                if (SHOW_MESSAGE_DIGEST_GROUP > 0 && !wizard.encrypt()) {
                    try {
                        MessageDigest digest = Registry.getMessageDigest(operation.getAlgorithmDescriptor().getAlgorithmName());
                        InputStream inputStream = EditorsManager.getInstance().getActiveEditorContentInputStream();
                        int i;
                        while ((i = inputStream.read()) != -1) {
                            digest.update((byte) i);
                        }
                        byte[] checksumAsBytes = digest.digest();
                        // $NON-NLS-1$
                        String checksum = "";
                        for (byte b : checksumAsBytes) {
                            String temp = Integer.toHexString((int) b);
                            // $NON-NLS-1$ //$NON-NLS-2$
                            checksum += (temp.length() == 1 ? "0" : "") + temp.substring(Math.max(0, temp.length() - 2));
                        }
                        String expectedChecksum = wizard.getExpectedChecksum();
                        if (checksum.equalsIgnoreCase(expectedChecksum)) {
                            MessageBox messageBox = new MessageBox(getActiveWorkbenchWindow().getShell(), SWT.ICON_WORKING);
                            // $NON-NLS-1$
                            messageBox.setText(Messages.getString("IntegratorAction.0"));
                            // $NON-NLS-1$
                            messageBox.setMessage(Messages.getString("IntegratorAction.1"));
                            messageBox.open();
                        } else {
                            MessageBox messageBox = new MessageBox(getActiveWorkbenchWindow().getShell(), SWT.ICON_ERROR);
                            // $NON-NLS-1$
                            messageBox.setText(Messages.getString("IntegratorAction.2"));
                            messageBox.setMessage(// $NON-NLS-1$
                            NLS.bind(// $NON-NLS-1$
                            Messages.getString("IntegratorAction.3"), new Object[] { checksum.toLowerCase(), expectedChecksum.toLowerCase() }));
                            messageBox.open();
                        }
                    } catch (NoSuchAlgorithmException e) {
                        LogUtil.logError(IntegratorPlugin.PLUGIN_ID, "NoSuchAlgorithmException while initializing a message digest", e, // $NON-NLS-1$
                        true);
                    }
                } else {
                    PerformOperationManager.getInstance().firePerformOperation(operation);
                }
                if (operation.getOperation() == OperationType.SIGN) {
                    MessageBox messageBox = new MessageBox(getActiveWorkbenchWindow().getShell(), SWT.NONE);
                    // $NON-NLS-1$
                    messageBox.setText(Messages.getString("DummyAction.13"));
                    // $NON-NLS-1$
                    messageBox.setMessage(Messages.getString("DummyAction.14") + wizard.signature());
                    messageBox.open();
                }
            } catch (IOException ex) {
                LogUtil.logError(ex);
            }
            break;
        case Window.CANCEL:
            break;
    }
    return (null);
}
Also used : InputStream(java.io.InputStream) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) IOException(java.io.IOException) IMetaKeyGenerator(org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator) AlgorithmDescriptor(org.jcryptool.crypto.flexiprovider.descriptors.algorithms.AlgorithmDescriptor) MessageBox(org.eclipse.swt.widgets.MessageBox) SecureRandomDescriptor(org.jcryptool.crypto.flexiprovider.descriptors.algorithms.SecureRandomDescriptor) BlockCipherDescriptor(org.jcryptool.crypto.flexiprovider.descriptors.algorithms.BlockCipherDescriptor) IDataObject(org.jcryptool.core.operations.dataobject.IDataObject) MessageDigest(de.flexiprovider.api.MessageDigest) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Aggregations

MessageDigest (de.flexiprovider.api.MessageDigest)1 NoSuchAlgorithmException (de.flexiprovider.api.exceptions.NoSuchAlgorithmException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 IDataObject (org.jcryptool.core.operations.dataobject.IDataObject)1 AlgorithmDescriptor (org.jcryptool.crypto.flexiprovider.descriptors.algorithms.AlgorithmDescriptor)1 BlockCipherDescriptor (org.jcryptool.crypto.flexiprovider.descriptors.algorithms.BlockCipherDescriptor)1 SecureRandomDescriptor (org.jcryptool.crypto.flexiprovider.descriptors.algorithms.SecureRandomDescriptor)1 IMetaKeyGenerator (org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator)1