about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/access.go25
-rw-r--r--src/http.go2
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