Skip to content

Commit

Permalink
Added a function to generate nonce via /dev/urandom on Unix. This is …
Browse files Browse the repository at this point in the history
…a fix for the problem

that (random most-positive-fixnum) with SBCL 1.1.6 on OS X does not create enough randomness.
The nonces start repeating after less than a dozen attempts.
  • Loading branch information
Wolfgang Mederle committed Apr 2, 2013
1 parent 4ca69c6 commit 03678ba
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/core/consumer.lisp
Expand Up @@ -32,13 +32,21 @@ it has query params already they are added onto it."
additional-headers) additional-headers)
drakma-args)) drakma-args))


;;; SBCL 1.1.6 on OS X does not generate proper random values with (random most-positive-fixnum).
(defun generate-nonce (&optional (size 30))
(with-open-file (in "/dev/urandom" :direction :input :element-type '(unsigned-byte 8))
(with-output-to-string (out)
(loop :repeat size
:do (write (read-byte in) :stream out :pretty nil :base 36)))))

(defun generate-auth-parameters (defun generate-auth-parameters
(consumer signature-method timestamp version &optional token) (consumer signature-method timestamp version &optional token)
(let ((parameters `(("oauth_consumer_key" . ,(token-key consumer)) (let ((parameters `(("oauth_consumer_key" . ,(token-key consumer))
("oauth_signature_method" . ,(string signature-method)) ("oauth_signature_method" . ,(string signature-method))
("oauth_timestamp" . ,(princ-to-string timestamp)) ("oauth_timestamp" . ,(princ-to-string timestamp))
("oauth_nonce" . ,(princ-to-string #+unix ("oauth_nonce" . ,(generate-nonce))
(random most-positive-fixnum))) #-unix ("oauth_nonce" . ,(princ-to-string
(random most-positive-fixnum)))
("oauth_version" . ,(princ-to-string version))))) ("oauth_version" . ,(princ-to-string version)))))
(if token (if token
(cons `("oauth_token" . ,(url-decode (token-key token))) parameters) (cons `("oauth_token" . ,(url-decode (token-key token))) parameters)
Expand Down

0 comments on commit 03678ba

Please sign in to comment.