How to include a hash of a source file in the final PDF file?
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}
This stores the checksum in variable \shasum
. All the \string\\\string\\
is needed to get \\\\
into the shell stream. Option --shell-escape
is needed of course. The advantage of this solution is that the result is stored in a macro, which is more natural IMHO.
\documentclass{article}
\makeatletter
\begingroup
\catcode`\%=11
\immediate\write18{printf "\string\\\string\\edef\string\\\string\\shasum{%s}" `shasum \jobname.tex | awk '{print $1}'` > \jobname.sha}
\endgroup
\input{\jobname.sha}
\makeatother
\begin{document}
The checksum is \shasum.
\end{document}
An alternative Linux/Unix - shell-escape based approach (although there is \pdfmdfivesum file {yourfilename}
)
It writes the hash to a file and reads it back to the original file.
\documentclass{article}
\usepackage{blindtext}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\AtBeginDocument{%
\immediate\write18{shasum \jobname.tex | awk '{print $1}'> \jobname.hash}
}
\cfoot{\input{\jobname.hash}}
\begin{document}
\blindtext[5]
\end{document}
A variation with reading to a macro:
\documentclass{article}
\usepackage{blindtext}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\newread\hashfile
\AtBeginDocument{%
\immediate\write18{shasum \jobname.tex | awk '{print $1}'> \jobname.hash}
\openin\hashfile=\jobname.hash
\read\hashfile to \filehash
\closein\hashfile
}
\cfoot{\filehash}
\begin{document}
\blindtext[5]
\end{document}