Skip to content

Instantly share code, notes, and snippets.

@pnathan
Created November 23, 2013 09:51
Show Gist options
  • Save pnathan/7612750 to your computer and use it in GitHub Desktop.
Save pnathan/7612750 to your computer and use it in GitHub Desktop.
Calculate the keyspace of a password.
(defun keyspace (string)
"Calculates the keyspace of an ASCII string; this can be used to
deny certain passwords from being used."
;; Hopefully the implementation is a smart string and knows how long
;; it is automatically.
(let ((length (length string))
(capabilities (make-hash-table :test #'eq)))
;; O(n)
(loop for ele across string
do
(cond
((upper-case-p ele)
(setf (gethash :upper-case capabilities) 26))
((lower-case-p ele)
(setf (gethash :lower-case capabilities) 26))
((digit-char-p ele)
(setf (gethash :digit capabilities) 10))
(t
(setf (gethash :other capabilities) 33))))
;; O(n)
(expt (loop for value being the hash-values of capabilities
sum value)
length)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment