Advertisement
Guest User

guile password generator

a guest
Mar 8th, 2019
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.80 KB | None | 0 0
  1. ;; generate passwords like this
  2. ;; (generate-password 10 (list numeric alpha-lower) (make-rule alpha-lower #:max 5 #:min 5))
  3. ;; This will generate a 10 character password with lowercase alphabetic characters and numeric caracters.
  4. ;; it also makes sure that you have a maximum AND minimum of 5 lowercase alphabetic characters.
  5. ;; public domain
  6. ;; WARNING: there is something wonky about it. I generated a million numeric characters, and the average
  7. ;; is always a bit low (around 4.45 instead of 4.5 in all tests run), so it seems my maths skills are off
  8.  
  9. (use-modules
  10.  (rnrs arithmetic fixnums)
  11.  (srfi srfi-1)
  12.  (ice-9 binary-ports))
  13.  
  14.  
  15. (define alpha-lower
  16.   (ucs-range->char-set 97 123))
  17.  
  18. (define alpha-upper
  19.   (ucs-range->char-set 67 91))
  20.  
  21. (define numeric
  22.   (ucs-range->char-set 48 58))
  23.  
  24. ;; looking at some different keyboard layouts, these seem to be accessible in most languages
  25. (define special
  26.   (->char-set "!#%&/()=?+.,:;-_[]><"))
  27.  
  28.  
  29. (define urandom (open-input-file "/dev/urandom"))
  30.  
  31. (define (false->empty-charset x)
  32.   (or x char-set:empty))
  33.  
  34. (define* (%generate-password len char-sets)
  35.   (let* ((char-string (char-set->string (apply char-set-union char-sets)))
  36.          (size (string-length char-string))
  37.          (res (make-string len)))
  38.     (do ((i 0 (1+ i)))
  39.         ((>= i len))
  40.       (let ((byte (remainder (get-u8 urandom) size)))
  41.         (string-set! res i (string-ref char-string byte))))
  42.     res))
  43.  
  44. (define* (make-rule cs #:key (min 0) (max (greatest-fixnum)))
  45.   (lambda (str)
  46.     (let ((n (string-count str cs)))
  47.       (and (>= n min) (<= n max)))))
  48.  
  49.  
  50. (define (generate-password len char-sets .  rules)
  51.   (let loop ((pass (%generate-password len char-sets)))
  52.     (if (every (lambda (proc) (proc pass)) rules)
  53.         pass
  54.         (loop (%generate-password len char-sets)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement