I'm trying to cache the results of a macro, similar to that question, but the argument of the macro can contain arbitrary characters and it's not suitable to be inside \csname ... \endcsname.

So, I'm wondering: surely some of the TeX core or packages contain functionality to calculate a hash, MD5 or otherwise (I don't really care which). But… I can't find it. The only result I've got from grep'ing my TeXlive tree is pdfmdfivesum, but it only works on files, not strings.

So: are there ready-made hash calculation macros/packages available somewhere?

  • 3
    Your mention of \pdfmdfivesum suggests you've looked at using the pdfTeX primitive. It doesn't need a file, it needs <general text>, so seems suitable. However, it's tied to pdfTeX. Is that OK, or do we need a solution for other engines too? (LuaTeX can break out to Lua, XeTeX can't, classical TeX also more tricky.)
    – Joseph Wright
    Commented Jun 27, 2015 at 18:09

1 Answer 1


\pdfmdfivesum also works on arbitrary strings:

\pdfmdfivesum{Hello World}



The hex string can be decoded to save space:

\pdfunescapehex{\pdfmdfivesum{Hello World}}

\pdfmdfivesum is expandable and can be used inside \edef.

\pdfmdfivesum works on file only, when the keyword file is given:

\pdfmdfivesum file {<filename>}

Package pdftexcmds

\pdfmdfivesum is available in pdfTeX in both modes DVI and PDF. Package pdftexcmds defines missing pdfTeX primitives in LuaTeX. The package also works in plain TeX (\input pdftexcmds.sty). The command names are using the prefix \pdf@ instead of \pdf:

\pdf@mdfivesum{Hello World}


  • Older versions, e.g., XeTeX (3.14159265-2.6-0.99992), do not support MD5 sums.
  • \pdffivesumm was added around version 0.99993 from pdfTeX and later renamed to \mdfivesum. Thus, the current version (3.14159265-2.6-0.99996) calculates MD5 sums via \mdfivesum. Also keyword file is supported as in pdfTeX.
  • Can anyone give me a hint, how to get only the first three signs of the hash?
    – PascalS
    Commented Mar 10 at 6:52
  • @PascalS With package pdftexcmds and macro \CarThree defined by \def\CarThree#1#2#3#4\@nil{#1#2#3}, then \expandafter\expandafter\expandafter\CarThree\pdf@mdfivesum{Hello World}{}{}{}\@nil gives B10. Don't forget \makeatletter/\makeatother if necessary. Commented May 18 at 5:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .