# What is the probability of a SHA-512 hash containing both digits (0-9) and alphabets (a-f) all the time?

Quite curious to know if ever a SHA-512 hash will contain only digits or only alphabets. Thanks in advance.

• That's not really related to crypto at all, it is a purely combinatorial question that could be asked on any hash function, not necessarily cryptographically secure hashes. Have you tried computing the probabilities, assuming that SHA-512 hashes are uniformly distributed over its range? It should be rather straightforward. – Geoffroy Couteau Jan 23 '18 at 13:37
• Geoffroy, I wasn't aware about the output encoding fact in SHA-512 (or any hashing algorithm in general), which Ella and Chris covered very well with good examples. Thanks for your inputs though. – Anonymous Jan 24 '18 at 13:29

A cryptographic hash outputs a string of bits.

Whether or not it appears as digits/alphabet letters is a matter of how the output is encoded and presented to the user.

You could easily ensure that nothing but digits/alphabet letters in what is presented to a user by performing base conversion on the output, from a base-2 system (binary) using the symbols $0$, $1$ to a base-$n$ system using arbitrary symbols.

For example, If you encode the output in the hexadecimal number base, a user will see only hexadecimal values (0-9 + A-F).

If you wanted to operate under the assumption of a given number base, you would do some math to quantify the probability of a given subset of symbols occurring in the output - but you have to pick a specific encoding first before you can do so, as the choice of encoding will drastically chance the probability.

For example, consider an encoding that uses (0-9 + A-Z + !) - The probability of sha512 outputting a string with only the symbols (0-9 + A-Z) is relatively high. While in the opposite case, if the encoding only uses (! + ?), the probability is non-existent.

• Thanks, Ella. Readers from future, please have a look at Chris's answer too. – Anonymous Jan 23 '18 at 6:02

It depends both on how you model SHA-512 and how you represent digits and letters.

For simplicity let's assume that SHA-512 outputs bits uniformly and at random, at that learning one bit of the output tells us nothing about the other bits. This is not the standard definition for a hash function, but will simplify the calculation significantly. Let's also assume we use Base64 to represent the bits output by SHA-512.

In Base64 the output range is `A-Za-z0-9+/'. This means that we have a $10/64$ chance of picking a number and a $52/64$ chance of picking a letter, when we pick one output at random.

A 512-bit hash will contain $512/6 = 85.3$ Base64 characters.

Now let's think about the probability that the first six bits of the hash will be a letter, or a number:

$P[$first 6 bits are a letter$]$$=\frac{52}{64} \approx 0.812 P[first 6 bits are a number]$$=\frac{10}{64} \approx 0.157$

How about the first twelve bits? We can multiply because we're assuming that SHA-512 outputs bits independently, uniformly and at random:

$P[$first 12 bits are a letter$]$$=\frac{52}{64}\times\frac{52}{64} = \frac{169}{256} \approx 0.66 P[first 12 bits are a numbers]$$=\frac{10}{64}\times\frac{10}{64} = \frac{25}{1024} \approx 0.024$

Finally we can combine our definitions and use the probability that we get a letter or a number ($62/64$) to perform the calculation for all $85.3$ characters

$P[$SHA-512 output contains only letters and numbers$]$ $=(\frac{62}{64})^{85.3} \approx 0.066$

or around one in fifteen times.

Of course, this is only because in this model there is a $1/32$ chance of each unique six bits of the output being either the character '+' or /'

Hopefully you can adapt this answer to suit the bit encoding model that you had in mind.

• I wish I could mark this answer as the correct one too. – Anonymous Jan 23 '18 at 6:00

It seems what you want to ask is what is the probability that SHA-512 output encoded as hexadecimal will be entirely letters? or entirely digits. We like to believe SHA-512 produces output indistinguishable from uniformly random so we will model as such. 512 bits require 128 characters in hexadecimal encoding (4 bit each).

For all numbers we get $(10/16)^{128}$ this is ~7e-27 also known as zero for all intents and purposes.

For all letters we get $(6/16)^{128}$ which is ~3e-55 even closer to zero. If you convert the entire bitcoin network to work on this problem alone the sun will die before it finds such a value.