tordam

A library for peer discovery inside the Tor network
git clone https://git.parazyd.org/tordam
Log | Files | Refs | README | LICENSE

commit 03e196797d988717545e5a571aed71e905907f8b
parent 1b15e4f26602d70ead07f116dba431c5a7087a6f
Author: parazyd <parazyd@dyne.org>
Date:   Thu,  7 Dec 2017 19:14:14 +0100

Implement generation of random ASCII strings

Diffstat:
Mgo/lib/helpers.go | 19+++++++++++++++++++
1 file changed, 19 insertions(+), 0 deletions(-)

diff --git a/go/lib/helpers.go b/go/lib/helpers.go @@ -4,7 +4,9 @@ package lib import ( "bytes" + "crypto/rand" "log" + "math/big" "net/http" "net/url" "os/exec" @@ -120,3 +122,20 @@ func HTTPPost(host string, data []byte) *http.Response { return resp } + +// GenRandomASCII returns a random ASCII string of a given length. +func GenRandomASCII(length int) (string, error) { + var res string + for { + if len(res) >= length { + return res, nil + } + num, err := rand.Int(rand.Reader, big.NewInt(int64(127))) + CheckError(err) + + n := num.Int64() + if n > 32 && n < 127 { + res += string(n) + } + } +}