summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVineet Kumar <git@vineetk.net>2026-05-22 15:05:16 -0400
committerVineet Kumar <git@vineetk.net>2026-05-22 15:06:09 -0400
commit4601077dcb28f21ca96e949d1840d1ba8188260d (patch)
tree9668e16dd4fd6d65855c11a1da8af85d2e74fc18
parenta8ccce5a5fb60c896a24585292602d7edb92874e (diff)
add zero-padding to base32 decoding for odd-length totp secrets
-rw-r--r--kasi-totp.el18
1 files 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 @@
5;; https://github.com/juergenhoetzel/emacs-totp 5;; https://github.com/juergenhoetzel/emacs-totp
6;; https://github.com/grimnebulin/emacs-hmac 6;; https://github.com/grimnebulin/emacs-hmac
7(require 'bindat) 7(require 'bindat)
8(require 'hexl)
9(require 'cl-lib) 8(require 'cl-lib)
9(require 'hexl)
10(require 'subr-x) 10(require 'subr-x)
11 11
12(defconst hmac-algorithm-blocksizes 12(defconst hmac-algorithm-blocksizes
@@ -56,17 +56,17 @@ string."
56 "The cheats' version of base-32 decode. 56 "The cheats' version of base-32 decode.
57 57
58This is not a 100% faithful implementation of RFC 4648. The 58This is not a 100% faithful implementation of RFC 4648. The
59concept of encoding partial quanta is not implemented fully. 59concept of encoding partial quanta is not implemented fully."
60
61No attempt is made to pad the output either as that is not
62required for HMAC-TOTP."
63 (unless (mod (length string) 8) 60 (unless (mod (length string) 8)
64 (error "Padding is incorrect")) 61 (error "Padding is incorrect"))
65 (setq string (upcase string)) 62 (setq string (upcase string))
66 (let ((trimmed-array (append (string-trim-right string "=+") nil))) 63 (let* ((trimmed-array (append (string-trim-right string "=+") nil))
67 (format "%X" (seq-reduce 64 (hex (format "%X" (seq-reduce
68 (lambda (acc char) (+ (ash acc 5) (aref base32-alphabet char))) 65 (lambda (acc char) (+ (ash acc 5) (aref base32-alphabet char)))
69 trimmed-array 0)))) 66 trimmed-array 0))))
67 (if (cl-oddp (length hex))
68 (concat "0" hex)
69 hex)))
70 70
71(defun totp--hex-decode-string (string) 71(defun totp--hex-decode-string (string)
72 "Hex-decode STRING and return the result as a unibyte string." 72 "Hex-decode STRING and return the result as a unibyte string."