Skip to content

Commit

Permalink
feat: finish mvvm integration
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterFlomaster1 committed Jul 24, 2024
1 parent 24fddbf commit 5f32296
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import dev.masterflomaster1.jfxc.gui.page.components.HashTextPage;
import dev.masterflomaster1.jfxc.gui.page.components.AtbashPage;
import dev.masterflomaster1.jfxc.gui.page.components.BlockCipherFilesPage;
import dev.masterflomaster1.jfxc.gui.page.components.BlockCipherPage;
import dev.masterflomaster1.jfxc.gui.page.components.BlockCipherTextPage;
import dev.masterflomaster1.jfxc.gui.page.components.CaesarPage;
import dev.masterflomaster1.jfxc.gui.page.components.EnigmaPage;
import dev.masterflomaster1.jfxc.gui.page.components.HashFilesPage;
Expand Down Expand Up @@ -82,7 +82,7 @@ private NavTree.Item createTree() {

var symmetricGroup = NavTree.Item.group("Symmetric Encryption", new FontIcon(BootstrapIcons.FILE_EARMARK_LOCK2));
symmetricGroup.getChildren().setAll(
NAV_TREE.get(BlockCipherPage.class),
NAV_TREE.get(BlockCipherTextPage.class),
NAV_TREE.get(BlockCipherFilesPage.class),
NAV_TREE.get(StreamCipherTextPage.class)
);
Expand Down Expand Up @@ -127,7 +127,7 @@ public static Map<Class<? extends Page>, NavTree.Item> createNavItems() {
map.put(AffinePage.class, NavTree.Item.page(AffinePage.NAME, AffinePage.class));
map.put(CaesarPage.class, NavTree.Item.page(CaesarPage.NAME, CaesarPage.class));
map.put(VigenereCipherPage.class, NavTree.Item.page(VigenereCipherPage.NAME, VigenereCipherPage.class));
map.put(BlockCipherPage.class, NavTree.Item.page(BlockCipherPage.NAME, BlockCipherPage.class));
map.put(BlockCipherTextPage.class, NavTree.Item.page(BlockCipherTextPage.NAME, BlockCipherTextPage.class));
map.put(BlockCipherFilesPage.class, NavTree.Item.page(BlockCipherFilesPage.NAME, BlockCipherFilesPage.class));
map.put(StreamCipherTextPage.class, NavTree.Item.page(StreamCipherTextPage.NAME, StreamCipherTextPage.class));
map.put(HashTextPage.class, NavTree.Item.page(HashTextPage.NAME, HashTextPage.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.masterflomaster1.jfxc.gui.page;

import atlantafx.base.controls.ModalPane;
import atlantafx.base.layout.InputGroup;
import atlantafx.base.theme.Styles;
import dev.masterflomaster1.jfxc.crypto.SecurityUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
Expand Down Expand Up @@ -69,7 +71,7 @@ public static Button createCopyButton(final TextField textField) {
return copyResultButton;
}

public static VBox createPasswordSettingsModal(int keyLength, EventHandler<ActionEvent> callback) {
public static VBox createPasswordSettingsModal(ComboBox<Integer> keyLengthComboBox, TextField keyField, ModalPane modalPane) {
var header = new Label("Generate password based key with PBKDF2");
header.getStyleClass().add(Styles.TITLE_4);

Expand All @@ -89,13 +91,21 @@ public static VBox createPasswordSettingsModal(int keyLength, EventHandler<Actio
var generateButton = new Button("Generate");

generateButton.setOnAction(event -> {
if (passwordTextField.getText().isEmpty())
return;

if (saltTextField.getText().isEmpty())
return;

var key = SecurityUtils.generatePasswordBasedKey(
passwordTextField.getText().toCharArray(),
keyLength,
keyLengthComboBox.getValue(),
HexFormat.of().parseHex(saltTextField.getText())
);

keyField.setText(HexFormat.of().formatHex(key));
modalPane.hide();
});
generateButton.setOnAction(callback);

return new VBox(
20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import atlantafx.base.controls.ModalPane;
import atlantafx.base.layout.InputGroup;
import atlantafx.base.layout.ModalBox;
import atlantafx.base.theme.Styles;
import atlantafx.base.util.Animations;
import atlantafx.base.util.BBCodeParser;
import dev.masterflomaster1.jfxc.MemCache;
import dev.masterflomaster1.jfxc.JFXCrypto;
import dev.masterflomaster1.jfxc.crypto.BlockCipherImpl;
import dev.masterflomaster1.jfxc.crypto.SecurityUtils;
import dev.masterflomaster1.jfxc.gui.page.SimplePage;
import dev.masterflomaster1.jfxc.gui.page.UIElementFactory;
import dev.masterflomaster1.jfxc.gui.page.viewmodel.BlockCipherFilesViewModel;
Expand All @@ -32,7 +29,6 @@
import org.kordamp.ikonli.javafx.FontIcon;

import java.io.File;
import java.util.HexFormat;

public class BlockCipherFilesPage extends SimplePage {

Expand Down Expand Up @@ -129,10 +125,7 @@ private Node mainSection() {

getChildren().add(modalPane);

var modal = UIElementFactory.createPasswordSettingsModal(keyLengthComboBox.getValue(), event -> {
keyTextField.setText(HexFormat.of().formatHex(key));
modalPane.hide();
});
var modal = UIElementFactory.createPasswordSettingsModal(keyLengthComboBox, keyTextField, modalPane);
modal.setPadding(new Insets(20));

var passwordSettingsModal = new ModalBox(modalPane);
Expand Down Expand Up @@ -221,52 +214,11 @@ private void bindComponents() {
keyLengthComboBox.getSelectionModel().selectFirst();
}

private VBox createPasswordModal() {
var header = new Label("Generate password based key with PBKDF2");
header.getStyleClass().add(Styles.TITLE_4);

var passwordTextField = new TextField();
var passwordLabel = new Label("Password");
var passwordGroup = new InputGroup(passwordLabel, passwordTextField);

var saltTextField = new TextField();
var saltLabel = new Label("Salt");
var saltShuffleButton = new Button("", new FontIcon(BootstrapIcons.SHUFFLE));
var saltGroup = new InputGroup(saltLabel, saltTextField, saltShuffleButton);

saltShuffleButton.setOnAction((e) -> {
saltTextField.setText(HexFormat.of().formatHex(SecurityUtils.generateSalt()));
});

var generateButton = new Button("Generate");

generateButton.setOnAction(event -> {
var key = SecurityUtils.generatePasswordBasedKey(
passwordTextField.getText().toCharArray(),
keyLengthComboBox.getValue(),
HexFormat.of().parseHex(saltTextField.getText())
);

keyTextField.setText(HexFormat.of().formatHex(key));
modalPane.hide();
});

return new VBox(
20,
header,
saltGroup,
passwordGroup,
generateButton
);
}

/**
* Disable iv input group if ECB selected
* Disable IV input group if ECB cipher mode is selected
*/
private void onModeSelection(ActionEvent e) {
var mode = modesComboBox.getValue();

ivGroup.setDisable(BlockCipherImpl.Mode.fromString(mode) == BlockCipherImpl.Mode.ECB);
ivGroup.setDisable(viewModel.isNonIvModeSelected());
}

@Override
Expand Down
Loading

0 comments on commit 5f32296

Please sign in to comment.