Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

python - Reversing random generation function

I have a random generation function that creates a string of random numbers based on a seed.

hash_ = '1'
for i in range(5):
    m = hashlib.sha256()
    m.update(hash_.encode("utf-8"))
    hash_ = m.hexdigest()
    print(hash_)

This outputs

6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b
e0bc614e4fd035a488619799853b075143deea596c477b8dc077e309c0fe42e9
d6a804981ea7ce374acc21c9a8bf82f50b684b0ea4bdf8b26a7a775291aaf7a6
ad376767fc04814220cc25c79b2777cd14704f23f1830318b5bd9eb97e4fedf6
b80de1ab9d0903823cc10fd5b2ba57616b339a69313b4d3e23363dbea6579cd6

And will output this every single time

My question is if it would be possible and how could I possibly reverse this function. So I start the function with: hash_ = b80de1ab9d0903823cc10fd5b2ba57616b339a69313b4d3e23363dbea6579cd6 and the last hash it would output would be: 6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b

Just wondering if this is even possible thanks for all help :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use for .. else ... grammar.
After all loop completed( if no error), will execute the code.
Like this:

hash_ = '1'
for i in range(5):
    m = hashlib.sha256()
    m.update(hash_.encode("utf-8"))
    hash_ = m.hexdigest()
else:
    print(hash_)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...