Commit 1e434ef3 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

vendor: updated random + fixed test import

There is a dummy test import: test/lib/random-dep.go
Because godep doen't yet vendor binaries nicely.
parent 4bf3e967
......@@ -111,8 +111,8 @@
"Rev": "1976046c2b0db0b668791b3e541d76a38b7c1af7"
},
{
"ImportPath": "github.com/jbenet/go-random/random",
"Rev": "623fff67b06299c43b5542d42c427c7c2855a362"
"ImportPath": "github.com/jbenet/go-random",
"Rev": "e4585173eb8c47eea36c3dbff22f26f3f94d3586"
},
{
"ImportPath": "github.com/kr/binarydist",
......
The MIT License (MIT)
Copyright (c) 2014 Juan Batiz-Benet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# go-random outputs randomness
This is a unix util that outputs randomness.
It is a thin wrapper around `crypto/rand`.
It aims to be portable (though it may not yet be).
### Install
```sh
go install github.com/jbenet/go-random/random
```
(The extra /random is there because go get is stupidly too proscriptive about
package/repository names and I don't yet know how to change the default binary
output name)
### Usage:
```
> random
Usage: random <int>
Print <int> random bytes (from Go's crypto/rand)
> random 6
2q���#
```
### License
MIT
package random
import (
"bytes"
randcrypto "crypto/rand"
"io"
randmath "math/rand"
)
func WriteRandomBytes(count int64, w io.Writer) error {
r := &io.LimitedReader{R: randcrypto.Reader, N: count}
_, err := io.Copy(w, r)
return err
}
func WritePseudoRandomBytes(count int64, w io.Writer, seed int64) error {
randmath.Seed(seed)
// Configurable buffer size
bufsize := int64(1024 * 1024 * 4)
b := make([]byte, bufsize)
for count > 0 {
if bufsize > count {
bufsize = count
b = b[:bufsize]
}
var n int64
for i := int64(0); i < bufsize; i++ {
n = randmath.Int63()
for j := 0; j < 8 && i < bufsize; j++ {
b[i] = byte(n & 0xff)
n >>= 8
i++
}
}
count = count - bufsize
r := bytes.NewReader(b)
_, err := io.Copy(w, r)
if err != nil {
return err
}
}
return nil
}
package main
import (
"bytes"
randcrypto "crypto/rand"
"fmt"
"io"
randmath "math/rand"
"os"
"strconv"
random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
)
func main() {
......@@ -22,13 +20,13 @@ func main() {
}
if l == 2 {
err = writeRandomBytes(count, os.Stdout)
err = random.WriteRandomBytes(count, os.Stdout)
} else {
seed, err2 := strconv.ParseInt(os.Args[2], 10, 64)
if err2 != nil {
usageError()
}
err = writePseudoRandomBytes(count, os.Stdout, seed)
err = random.WritePseudoRandomBytes(count, os.Stdout, seed)
}
if err != nil {
......@@ -47,42 +45,3 @@ func die(err error) {
fmt.Fprintf(os.Stderr, "Error: %v", err)
os.Exit(-1)
}
func writeRandomBytes(count int64, w io.Writer) error {
r := &io.LimitedReader{R: randcrypto.Reader, N: count}
_, err := io.Copy(w, r)
return err
}
func writePseudoRandomBytes(count int64, w io.Writer, seed int64) error {
randmath.Seed(seed)
// Configurable buffer size
bufsize := int64(1024 * 1024 * 4)
b := make([]byte, bufsize)
for count > 0 {
if bufsize > count {
bufsize = count
b = b[:bufsize]
}
var n int64
for i := int64(0); i < bufsize; i++ {
n = randmath.Int63()
for j := 0; j < 8 && i < bufsize; j++ {
b[i] = byte(n & 0xff)
n >>= 8
i++
}
}
count = count - bufsize
r := bytes.NewReader(b)
_, err := io.Copy(w, r)
if err != nil {
return err
}
}
return nil
}
package main
package random
import (
"bytes"
......@@ -18,7 +18,7 @@ func TestPseudoRandom(t *testing.T) {
for _, size := range testCases {
var buf bytes.Buffer
err := writePseudoRandomBytes(int64(size), &buf, int64(time.Now().UnixNano()))
err := WritePseudoRandomBytes(int64(size), &buf, int64(time.Now().UnixNano()))
if err != nil {
t.Fatal(err)
}
......@@ -39,11 +39,11 @@ func TestPseudoRandomSeed(t *testing.T) {
var bufs bytes.Buffer
var bufr bytes.Buffer
if err := writePseudoRandomBytes(size, &bufs, seed); err != nil {
if err := WritePseudoRandomBytes(size, &bufs, seed); err != nil {
t.Fatal(err)
}
if err := writePseudoRandomBytes(size, &bufr, time.Now().UnixNano()); err != nil {
if err := WritePseudoRandomBytes(size, &bufr, time.Now().UnixNano()); err != nil {
t.Fatal(err)
}
......@@ -76,7 +76,7 @@ func TestCryptoRandom(t *testing.T) {
for _, size := range testCases {
var buf bytes.Buffer
err := writeRandomBytes(int64(size), &buf)
err := WriteRandomBytes(int64(size), &buf)
if err != nil {
t.Fatal(err)
}
......@@ -88,9 +88,9 @@ func TestCryptoRandom(t *testing.T) {
}
func BenchmarkCryptoRandom(b *testing.B) {
writeRandomBytes(int64(b.N), ioutil.Discard)
WriteRandomBytes(int64(b.N), ioutil.Discard)
}
func BenchmarkPseudoRandom(b *testing.B) {
writePseudoRandomBytes(int64(b.N), ioutil.Discard, time.Now().UnixNano())
WritePseudoRandomBytes(int64(b.N), ioutil.Discard, time.Now().UnixNano())
}
......@@ -4,7 +4,5 @@
package randomdep
import (
random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random/random"
_ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
)
var _ = random
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment