-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivacy-output-stream.lisp
36 lines (29 loc) · 1.46 KB
/
privacy-output-stream.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
;;; privacy-output-stream
;;;
;;; SPDX-License-Identifier: MIT
;;;
;;; Copyright (C) 2023 Anthony Green <[email protected]>
(in-package :privacy-output-stream)
(defclass privacy-output-stream (fundamental-character-output-stream)
((stream :initarg :stream :reader stream-of)
(secrets :initarg :secrets :reader secrets :initform nil)))
(defmethod stream-element-type ((stream privacy-output-stream))
(stream-element-type (stream-of stream)))
(defmethod close ((stream privacy-output-stream) &key abort)
(close (stream-of stream) :abort abort))
(defmethod stream-force-output ((stream privacy-output-stream))
(force-output (stream-of stream)))
(defmethod stream-write-char ((stream privacy-output-stream)
char)
(write-char char (stream-of stream)))
(defmethod stream-write-string ((stream privacy-output-stream)
string &optional start end)
(dolist (secret (secrets stream))
(let ((secret (secret-value:reveal-value secret)))
(setf string (with-output-to-string (out)
(loop with start = 0
for pos = (search secret string :start2 start)
do (write-string string out :start start :end pos)
when pos do (dotimes (x (length secret)) (write-char #\* out))
while pos do (setf start (+ pos (length secret))))))))
(write-string string (stream-of stream) :start start :end end))