commit aec0e2a7c1fac9a2983851c4b8873181b135155f Author: Thomas Hintz Date: Sat Dec 28 06:53:42 2013 -0800 Initial. diff --git a/cl-password.asd b/cl-password.asd new file mode 100644 index 0000000..d78b714 --- /dev/null +++ b/cl-password.asd @@ -0,0 +1,9 @@ +(defpackage :cl-password-system (:use :asdf :cl)) + +(in-package :cl-password-system) + +(defsystem :cl-password + :version "0.1" + :depends-on (ironclad) + :components ((:file "packages") + (:file "cl-password" :depends-on ("packages")))) diff --git a/cl-password.lisp b/cl-password.lisp new file mode 100644 index 0000000..9b4ddc2 --- /dev/null +++ b/cl-password.lisp @@ -0,0 +1,47 @@ +(in-package :cl-password) + +(defparameter *prng* (make-prng :fortuna :seed :random)) + +(defun make-random-salt (&optional (size 16)) + (random-data size *prng*)) + +(defun generate-password-hash-scrypt (password salt hash-length n r p) + (byte-array-to-hex-string + (derive-key + (make-kdf 'scrypt-kdf :n n :r r :p p) + (ascii-string-to-byte-array password) + salt + 0 ; ignored for scrypt + hash-length))) + +(define-condition unsupported-hash-error (error) + ((name :initarg :name :reader name))) + +(defun hash-password + (password type + &key (hash-length 40) + (n 2048) (r 1) (p 1) + (salt (make-random-salt))) + (when (typep salt 'string) (setf salt (hex-string-to-byte-array salt))) + (cond ((eq type :scrypt) + (list :type type + :salt (byte-array-to-hex-string salt) + :n n + :r r + :p p + :hash-length hash-length + :hash (generate-password-hash-scrypt password salt hash-length n r p))) + (t (error 'unsupported-hash-error :name type)))) + +(defun check-password (password hashed-password-plist) + (equalp + (getf + (hash-password password + (getf hashed-password-plist :type) + :hash-length (getf hashed-password-plist :hash-length) + :n (getf hashed-password-plist :n) + :r (getf hashed-password-plist :r) + :p (getf hashed-password-plist :p) + :salt (getf hashed-password-plist :salt)) + :hash) + (getf hashed-password-plist :hash))) diff --git a/packages.lisp b/packages.lisp new file mode 100644 index 0000000..b53f737 --- /dev/null +++ b/packages.lisp @@ -0,0 +1,9 @@ +(in-package :cl-user) + +(defpackage #:cl-password + (:use :cl :ironclad) + (:shadow :null) + (:export #:make-random-salt + #:unsupported-hash-error + #:hash-password + #:check-password))