summaryrefslogtreecommitdiffstats
path: root/challenge/c22.go
blob: c62c7b861002982849882e2ec46a74d3a325aaaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright © 2021 siddharth <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package challenge

import (
	"fmt"
	"time"

	"ricketyspace.net/cryptopals/lib"
)

func C22() {
	mt := new(lib.MTRand)
	minWait := 40 * time.Second
	maxWait := 1000 * time.Second
	randomWait := func() {
		fmt.Printf("Waiting for a random amount of time...\n")
		s := time.Now()
		lib.SleepRandom(minWait, maxWait)
		fmt.Printf("Elapsed time %v\n", time.Now().Sub(s))
	}

	fmt.Printf("This challenge might take ~%v to ~%v to complete\n",
		2*minWait, 2*maxWait)

	randomWait()
	fmt.Printf("Generating seed from current time...\n")
	mt.Seed(uint32(time.Now().Unix()))
	randomWait()

	fmt.Printf("Extracting first random 32-bit garbage fron RNG...\n")
	random := mt.Extract()

	fmt.Printf("Cracking seed...\n")
	guess := uint32(time.Now().Unix())
	for {
		mt.Seed(guess)
		x := mt.Extract()
		if x == random {
			fmt.Printf("Found seed %v\n", guess)
			break
		}
		guess = guess - 1
	}
}

// Output:
// This challenge might take ~1m20s to ~33m20s to complete
// Waiting for a random amount of time...
// Elapsed time 10m51.671176141s
// Generating seed from current time...
// Waiting for a random amount of time...
// Elapsed time 14m48.257182374s
// Extracting first random 32-bit garbage fron RNG...
// Cracking seed...
// Found seed 1625112975