diff options
| author | Vineet Kumar <git@vineetk.net> | 2026-05-22 14:56:20 -0400 |
|---|---|---|
| committer | Vineet Kumar <git@vineetk.net> | 2026-05-22 15:05:06 -0400 |
| commit | a8ccce5a5fb60c896a24585292602d7edb92874e (patch) | |
| tree | ec219f4d7425691e7a110ccb6374c177d1200252 | |
| parent | f503c45b7156ef47afed0ab6bf98dc1067e79aa7 (diff) | |
import hmac code from sean mcafee to remove gnutls dependency
| -rw-r--r-- | kasi-totp.el | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/kasi-totp.el b/kasi-totp.el index d14e00a..b8fe078 100644 --- a/kasi-totp.el +++ b/kasi-totp.el | |||
| @@ -1,10 +1,41 @@ | |||
| 1 | ;; TOTP code taken from Mastering Emacs which took from Jürgen Hötzel's `totp.el'. | 1 | ;; TOTP code taken from Mastering Emacs which took from Jürgen Hötzel's `totp.el'. |
| 2 | ;; Base32 code taken from same Mastering Emacs article. | 2 | ;; Base32 code taken from same Mastering Emacs article. |
| 3 | ;; HMAC code taken from Sean McAfee. | ||
| 3 | ;; https://www.masteringemacs.org/article/securely-generating-totp-tokens-emacs | 4 | ;; https://www.masteringemacs.org/article/securely-generating-totp-tokens-emacs |
| 4 | ;; https://github.com/juergenhoetzel/emacs-totp | 5 | ;; https://github.com/juergenhoetzel/emacs-totp |
| 6 | ;; https://github.com/grimnebulin/emacs-hmac | ||
| 5 | (require 'bindat) | 7 | (require 'bindat) |
| 6 | (require 'gnutls) | ||
| 7 | (require 'hexl) | 8 | (require 'hexl) |
| 9 | (require 'cl-lib) | ||
| 10 | (require 'subr-x) | ||
| 11 | |||
| 12 | (defconst hmac-algorithm-blocksizes | ||
| 13 | '((md5 . 64) | ||
| 14 | (sha1 . 64) | ||
| 15 | (sha224 . 64) | ||
| 16 | (sha256 . 64) | ||
| 17 | (sha384 . 128) | ||
| 18 | (sha512 . 128)) | ||
| 19 | "Mapping from HMAC algorithm to the algorithm's blocksize.") | ||
| 20 | |||
| 21 | (defun hmac (algorithm key message &optional binary) | ||
| 22 | "Compute the HMAC for a given message, private key and algorithm. | ||
| 23 | |||
| 24 | ALGORITHM is a symbol naming one of the algorithms recognized by | ||
| 25 | `secure-hash'. KEY is a the private key to use. MESSAGE, a | ||
| 26 | string, is the message to hash. If BINARY is non-nil, the hmac | ||
| 27 | will be returned as a binary string, otherwise as a hexadecimal | ||
| 28 | string." | ||
| 29 | (if-let (blocksize (alist-get algorithm hmac-algorithm-blocksizes)) | ||
| 30 | (progn | ||
| 31 | (when (> (length key) blocksize) | ||
| 32 | (setq key (secure-hash algorithm key))) | ||
| 33 | (when (< (length key) blocksize) | ||
| 34 | (setq key (concat key (make-string (- blocksize (length key)) 0)))) | ||
| 35 | (let ((o-key-pad (cl-map 'string #'logxor key (make-string blocksize #x5c))) | ||
| 36 | (i-key-pad (cl-map 'string #'logxor key (make-string blocksize #x36)))) | ||
| 37 | (secure-hash algorithm (concat o-key-pad (secure-hash algorithm (concat i-key-pad message) nil nil t)) nil nil binary))) | ||
| 38 | (error "Unsupported hash algorithm %s" algorithm))) | ||
| 8 | 39 | ||
| 9 | (defconst base32-alphabet | 40 | (defconst base32-alphabet |
| 10 | (let ((tbl (make-char-table nil))) | 41 | (let ((tbl (make-char-table nil))) |
| @@ -50,7 +81,7 @@ required for HMAC-TOTP." | |||
| 50 | ;; we have to manually split the 64 bit number (u64 not supported in Emacs 27.2) | 81 | ;; we have to manually split the 64 bit number (u64 not supported in Emacs 27.2) |
| 51 | (counter-bytes (bindat-pack '((:high u32) (:low u32)) | 82 | (counter-bytes (bindat-pack '((:high u32) (:low u32)) |
| 52 | `((:high . ,(ash counter -32)) (:low . ,(logand counter #xffffffff))))) | 83 | `((:high . ,(ash counter -32)) (:low . ,(logand counter #xffffffff))))) |
| 53 | (mac (gnutls-hash-mac 'SHA1 key-bytes counter-bytes)) | 84 | (mac (hmac 'sha1 key-bytes counter-bytes)) |
| 54 | (offset (logand (bindat-get-field (bindat-unpack '((:offset u8)) mac 19) :offset) #xf))) | 85 | (offset (logand (bindat-get-field (bindat-unpack '((:offset u8)) mac 19) :offset) #xf))) |
| 55 | (format "%06d" | 86 | (format "%06d" |
| 56 | (mod | 87 | (mod |
