commit f503c45b7156ef47afed0ab6bf98dc1067e79aa7
parent fc307ad821becea95353db58a7133bd5317856c1
Author: Vineet Kumar <git@vineetk.net>
Date: Fri, 22 May 2026 14:50:42 -0400
slightly simplify totp code
Diffstat:
1 file changed, 5 insertions(+), 10 deletions(-)
diff --git 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)))))