tImplement generation of random ASCII strings - tordam - A library for peer discovery inside the Tor network (HTM) git clone https://git.parazyd.org/tordam (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 03e196797d988717545e5a571aed71e905907f8b (DIR) parent 1b15e4f26602d70ead07f116dba431c5a7087a6f (HTM) Author: parazyd <parazyd@dyne.org> Date: Thu, 7 Dec 2017 19:14:14 +0100 Implement generation of random ASCII strings Diffstat: M go/lib/helpers.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+), 0 deletions(-) --- (DIR) diff --git a/go/lib/helpers.go b/go/lib/helpers.go t@@ -4,7 +4,9 @@ package lib import ( "bytes" + "crypto/rand" "log" + "math/big" "net/http" "net/url" "os/exec" t@@ -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) + } + } +}