| 1 | package goproxy_testscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/" |
| 5 | "crypto/rsascip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/" |
| 6 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/" |
| 7 | "ioscip-go gomod github.com/golang/go/src go1.18 io/" |
| 8 | "mathscip-go gomod github.com/golang/go/src go1.18 math/" |
| 9 | "math/randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/" |
| 10 | "testingscip-go gomod github.com/golang/go/src go1.18 testing/" |
| 11 | |
| 12 | "github.com/elazarl/goproxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/" |
| 13 | ) |
| 14 | |
| 15 | type RandSeedReaderscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/RandSeedReader# struct { |
| 16 | rscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/RandSeedReader#r. randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.Randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/Rand# |
| 17 | } |
| 18 | |
| 19 | func (r *RandSeedReaderscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/RandSeedReader#) Readscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/RandSeedReader#Read().(b []byte) (n int, err error) { |
| 20 | for i := range b { |
| 21 | b[i] = byte(r.rscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/RandSeedReader#r..Intscip-go gomod github.com/golang/go/src go1.18 `math/rand`/Rand#Int().() & 0xFF) |
| 22 | } |
| 23 | return len(b), nil |
| 24 | } |
| 25 | |
| 26 | func TestCounterEncDifferentConsecutivescip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/TestCounterEncDifferentConsecutive().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 27 | k, err := rsascip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/.GenerateKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/GenerateKey().(&RandSeedReaderscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/RandSeedReader#{*randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.Newscip-go gomod github.com/golang/go/src go1.18 `math/rand`/New().(randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.NewSourcescip-go gomod github.com/golang/go/src go1.18 `math/rand`/NewSource().(0xFF43109))}, 128) |
| 28 | fatalOnErrscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/fatalOnErr().(err, "rsa.GenerateKey", t) |
| 29 | c, err := goproxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/.NewCounterEncryptorRandFromKeyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/NewCounterEncryptorRandFromKey().(k, []byte("the quick brown fox run over the lazy dog")) |
| 30 | fatalOnErrscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/fatalOnErr().(err, "NewCounterEncryptorRandFromKey", t) |
| 31 | for i := 0; i < 100*1000; i++ { |
| 32 | var a, b int64 |
| 33 | binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.Readscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/Read().(&c, binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian., &a) |
| 34 | binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.Readscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/Read().(&c, binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian., &b) |
| 35 | if a == b { |
| 36 | t.Fatalscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatal().("two consecutive equal int64", a, b) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestCounterEncIdenticalStreamsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/TestCounterEncIdenticalStreams().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 42 | k, err := rsascip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/.GenerateKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/GenerateKey().(&RandSeedReaderscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/RandSeedReader#{*randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.Newscip-go gomod github.com/golang/go/src go1.18 `math/rand`/New().(randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.NewSourcescip-go gomod github.com/golang/go/src go1.18 `math/rand`/NewSource().(0xFF43109))}, 128) |
| 43 | fatalOnErrscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/fatalOnErr().(err, "rsa.GenerateKey", t) |
| 44 | c1, err := goproxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/.NewCounterEncryptorRandFromKeyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/NewCounterEncryptorRandFromKey().(k, []byte("the quick brown fox run over the lazy dog")) |
| 45 | fatalOnErrscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/fatalOnErr().(err, "NewCounterEncryptorRandFromKey", t) |
| 46 | c2, err := goproxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/.NewCounterEncryptorRandFromKeyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/NewCounterEncryptorRandFromKey().(k, []byte("the quick brown fox run over the lazy dog")) |
| 47 | fatalOnErrscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/fatalOnErr().(err, "NewCounterEncryptorRandFromKey", t) |
| 48 | nout := 1000 |
| 49 | out1, out2 := make([]byte, nout), make([]byte, nout) |
| 50 | ioscip-go gomod github.com/golang/go/src go1.18 io/.ReadFullscip-go gomod github.com/golang/go/src go1.18 io/ReadFull().(&c1, out1) |
| 51 | tmp := out2[:] |
| 52 | randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.Seedscip-go gomod github.com/golang/go/src go1.18 `math/rand`/Seed().(0xFF43109) |
| 53 | for len(tmp) > 0 { |
| 54 | n := 1 + randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.18 `math/rand`/Intn().(256) |
| 55 | if n > len(tmp) { |
| 56 | n = len(tmp) |
| 57 | } |
| 58 | n, err := c2.Readscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/CounterEncryptorRand#Read().(tmp[:n]) |
| 59 | fatalOnErrscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/fatalOnErr().(err, "CounterEncryptorRand.Read", t) |
| 60 | tmp = tmp[n:] |
| 61 | } |
| 62 | if !bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Equalscip-go gomod github.com/golang/go/src go1.18 bytes/Equal().(out1, out2) { |
| 63 | t.Errorscip-go gomod github.com/golang/go/src go1.18 testing/common#Error().("identical CSPRNG does not produce the same output") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func stddevscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/stddev().(data []int) float64 { |
| 68 | var sum, sum_sqr float64 = 0, 0 |
| 69 | for _, h := range data { |
| 70 | sum += float64(h) |
| 71 | sum_sqr += float64(h) * float64(h) |
| 72 | } |
| 73 | n := float64(len(data)) |
| 74 | variance := (sum_sqr - ((sum * sum) / n)) / (n - 1) |
| 75 | return mathscip-go gomod github.com/golang/go/src go1.18 math/.Sqrtscip-go gomod github.com/golang/go/src go1.18 math/Sqrt().(variance) |
| 76 | } |
| 77 | |
| 78 | func TestCounterEncStreamHistogramscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/TestCounterEncStreamHistogram().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 79 | k, err := rsascip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/.GenerateKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/GenerateKey().(&RandSeedReaderscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/RandSeedReader#{*randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.Newscip-go gomod github.com/golang/go/src go1.18 `math/rand`/New().(randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.NewSourcescip-go gomod github.com/golang/go/src go1.18 `math/rand`/NewSource().(0xFF43109))}, 128) |
| 80 | fatalOnErrscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/fatalOnErr().(err, "rsa.GenerateKey", t) |
| 81 | c, err := goproxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/.NewCounterEncryptorRandFromKeyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/NewCounterEncryptorRandFromKey().(k, []byte("the quick brown fox run over the lazy dog")) |
| 82 | fatalOnErrscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/fatalOnErr().(err, "NewCounterEncryptorRandFromKey", t) |
| 83 | nout := 100 * 1000 |
| 84 | out := make([]byte, nout) |
| 85 | ioscip-go gomod github.com/golang/go/src go1.18 io/.ReadFullscip-go gomod github.com/golang/go/src go1.18 io/ReadFull().(&c, out) |
| 86 | refhist := make([]int, 512) |
| 87 | for i := 0; i < nout; i++ { |
| 88 | refhist[randscip-go gomod github.com/golang/go/src go1.18 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.18 `math/rand`/Intn().(256)]++ |
| 89 | } |
| 90 | hist := make([]int, 512) |
| 91 | for _, b := range out { |
| 92 | hist[int(b)]++ |
| 93 | } |
| 94 | refstddev, stddev := stddevscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/stddev().(refhist), stddevscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy_test`/stddev().(hist) |
| 95 | // due to lack of time, I guestimate |
| 96 | t.Logfscip-go gomod github.com/golang/go/src go1.18 testing/common#Logf().("ref:%v - act:%v = %v", refstddev, stddev, mathscip-go gomod github.com/golang/go/src go1.18 math/.Absscip-go gomod github.com/golang/go/src go1.18 math/Abs().(refstddev-stddev)) |
| 97 | if mathscip-go gomod github.com/golang/go/src go1.18 math/.Absscip-go gomod github.com/golang/go/src go1.18 math/Abs().(refstddev-stddev) >= 1 { |
| 98 | t.Errorfscip-go gomod github.com/golang/go/src go1.18 testing/common#Errorf().("stddev of ref histogram different than regular PRNG: %v %v", refstddev, stddev) |
| 99 | } |
| 100 | } |