From f4df7d43681445a038dd53c887e4f8dae3beb5ad Mon Sep 17 00:00:00 2001 From: Howard Abrams Date: Mon, 18 Sep 2023 10:32:59 -0700 Subject: [PATCH] Redact sensitive information with regular expressions Found a great idea from Zenodium. --- ha-passwords.org | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ha-passwords.org b/ha-passwords.org index 94864ba..e3f9673 100644 --- a/ha-passwords.org +++ b/ha-passwords.org @@ -23,7 +23,34 @@ A literate programming version for Emacs code to generate and store passwords. ;; ;;; Code: #+end_src -* Introduction +* Overlay Redaction +Love [[https://xenodium.com/redact-that-buffer/][this idea]] for using a regular expression to /redact/ (or at least, obscure) sensitive information before screen sharing. +#+begin_src emacs-lisp + (defun toggle-redact-buffer (regexp) + "Redact buffer content matching regexp. A space redacts all." + (interactive (list (read-regexp "Text to Redact (Regexp)" 'regexp-history-last))) + (let* ((redacted) + (matches (let ((results '())) + (when (string-empty-p regexp) + (setq regexp "[[:graph:]]") + (setq regexp-history-last regexp) + (add-to-history 'regexp-history regexp)) + (save-excursion + (goto-char (point-min)) + (while (re-search-forward regexp nil t) + (push (cons (match-beginning 0) (match-end 0)) results))) + (nreverse results)))) + (mapc (lambda (match) + (dolist (overlay (overlays-in (car match) (cdr match))) + (setq redacted t) + (delete-overlay overlay)) + (unless redacted + (overlay-put (make-overlay (car match) (cdr match)) + 'display (make-string (- (cdr match) (car match)) ?x)))) + matches))) +#+end_src +I’m not sure how often I will use this, so I’m not putting it on a keybinding, also so, I will also not put a name-spacing prefix on the function. +* Password Generation Let's assume that I store a bunch of words in data files: #+begin_src emacs-lisp (defvar ha-passwords-data-files (list (expand-file-name "adjectives.txt"