diff --git a/hmac.scm b/hmac.scm index 2f7b7d8..bb82bc6 100644 --- a/hmac.scm +++ b/hmac.scm @@ -9,21 +9,22 @@ (use message-digest-port) (define (hmac key digest-primitive #!optional (block-size 64)) - (when (> (string-length key) block-size) - (set! key (call-with-output-digest digest-primitive (cut display key <>) 'string))) - (set! key (string-pad-right key block-size (integer->char 0))) - (let ((ipad (string-map (lambda (c) (integer->char (bitwise-xor (char->integer c) #x36))) key)) - (opad (string-map (lambda (c) (integer->char (bitwise-xor (char->integer c) #x5c))) key))) - (lambda (message) - (call-with-output-digest - digest-primitive - (cut display - (string-append opad - (call-with-output-digest + (let ((key_ key)) + (when (> (string-length key_) block-size) + (set! key_ (call-with-output-digest digest-primitive (cut display key_ <>) 'string))) + (set! key_ (string-pad-right key_ block-size (integer->char 0))) + (let ((ipad (string-map (lambda (c) (integer->char (bitwise-xor (char->integer c) #x36))) key_)) + (opad (string-map (lambda (c) (integer->char (bitwise-xor (char->integer c) #x5c))) key_))) + (lambda (message) + (call-with-output-digest + digest-primitive + (cut display + (string-append opad + (call-with-output-digest digest-primitive (cut display (string-append ipad message) <>) 'string)) - <>) - 'string)))) + <>) + 'string))))) )