Given the SHA-256 hash, the size and the encoding of the original string, is it possible to recover the string via brute force? How many string of a given size will result in the same hash?


Given the original length L of the string, you could consider that the effort to retrieve it is 92 power L (92 is 26 lowercase + 26 uppercase + 10 digits + 30 punctuation) instead of 2 power 256.

So if your string is more than 39 chars long, knowing the lenght gives no help.

EDIT: we should consider that an opponent which tries to recover the original string will not actually bruteforce each character. He will bruteforce words.
Except if the string contains a random password, an unknown lastname or something else which is not in a dictionnary, this is the best approach.
With an upper bound of 1,022,000 words and a lower bounds of 171,476 words (words currently used) the order of magnitude can be calculated by 1000000 power N (N is the number of words in the string).
So if the sentence is less than 13 words long, it could be broken before the hash.
However, we have to consider that all the words permutations are not relevant. the opponent could use a smart approach in the building of the candidates to avoid meaningless sentences (for example testing "the" after another "the" could be avoid, testing "than" after "most" makes no sense too).
Following this approach, knowing the number of characters of the string is helpful for the opponent because it will exclude many candidates.
But I'm not sure that the time spent to test the length and the meaningfulness of the sentence is not bigger than just hash it and compare it.

| improve this answer | |
  • Why 39 characters is the maximum length for recover? – Max Jul 10 at 19:08
  • 1
    Because 92^39 ~= 2^256. 2^256 is the number of possible SHA256 hashes. So, if the string is >39 characters, it's 'easier' to brute-force the hash than the string. – mti2935 Jul 10 at 20:22
  • @mti2935 Isn't there only one way to brute force the hash, which is comparing the target hash with the hash generated from different strings one by one? – Max Jul 11 at 15:46
  • 1
    Yes. But, if the length of the string is >39, then you'll crack the hash before you cycle through all of the combinations of different strings, because there are 'only' 2^256 possible outcomes of a SHA256 hash. In other words, once the string is >39 characters in length, it doesn't matter how long the string is, because you'll find a string that produces the target hash after a maximum of 2^256 tries. – mti2935 Jul 11 at 16:15
  • 1
    Keep in mind that if your string is longer than 39 chars, you will crack the hash before finding the good string. But the reversed hash will not be the real string, it will only be a collision. So if the string is a secret, it remains unviolated even after the first success in the 2^256 attempts – Sibwara Jul 11 at 20:39

There are 2^256 hashes, so approximately 1 in 2^256 strings will have a particular hash.

Therefore the number of strings with that hash is approximately the total number of strings divided by 2^256.

How long is the length? There are a lot of strings to try. You'd have to use up all of the sun's energy just to count to 2^256, never mind hashing. But if the length is only 32 bits, you only have to try 2^32 strings which is a lot less.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.