Search code examples
elisp

Can't pass variable to secure-hash


Why does secure-hash fail when I use a variable on the second call? Emacs 27.1, 27.2 and 26.3

(setq mytext "test")
(message (secure-hash 'sha256' mytext))
(message (secure-hash 'sha256' "test"))

Solution

  • You have an extra quote ' before mytext that prevents evaluation of the mytext variable.

    The following works just fine:

    (let ((mytext "test"))
      (secure-hash 'sha256 mytext))
    ==> "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
    (secure-hash 'sha256 "test")
    ==> "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
    

    PS. If you are new to Lisp and this was not just a typo, you will greatly benefit from reading "An Introduction to Programming in Emacs Lisp". The best way to do it is in Emacs: C-h i m intro TAB RET.