# What is the probability of a hash containing a substring?

An SHA-$256$ hash is one that is $64$ digits long, where every digit is a hex value ($0$-$9$ and a-f). Here’s an example hash:

fffbdf$15$af$1$b$58$fc$48$d$5562$b$3$e$12$a$2$bb$9$ea$91$e$78324$f$3909723$bf$048396773$dc

Now, I would like to calculate the probability of a hash containing a (random) substring with $n$ digits (the substring is also made up of hex values).

For example, what would be the probability of the hash containing a random 2-digit substring (e.g.‘c$8$’)?

Or what would be the probability of the hash containing a random $3$-digit substring (e.g. ‘c$84$’)?

I did some numerical tests using OpenOffice, giving a 1.5% probability for a 3-digit string. But I don’t know how to exactly calculate the probabilities.

Thanks for your suggestions and hints!

-
Those are two different questions. First you're asking about a random substring, then about specific substrings with different digits. The latter is not, as you suggest, an example of the former, and the answers will differ. For instance, the number of $3$-digit hexadecimal strings containing 'ff' is $31$ whereas the number of $3$-digit hexadecimal strings containing 'c8' is $32$. –  joriki Jan 18 at 9:24
@joriki Yes, you're right. I just edited my question. –  Max Min Jan 18 at 12:49