Secure and weak generators
Generators are divided into two types. A simple PRNG (for example, the random module, LCG, Mersenne Twister) works fast and gives statistically nice numbers, but an attacker who has seen a few outputs can recover its internal state and calculate both the future and the past.
A cryptographic generator (CSPRNG), however, relies on the operating system's entropy, and it is practically impossible to recover the internal state from its outputs. In Python, this is the secrets module and os.urandom. The rule is simple: a password, token, key, nonce, session ID — only from a CSPRNG.
Part 1. Theoretical choice
Of the 7 codes below, three are cryptographically secure, the rest are vulnerable. Determine the numbers of the secure ones.
Code 1:
parol = str(random.randint(100000, 999999))
Code 2:
token = secrets.token_hex(16)
Code 3:
x = (1103515245 * x + 12345) % 2**31
return x
Code 4:
kalit = os.urandom(32)
Code 5:
random.seed(int(time.time()))
sid = random.getrandbits(128)
Code 6:
sid = hashlib.md5(str(time.time()).encode()).hexdigest()
Code 7:
kalit = secrets.SystemRandom().getrandbits(256)
Part 2. Practical verification
The attached file contains two sequences: A and B. Both consist of numbers from 0 to 231 and look "random". But one is obtained from a vulnerable LCG.
Hint: in an LCG, if x = (a·x + c) mod 2k and a, c are odd, the least significant bit of the numbers (even/odd) strictly alternates. That is, even, odd, even, odd... This can be checked in a few lines.
Assemble the answer in the following format — first the numbers of the secure codes in ascending order, then the letter of the vulnerable sequence: BAYROQ{PRNG_xavfsiz_<r1>_<r2>_<r3>_zaif_<harf>} (the letter is a capital A or B).