[Tex/LaTex] How to include a hash of a source file in the final PDF file

pdftexrevision control

Suppose I've got a poem.tex source file.

Is there a way to include a hash of the source file as text in the final PDF? I usually use pdflatex to generate PDF files.

I am looking for something like the result of shasum poem.tex, looking like this:

c0c0fa9a776ea1891264c884e0e69dbde6142c4a,

being added somewhere to the body of the output file of the pdflatex poem.tex command.

Is there an easy way to do it?

Best Answer

pdfTeX contains the primitive \pdfmdfivesum for this purpose, available in recent XeTeX as \mdfivesum and implemented in Lua for LuaTeX in the pdftexcmds package. Using the latter as a wrapper we might have

\documentclass{article}

\usepackage{blindtext}
\usepackage{fancyhdr}
\usepackage{pdftexcmds}
\makeatletter
\ifx\pdf@filemdfivesum\undefined\def\pdf@filemdfivesum#{\mdfivesum file}\fi
\let\filesum\pdf@filemdfivesum
\makeatother
\pagestyle{fancy}
\fancyhf{}

\cfoot{\filesum{\jobname}}

\begin{document}
\blindtext[5]
\end{document}

The primitive syntax (if we assume pdfTeX/XeTeX):

\documentclass{article}

\usepackage{blindtext}
\usepackage{fancyhdr}
\makeatletter
\ifx\pdfmdfivesum\undefined
  \let\pdfmdfivesum\mdfivesum
\fi
\edef\filesum{\pdfmdfivesum file {\jobname}}
\makeatother
\pagestyle{fancy}
\fancyhf{}

\cfoot{\filesum}

\begin{document}
\blindtext[5]
\end{document}
Related Question