[Tex/LaTex] Linebreaks in long character strings

line-breakingstringstypewriter

I would like to print a number of 256-bit long hashes in hexadecimal (so 64 characters, I'm skipping the usual "0x" without white spaces or punctuation) in line and using a monospaced font.

So, I first went for \texttt, which does not hyphenate my strings.
I saw some questions with interesting answers, such as How to get long \texttt sections to break and Automatic linebreak on specific character but those both define linebreak for a specific character and I would find ugly to use their trick for each of the 16 characters of hexadecimal without some sort of natural loop.

Is there a way to define a command such that the text inside will be typed as texttt but break on any character or, even better, a standard way to typeset long hexadecimal strings that takes this issue into account?

MWE:

\documentclass{article}

\newcommand{\hash}[1]{\texttt{#1}}%In a perfect world, this would be changed to allow linebreaks anywhere in #1

\begin{document}
SHA-256 is a hash function with a 256-bit long output: \hash{d270f747a8743f11aef93c10e9cb6932cc0b862464c1133dc0f8889088740d15}
\end{document}

Best Answer

There is already a package for this:

\documentclass{article}
\usepackage{seqsplit}

\newcommand{\hash}[1]{{\ttfamily\seqsplit{#1}}}

\begin{document}

SHA-256 is a hash function with a 256-bit long output:
\hash{d270f747a8743f11aef93c10e9cb6932cc0b862464c1133dc0f8889088740d15}

\end{document}

enter image description here

Related Question