hamacs/ha-passwords.org
Howard Abrams 3a927e756a Fix my code inclusion, like beep and demo-it
Using the :local-repo to test local repositories of my .. this doesn't
seem right.
2021-12-27 09:46:10 -08:00

3.6 KiB

Personal Password Generator

A literate programming version for Emacs code to generate and store passwords.

Introduction

Let's assume that I store a bunch of words in data files:

(defvar ha-passwords-data-files (list (expand-file-name "adjectives.txt"
                                                        (expand-file-name "data" hamacs-source-dir))
                                      (expand-file-name "colors.txt"
                                                        (expand-file-name "data" hamacs-source-dir))
                                      (expand-file-name "nouns.txt"
                                                        (expand-file-name "data" hamacs-source-dir)))
  "List of file name containing a data lines for our password generator. Order of these files matter.")

(defvar ha-passwords-data nil
  "Contains a list of lists of words that we can choose.")

You can see where I'm going with this, can't you? Let's read them into list variables.

(defun ha-passwords--read-data-file (filename)
  (with-temp-buffer
    (insert-file-contents filename)
    (split-string (buffer-string) "\n" t)))

Now we just get three or so words from our list of lists:

(defun ha-passwords-words ()
  (unless ha-passwords-data
    (setq ha-passwords-data
          (--map (ha-passwords--read-data-file it) ha-passwords-data-files)))

  (--map (nth (random (length it)) it) ha-passwords-data))

Let's make a password:

(defun ha-passwords-generate (&optional separator)
  (unless separator
    (setq separator "-"))

  (let* ((choices '("!" "@" "#" "$" "%" "^" "&" "*"))
         (choice (random (length choices)))
         (number (1+ choice)))
    (->> (ha-passwords-words)
         (s-join separator)
         (s-capitalize)
         (s-append (nth choice choices))
         (s-append (number-to-string number)))))
(defun generate-password (&optional separator)
  (interactive)
  (let ((passphrase (ha-passwords-generate separator)))
    (kill-new passphrase)
    (message "Random password: %s" passphrase)))

Keybindings

Got make it easy to call:

(ha-leader "a g" '("generate passwd" . generate-password))