From f331ae701b2dc7f78a14545b49db91e9436c6208 Mon Sep 17 00:00:00 2001 From: Emile Date: Wed, 9 Oct 2019 11:15:21 +0200 Subject: generated a random access code xkcd 221 doesn't seem great in production --- src/access.go | 25 ++++++++++++++++++++++--- src/http.go | 2 +- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/access.go b/src/access.go index e43d5aa..6de2c05 100644 --- a/src/access.go +++ b/src/access.go @@ -1,7 +1,26 @@ package main -import "fmt" +import ( + "math/rand" + "strings" + "time" +) -func newAccessCode() string { - return fmt.Sprintf("%d", 4) +func newAccessCode(length int) string { + // seed the random numbergenerator + rand.Seed(time.Now().UnixNano()) + + // define the alphabet + chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + "abcdefghijklmnopqrstuvwxyz" + + "0123456789") + + // build the accesscode + var b strings.Builder + for i := 0; i < length; i++ { + b.WriteRune(chars[rand.Intn(len(chars))]) + } + str := b.String() + + return str } diff --git a/src/http.go b/src/http.go index 2c15fc6..5263062 100644 --- a/src/http.go +++ b/src/http.go @@ -69,7 +69,7 @@ func registerPostHandler(w http.ResponseWriter, r *http.Request) { usernames = append(usernames, username) // generate a new accesscode - accesscode := newAccessCode() + accesscode := newAccessCode(16) log.Printf("Generated a new AccessCode for user \"%s\": \"%s\"", username, accesscode) // generate a new companion -- cgit 1.4.1