0

Is there any (more efficient) way for computing any string with a given prefix in it's SHA 256 hash? And how probable am I to stumble on such a string if I brute force?

3
  • It depends a little on what you mean by 'compute'. Do you need an algorithm that will search for a string that hashes to a specific prefix of any given length? Do you need a way to find a string that hashes to any prefix with a predictable length? Or do you just need to solve the problem once for one prefix? Each of those would be best solved a different way. Can you provide more detail?
    – Johnny
    Commented Apr 12, 2019 at 16:55
  • @Johnny I need an algorithm that will search for a string that hashes to a specific prefix of any given length.
    – yobro97
    Commented Apr 12, 2019 at 17:00
  • Given some (unknown) string y, with a hash ya and of length yb. If your target prefix is exactly ya (that is, the given length is also yb), then you are attempting a preimage attack. Commented Apr 12, 2019 at 17:18

1 Answer 1

2

If I have understood your problem correctly (from the clarification in your comment), in short, brute force will be your only option. But if you know a few boundaries to the problem and you don't mind storing some (potentially very large) amount data you can try to make shortcuts for yourself for the future.

If, for example, you know that you will only ever need to match a maximum prefix length of, say, 32 bits, then each time you calculate a hash that doesn't match your target, save the string and the 32 bit prefix into a table (discarding duplicates, of course). Then, with each future search, you can search for the required prefix in your table to see if you've precalculated it, before resuming your brute force search from where you left off.

Sadly, there's no way to use your precalculated prefixes to exclude candidate strings (such a thing would count as an extraordinarily strong defeat of SHA-256) and shorten your search. My advice would simply be to brute force your way through every 256-bit starting string beginning from all zeros.

2
  • 1
    Do you mean bits rather than bytes? A 32 byte "prefix" would actually be the entire SHA-256 hash. Commented Apr 12, 2019 at 18:57
  • @AndrolGenhald, heh, yes, yes I do mean bits. Thank you for the correction, edited.
    – Johnny
    Commented Apr 12, 2019 at 18:59

Not the answer you're looking for? Browse other questions tagged .