diff options
author | siddharth <s@ricketyspace.net> | 2021-07-01 00:42:46 -0400 |
---|---|---|
committer | siddharth <s@ricketyspace.net> | 2021-07-01 00:42:46 -0400 |
commit | ce4fd171d1bd6bd2a0c478d78151d4d379f5380b (patch) | |
tree | c00c3db23cdfffa1a35b3b70faa4fad497c89723 | |
parent | 78cbe992554636a91a655bc2641f023aae75b204 (diff) |
challenge: do challenge 22
-rw-r--r-- | challenge/c22.go | 56 | ||||
-rw-r--r-- | cryptopals.go | 2 | ||||
-rw-r--r-- | lib/time.go | 11 |
3 files changed, 69 insertions, 0 deletions
diff --git a/challenge/c22.go b/challenge/c22.go new file mode 100644 index 0000000..603cb9e --- /dev/null +++ b/challenge/c22.go @@ -0,0 +1,56 @@ +// Copyright © 2021 rsiddharth <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +package challenge + +import ( + "fmt" + "time" + + "ricketyspace.net/cryptopals/lib" +) + +func C22() { + 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") + lib.MTSeed(uint32(time.Now().Unix())) + randomWait() + + fmt.Printf("Extracting first random 32-bit garbage fron RNG...\n") + random := lib.MTExtract() + + fmt.Printf("Cracking seed...\n") + guess := uint32(time.Now().Unix()) + for { + lib.MTSeed(guess) + x := lib.MTExtract() + 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 diff --git a/cryptopals.go b/cryptopals.go index 98b6759..4767a37 100644 --- a/cryptopals.go +++ b/cryptopals.go @@ -61,5 +61,7 @@ func main() { challenge.C20() case 21: challenge.C21() + case 22: + challenge.C22() } } diff --git a/lib/time.go b/lib/time.go new file mode 100644 index 0000000..dbeb843 --- /dev/null +++ b/lib/time.go @@ -0,0 +1,11 @@ +// Copyright © 2021 rsiddharth <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +package lib + +import "time" + +// Sleeps for atleast `min` but not more than `max`. +func SleepRandom(min, max time.Duration) { + time.Sleep(time.Duration(RandomInt(int64(min), int64(max)))) +} |