From f503c45b7156ef47afed0ab6bf98dc1067e79aa7 Mon Sep 17 00:00:00 2001 From: Vineet Kumar Date: Fri, 22 May 2026 14:50:42 -0400 Subject: slightly simplify totp code --- kasi-totp.el | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/kasi-totp.el b/kasi-totp.el index 3d197d6..d14e00a 100644 --- a/kasi-totp.el +++ b/kasi-totp.el @@ -5,7 +5,6 @@ (require 'bindat) (require 'gnutls) (require 'hexl) -(require 'auth-source) (defconst base32-alphabet (let ((tbl (make-char-table nil))) @@ -44,21 +43,17 @@ required for HMAC-TOTP." (seq-map (lambda (s) (hexl-htoi (aref s 0) (aref s 1))) (seq-partition string 2)))) -(defun totp (string &optional time digits) - "Return a TOTP token using the secret hex STRING and current time. -TIME is used as counter value instead of current time, if non-nil. -DIGITS is the number of pin digits and defaults to 6." +(defun totp (string) + "Return a TOTP token using the secret hex STRING and current time." (let* ((key-bytes (totp--hex-decode-string (upcase string))) - (counter (truncate (/ (or time (time-to-seconds)) 30))) - (digits (or digits 6)) - (format-string (format "%%0%dd" digits)) + (counter (truncate (/ (time-to-seconds) 30))) ;; we have to manually split the 64 bit number (u64 not supported in Emacs 27.2) (counter-bytes (bindat-pack '((:high u32) (:low u32)) `((:high . ,(ash counter -32)) (:low . ,(logand counter #xffffffff))))) (mac (gnutls-hash-mac 'SHA1 key-bytes counter-bytes)) (offset (logand (bindat-get-field (bindat-unpack '((:offset u8)) mac 19) :offset) #xf))) - (format format-string + (format "%06d" (mod (logand (bindat-get-field (bindat-unpack '((:totp-pin u32)) mac offset) :totp-pin) #x7fffffff) - (expt 10 digits))))) + (expt 10 6))))) -- cgit v1.2.3