Created
February 9, 2025 00:22
-
-
Save HeitorAugustoLN/308a1ad1ddb4305b488017a247bb4082 to your computer and use it in GitHub Desktop.
Password generator in steel scheme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require-builtin "steel/random") | |
(define lowercase-chars "abcdefghijklmnopqrstuvwxyz") | |
(define uppercase-chars "ABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
(define number-chars "0123456789") | |
(define special-chars "!@#$%^&*()_+-=[]{}|;:,.<>?") | |
(define (random-char str) | |
(string-ref str (rng->gen-range 0 (string-length str)))) | |
(define (shuffle-string str) | |
(define len (string-length str)) | |
(define char-list (string->list str)) | |
(define (shuffle lst) | |
(if (null? lst) | |
'() | |
(let* ((i (rng->gen-range 0 (length lst))) | |
(item (list-ref lst i)) | |
(rest (append (take lst i) (drop lst (+ i 1))))) | |
(cons item (shuffle rest))))) | |
(list->string (shuffle (string->list str)))) | |
(define (generate-password length | |
#:use-lowercase? (use-lowercase? #t) | |
#:use-uppercase? (use-uppercase? #t) | |
#:use-numbers? (use-numbers? #t) | |
#:use-special? (use-special? #t)) | |
(if (< length 4) | |
(error "Password length must be at least 4 characters") | |
(let* ( | |
(initial-chars | |
(string-append | |
(if use-lowercase? (string (random-char lowercase-chars)) "") | |
(if use-uppercase? (string (random-char uppercase-chars)) "") | |
(if use-numbers? (string (random-char number-chars)) "") | |
(if use-special? (string (random-char special-chars)) ""))) | |
(char-pool | |
(string-append | |
(if use-lowercase? lowercase-chars "") | |
(if use-uppercase? uppercase-chars "") | |
(if use-numbers? number-chars "") | |
(if use-special? special-chars ""))) | |
(remaining-length (- length (string-length initial-chars))) | |
(remaining-chars | |
(let loop ((n remaining-length) (result "")) | |
(if (zero? n) | |
result | |
(loop (- n 1) | |
(string-append result (string (random-char char-pool))))))) | |
;; Combine and shuffle the password | |
(final-password (shuffle-string (string-append initial-chars remaining-chars)))) | |
final-password))) | |
;; Example usage: | |
(display "Generated Passwords:\n") | |
(display "Default (all character types): ") | |
(display (generate-password 12)) | |
(display "\n") | |
(display "Only lowercase and numbers: ") | |
(display (generate-password 8 | |
#:use-uppercase? #f | |
#:use-special? #f)) | |
(display "\n") | |
(display "Strong password (16 chars): ") | |
(display (generate-password 16)) | |
(display "\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment