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?
1 Answer
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.
-
1Do 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.– JohnnyCommented Apr 12, 2019 at 18:59
y
, with a hashya
and of lengthyb
. If your target prefix is exactlyya
(that is, the given length is alsoyb
), then you are attempting a preimage attack.