diff --git a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java index 5180efb..32095a9 100644 --- a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java +++ b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java @@ -1,8 +1,11 @@ package nl.andrewlalis.erme; import com.formdev.flatlaf.FlatLightLaf; +import nl.andrewlalis.erme.util.Hash; import nl.andrewlalis.erme.view.EditorFrame; +import java.nio.charset.StandardCharsets; + public class EntityRelationMappingEditor { public static final String VERSION = "1.2.0"; @@ -10,7 +13,19 @@ public class EntityRelationMappingEditor { if (!FlatLightLaf.install()) { System.err.println("Could not install FlatLight Look and Feel."); } - final EditorFrame frame = new EditorFrame(); + final boolean includeAdminActions = shouldIncludeAdminActions(args); + if (includeAdminActions) { + System.out.println("Admin actions have been enabled."); + } + final EditorFrame frame = new EditorFrame(includeAdminActions); frame.setVisible(true); } + + private static boolean shouldIncludeAdminActions(String[] args) { + if (args.length < 1) { + return false; + } + byte[] pw = args[0].getBytes(StandardCharsets.UTF_8); + return Hash.matches(pw, "admin_hash.txt"); + } } diff --git a/src/main/java/nl/andrewlalis/erme/util/Hash.java b/src/main/java/nl/andrewlalis/erme/util/Hash.java new file mode 100644 index 0000000..2e9a25a --- /dev/null +++ b/src/main/java/nl/andrewlalis/erme/util/Hash.java @@ -0,0 +1,50 @@ +package nl.andrewlalis.erme.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; + +public class Hash { + public static boolean matches(byte[] password, String resourceFile) { + MessageDigest md; + try { + md = MessageDigest.getInstance("SHA-256"); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + return false; + } + byte[] passwordHash = md.digest(password); + InputStream is = Hash.class.getClassLoader().getResourceAsStream(resourceFile); + if (is == null) { + System.err.println("Could not obtain input stream to admin_hash.txt"); + return false; + } + char[] buffer = new char[64]; + try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { + if (br.read(buffer) != buffer.length) { + System.err.println("Incorrect number of characters read from hash file."); + return false; + } + } catch (IOException e) { + e.printStackTrace(); + return false; + } + String hashHex = String.valueOf(buffer); + byte[] hash = hexStringToByteArray(hashHex); + return Arrays.equals(passwordHash, hash); + } + + private static byte[] hexStringToByteArray(String s) { + int len = s.length(); + byte[] data = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + + Character.digit(s.charAt(i+1), 16)); + } + return data; + } +} diff --git a/src/main/java/nl/andrewlalis/erme/view/EditorFrame.java b/src/main/java/nl/andrewlalis/erme/view/EditorFrame.java index 362cfb8..8bf536d 100644 --- a/src/main/java/nl/andrewlalis/erme/view/EditorFrame.java +++ b/src/main/java/nl/andrewlalis/erme/view/EditorFrame.java @@ -9,10 +9,10 @@ import java.awt.*; * The main JFrame for the editor. */ public class EditorFrame extends JFrame { - public EditorFrame() { + public EditorFrame(boolean includeAdminActions) { super("ER-Mapping Editor"); this.setContentPane(new DiagramPanel(new MappingModel())); - this.setJMenuBar(new EditorMenuBar()); + this.setJMenuBar(new EditorMenuBar(includeAdminActions)); this.setMinimumSize(new Dimension(400, 400)); this.setPreferredSize(new Dimension(800, 800)); this.pack(); diff --git a/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java b/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java index 9192bdd..76e2e36 100644 --- a/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java +++ b/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java @@ -12,7 +12,7 @@ import javax.swing.*; * The menu bar that's visible atop the application. */ public class EditorMenuBar extends JMenuBar { - public EditorMenuBar() { + public EditorMenuBar(boolean includeAdminActions) { this.add(this.buildFileMenu()); this.add(this.buildEditMenu()); this.add(this.buildHelpMenu()); diff --git a/src/main/resources/admin_hash.txt b/src/main/resources/admin_hash.txt new file mode 100644 index 0000000..d1f06e0 --- /dev/null +++ b/src/main/resources/admin_hash.txt @@ -0,0 +1 @@ +cfdabe75d984e5a92fb491dadc9091419d9587c049246356a488e83a75505bce \ No newline at end of file