From 4601077dcb28f21ca96e949d1840d1ba8188260d Mon Sep 17 00:00:00 2001 From: Vineet Kumar Date: Fri, 22 May 2026 15:05:16 -0400 Subject: add zero-padding to base32 decoding for odd-length totp secrets --- kasi-totp.el | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/kasi-totp.el b/kasi-totp.el index b8fe078..a237ef7 100644 --- a/kasi-totp.el +++ b/kasi-totp.el @@ -5,8 +5,8 @@ ;; https://github.com/juergenhoetzel/emacs-totp ;; https://github.com/grimnebulin/emacs-hmac (require 'bindat) -(require 'hexl) (require 'cl-lib) +(require 'hexl) (require 'subr-x) (defconst hmac-algorithm-blocksizes @@ -56,17 +56,17 @@ string." "The cheats' version of base-32 decode. This is not a 100% faithful implementation of RFC 4648. The -concept of encoding partial quanta is not implemented fully. - -No attempt is made to pad the output either as that is not -required for HMAC-TOTP." +concept of encoding partial quanta is not implemented fully." (unless (mod (length string) 8) (error "Padding is incorrect")) (setq string (upcase string)) - (let ((trimmed-array (append (string-trim-right string "=+") nil))) - (format "%X" (seq-reduce - (lambda (acc char) (+ (ash acc 5) (aref base32-alphabet char))) - trimmed-array 0)))) + (let* ((trimmed-array (append (string-trim-right string "=+") nil)) + (hex (format "%X" (seq-reduce + (lambda (acc char) (+ (ash acc 5) (aref base32-alphabet char))) + trimmed-array 0)))) + (if (cl-oddp (length hex)) + (concat "0" hex) + hex))) (defun totp--hex-decode-string (string) "Hex-decode STRING and return the result as a unibyte string." -- cgit v1.2.3