From b2981ba23df0efdfe9665a15550da8d6021b14f9 Mon Sep 17 00:00:00 2001 From: "Toerd@archlinux" Date: Sat, 27 Jun 2020 15:12:32 +0200 Subject: [PATCH] fix: minor improvements --- diffie-hellman.el | 74 +++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/diffie-hellman.el b/diffie-hellman.el index 2046e4c..757d45d 100644 --- a/diffie-hellman.el +++ b/diffie-hellman.el @@ -40,48 +40,45 @@ (when (get-buffer buf-a) (kill-buffer buf-a)) (generate-new-buffer buf-a) (with-current-buffer buf-a - (progn - (insert (phga/dh-pretty-name "Alice")) - (insert (concat "P for GF(P) = " (number-to-string p) "\n")) - (insert (concat "G = " (number-to-string g) "\n")) - (insert (concat "A = " (number-to-string a) " (only known by Alice!)\n")) - (insert (concat "Alpha = " (number-to-string aa) "\n")) - (insert (concat "Beta = " (number-to-string bb) " (sent by Bob to Alice)\n")) - (insert (concat "Secret Key = " (number-to-string ka) " (Beta^A mod P == (G^B)^A mod P\n")) - )) + (insert (phga/dh-pretty-name "Alice")) + (insert (concat "P for GF(P) = " (number-to-string p) "\n")) + (insert (concat "G = " (number-to-string g) "\n")) + (insert (concat "A = " (number-to-string a) " (only known by Alice!)\n")) + (insert (concat "Alpha = " (number-to-string aa) "\n")) + (insert (concat "Beta = " (number-to-string bb) " (sent by Bob to Alice)\n")) + (insert (concat "Secret Key = " (number-to-string ka) " (Beta^A mod P == (G^B)^A mod P\n")) + ) ;; Bob (when (get-buffer buf-b) (kill-buffer buf-b)) (generate-new-buffer buf-b) (with-current-buffer buf-b - (progn - (insert (phga/dh-pretty-name "Bob")) - (insert (concat "P for GF(P) = " (number-to-string p) "\n")) - (insert (concat "G = " (number-to-string g) "\n")) - (insert (concat "B = " (number-to-string b) " (only known by Bob!)\n")) - (insert (concat "Beta = " (number-to-string bb) "\n")) - (insert (concat "Alpha = " (number-to-string aa) " (sent by Alice to Bob)\n")) - (insert (concat "Secret Key = " (number-to-string kb) " (Alpha^B mod P == (G^A)^B mod P\n")) - )) + (insert (phga/dh-pretty-name "Bob")) + (insert (concat "P for GF(P) = " (number-to-string p) "\n")) + (insert (concat "G = " (number-to-string g) "\n")) + (insert (concat "B = " (number-to-string b) " (only known by Bob!)\n")) + (insert (concat "Beta = " (number-to-string bb) "\n")) + (insert (concat "Alpha = " (number-to-string aa) " (sent by Alice to Bob)\n")) + (insert (concat "Secret Key = " (number-to-string kb) " (Alpha^B mod P == (G^A)^B mod P\n")) + ) ;; (kill-buffer "*Public*") (when (get-buffer buf-p) (kill-buffer buf-p)) (generate-new-buffer buf-p) (with-current-buffer buf-p - (progn - (insert "XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx\n") - (insert " This is a public transport buffer!\n") - (insert " Mean Hackerbois can wait here\n and sniff the communication!!!\n") - (insert "XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx\n\n") - (insert (concat "P for GF(P) = " (number-to-string p) "\n")) - (insert (concat "G = " (number-to-string g) "\n")) - (insert (concat "Alpha = " (number-to-string aa) "\n")) - (insert (concat "Beta = " (number-to-string bb) "\n")) - )) + (insert "XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx\n") + (insert " This is a public transport buffer!\n") + (insert " Mean Hackerbois can wait here\n and sniff the communication!!!\n") + (insert "XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx\n\n") + (insert (concat "P for GF(P) = " (number-to-string p) "\n")) + (insert (concat "G = " (number-to-string g) "\n")) + (insert (concat "Alpha = " (number-to-string aa) "\n")) + (insert (concat "Beta = " (number-to-string bb) "\n")) + ) ;; Create Window setup lel that's soooo bad I am SORRY (delete-other-windows) - (command-execute 'split-window-horizontally) - (command-execute 'split-window-horizontally) - (command-execute 'balance-windows) + (split-window-horizontally) + (split-window-horizontally) + (balance-windows) (set-window-buffer (nth 0 (window-list)) buf-a) (set-window-buffer (nth 1 (window-list)) buf-p) @@ -93,11 +90,10 @@ "Get a random prime number up to n (inclusive)" (interactive) (let ( - (q (random n)) + (q (+ 2 (random (- n 2)))) ) ;; p <= 1 -> not a prime number - (if (and (> q 1) - (phga/dh-is-prime q)) + (if (phga/dh-is-prime q) q (phga/dh-get-random-prime-number n)) ) @@ -133,12 +129,14 @@ (+ 1 (random (- p 2)))) (defun phga/dh-pretty-name (name) -"Returns the name in a pretty format" -(format "****************************\n Hi my Name is %s!\n****************************\n\n" name)) + "Returns the name in a pretty format" + (format "****************************\n Hi my Name is %s!\n****************************\n\n" name)) +;; (phga/dh-example) +;; (and nil t) -;; Tests: ;; (phga/dh-get-generator-p (phga/dh-get-random-prime-number 20)) -;; (and nil t) + +;; ;; Test ;; (setq phga/p (phga/dh-get-generator-p (phga/dh-get-random-prime-number 20))) ;; (setq phga/g (phga/dh-get-generator-elem phga/p)) ;; (setq phga/a (+ 1 (random (- phga/p 2))))