Tisamu
4/27/2018 - 2:26 PM

Generate Token

package main

import (
	"crypto/rand"
	"encoding/base64"
	"fmt"
)

func main() {
	for i := 1; i <= 10; i++ {
		token := GenerateRandomString(64)

		fmt.Println("Token : ", i, token)
	}
}

// GenerateRandomBytes func
func GenerateRandomBytes(n int) ([]byte, error) {

	b := make([]byte, n)
	_, err := rand.Read(b)
	if err != nil {
		return nil, err
	}

	return b, nil
}

//GenerateRandomString func
func GenerateRandomString(s int) string {
	b, _ := GenerateRandomBytes(s)
	return base64.URLEncoding.EncodeToString(b)
}