1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-23 05:35:58 +08:00

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.
This commit is contained in:
Juan Batiz-Benet
2014-11-08 20:01:38 -08:00
parent 4bf3e967d8
commit 1e434ef3c4
8 changed files with 112 additions and 57 deletions

4
Godeps/Godeps.json generated
View File

@ -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",

View File

@ -0,0 +1,21 @@
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.

View File

@ -0,0 +1,29 @@
# 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<EFBFBD><EFBFBD><EFBFBD>#
```
### License
MIT

View File

@ -0,0 +1,47 @@
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
}

View File

@ -0,0 +1 @@
random

View File

@ -1,13 +1,11 @@
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
}

View File

@ -1,4 +1,4 @@
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())
}

View File

@ -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