Skip to content

Instantly share code, notes, and snippets.

@valer-cara
Created November 4, 2018 14:53
Show Gist options
  • Save valer-cara/76ff251620c5ff88b287bc79055af290 to your computer and use it in GitHub Desktop.
Save valer-cara/76ff251620c5ff88b287bc79055af290 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"io"
"math/big"
)
type BadRand struct {
reader io.Reader
expectedTotalBytes uint
}
func NewBadRand(out int64, outputLen uint) *BadRand {
x := big.NewInt(out)
y := make([]byte, outputLen)
tail := x.Bytes()
for i, j := len(tail)-1, len(y)-1; i >= 0 && j > 0; i, j = i-1, j-1 {
y[j] = tail[i]
}
return &BadRand{
reader: bytes.NewReader(y),
expectedTotalBytes: outputLen,
}
}
func (b *BadRand) Read(p []byte) (n int, err error) {
return b.reader.Read(p)
}
func main() {
fmt.Println("Hello, playground")
//x := NewBadRand(1)
//buf := make([]byte, 100)
//n, err := io.ReadFull(x, buf)
//if err != nil {
// fmt.Println("BAD!!")
//}
//fmt.Printf("Read %d bytes\n", n)
x := NewBadRand(123, 40)
out := make([]byte, 100)
n, err := io.ReadFull(x, out)
if err != nil {
fmt.Println("BAD READ!")
}
fmt.Printf("%v bytes read\n", n)
fmt.Printf("%v\n", out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment