package main

import (
	"crypto/rand"
	"encoding/base32"
	"log"
	"net/http"

	"m.cluseau.fr/go/httperr"
)

func newToken(sizeInBytes int) (token string, err error) {
	randBytes := make([]byte, sizeInBytes)

	_, err = rand.Read(randBytes)
	if err != nil {
		log.Print("rand read error: ", err)
		err = httperr.New(http.StatusInternalServerError, err)
		return
	}

	token = base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(randBytes)
	return
}
