jeudi 10 mars 2022

How can I get the random seed that was used from results and python random.getstate()?

So I run the following code to produce the following snippet around 8:30 est 3/10/2022. I say this because when Python fails to get the OS-specific randomness source then by-default current system time is used as a seed value so perhaps it's relevant.

import random
import hashlib
import time

chrs = "abcdefghijklmnopqrstuvwxyz0123456789"
chrs = chrs.upper()

def nStr(n):
    ans = ""
    for k in range(0,n):
        ans+=chrs[int(random.random()*len(chrs))]
    return(ans)

def passes(h):
    tags = ["13370","00000000","11111111","22222222","33333333","44444444","55555555","66666666","77777777","88888888","99999999",
            "aaaaaaaa","bbbbbbbb","cccccccc","dddddddd","eeeeeeee","ffffffff"]
    match = False
    for k in tags:
        if h.startswith(k):
            match = True
            break
    return match

count = 0
s = nStr(64)
start = time.time()
t = 6
while time.time() - start < t:
    while time.time() - start < t:
        count+=1
        s = nStr(64)
        h = hashlib.sha256(s.encode()).hexdigest()
        if passes(h):
            break
    if passes(h):
        print(s)
        print(h)
        print()
print(f"hash rate: {count/t} hash/second")

Output From running at Approximately 8:30 EST 3/10/2020

And I know that pythons random module has a state property that looks like this:

>>> s = random.getstate
>>> dir(s)
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> def series(state,length):
...     random.setstate(state)
...     return [random.random() for k in range(length)]
...
>>> s = random.getstate()
>>> series(s,3)
[0.6578281745562433, 0.3851968268567585, 0.8303449881701223]
>>> series(s,3)
[0.6578281745562433, 0.3851968268567585, 0.8303449881701223]
>>> random.random()
0.5270977870567883
>>> 

I'm wondering if there is a way to retrieve the original state of random when I ran this code and or the random state when the hashed sequence was generated... I didn't save it. Perhaps it's still on system memory although that feels like a separate question about pycharm specifically. Any and all input is appreciated.

Edit: The terms seed and state seem to get a little confused here I think of the state as being the seed because seed is just state F(PRNG i=0) I use the 2 interchangeably and just ignoring the subtleties of i!=0.




Aucun commentaire:

Enregistrer un commentaire