2021-11-16 00:18:31 +00:00
|
|
|
#+TITLE: Personal Password Generator
|
|
|
|
#+AUTHOR: Howard X. Abrams
|
|
|
|
#+DATE: 2021-01-11
|
|
|
|
|
|
|
|
A literate programming version for Emacs code to generate and store passwords.
|
|
|
|
|
2022-06-18 00:25:47 +00:00
|
|
|
#+begin_src emacs-lisp :exports none
|
2022-03-09 18:45:37 +00:00
|
|
|
;;; ha-passwords --- Emacs code to generate and store passwords. -*- lexical-binding: t; -*-
|
|
|
|
;;
|
|
|
|
;; © 2021-2022 Howard X. Abrams
|
2022-06-18 00:25:47 +00:00
|
|
|
;; Licensed under a Creative Commons Attribution 4.0 International License.
|
2022-03-09 18:45:37 +00:00
|
|
|
;; See http://creativecommons.org/licenses/by/4.0/
|
|
|
|
;;
|
|
|
|
;; Author: Howard X. Abrams <http://gitlab.com/howardabrams>
|
|
|
|
;; Maintainer: Howard X. Abrams
|
|
|
|
;; Created: January 11, 2021
|
|
|
|
;;
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;; *NB:* Do not edit this file. Instead, edit the original literate file at:
|
|
|
|
;; ~/other/hamacs/ha-passwords.org
|
|
|
|
;; And tangle the file to recreate this one.
|
|
|
|
;;
|
|
|
|
;;; Code:
|
2022-06-18 00:25:47 +00:00
|
|
|
#+end_src
|
2021-11-16 00:18:31 +00:00
|
|
|
* Introduction
|
|
|
|
Let's assume that I store a bunch of words in data files:
|
2022-06-18 00:25:47 +00:00
|
|
|
#+begin_src emacs-lisp
|
|
|
|
(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.")
|
|
|
|
#+end_src
|
2021-11-16 00:18:31 +00:00
|
|
|
|
|
|
|
You can see where I'm going with this, can't you? Let's read them into list variables.
|
2022-06-18 00:25:47 +00:00
|
|
|
#+begin_src emacs-lisp
|
|
|
|
(defun ha-passwords--read-data-file (filename)
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents filename)
|
|
|
|
(split-string (buffer-string) "\n" t)))
|
2021-11-16 00:18:31 +00:00
|
|
|
|
2022-06-18 00:25:47 +00:00
|
|
|
#+end_src
|
2021-11-16 00:18:31 +00:00
|
|
|
|
2022-06-18 00:25:47 +00:00
|
|
|
Now we get three or so words from our list of lists:
|
|
|
|
#+begin_src emacs-lisp
|
|
|
|
(defun ha-passwords-words ()
|
|
|
|
(unless ha-passwords-data
|
|
|
|
(setq ha-passwords-data
|
|
|
|
(--map (ha-passwords--read-data-file it) ha-passwords-data-files)))
|
2021-11-16 00:18:31 +00:00
|
|
|
|
2022-06-18 00:25:47 +00:00
|
|
|
(--map (nth (random (length it)) it) ha-passwords-data))
|
|
|
|
#+end_src
|
2021-11-16 00:18:31 +00:00
|
|
|
|
|
|
|
Let's make a password:
|
|
|
|
|
2022-06-18 00:25:47 +00:00
|
|
|
#+begin_src emacs-lisp
|
|
|
|
(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)))))
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
#+begin_src emacs-lisp
|
|
|
|
(defun generate-password (&optional separator)
|
|
|
|
(interactive)
|
|
|
|
(let ((passphrase (ha-passwords-generate separator)))
|
|
|
|
(kill-new passphrase)
|
|
|
|
(message "Random password: %s" passphrase)))
|
|
|
|
#+end_src
|
2021-11-16 00:18:31 +00:00
|
|
|
* Keybindings
|
|
|
|
Got make it easy to call:
|
2022-06-18 00:25:47 +00:00
|
|
|
#+begin_src emacs-lisp
|
|
|
|
(ha-leader "a g" '("generate passwd" . generate-password))
|
|
|
|
#+end_src
|
2021-11-16 00:18:31 +00:00
|
|
|
* Technical Artifacts :noexport:
|
|
|
|
This will =provide= a code name, so that we can =require= this.
|
|
|
|
|
2022-06-18 00:25:47 +00:00
|
|
|
#+begin_src emacs-lisp :exports none
|
|
|
|
(provide 'ha-passwords)
|
|
|
|
;;; ha-passwords.el ends here
|
|
|
|
#+end_src
|
2021-11-16 00:18:31 +00:00
|
|
|
|
|
|
|
#+DESCRIPTION: A literate programming version for Emacs code to generate and store passwords.
|
|
|
|
|
|
|
|
#+PROPERTY: header-args:sh :tangle no
|
|
|
|
#+PROPERTY: header-args:emacs-lisp :tangle yes
|
|
|
|
#+PROPERTY: header-args :results none :eval no-export :comments no mkdirp yes
|
|
|
|
|
|
|
|
#+OPTIONS: num:nil toc:nil todo:nil tasks:nil tags:nil date:nil
|
|
|
|
#+OPTIONS: skip:nil author:nil email:nil creator:nil timestamp:nil
|
|
|
|
#+INFOJS_OPT: view:nil toc:nil ltoc:t mouse:underline buttons:0 path:http://orgmode.org/org-info.js
|