github.com/greenpau/go-identity@v1.1.6/uuid.go (about) 1 // Copyright 2020 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package identity 16 17 import ( 18 "github.com/satori/go.uuid" 19 "math/rand" 20 "strings" 21 ) 22 23 // NewID returns a random ID to be used for user identification. 24 func NewID() string { 25 return uuid.NewV4().String() 26 } 27 28 // NewRandomString returns a random string. 29 func NewRandomString(length int) string { 30 chars := []rune("abcdefghijklmnopqrstuvwxyz0123456789") 31 if length == 0 { 32 length = 32 33 } 34 var b strings.Builder 35 for i := 0; i < length; i++ { 36 b.WriteRune(chars[rand.Intn(len(chars))]) 37 } 38 return b.String() 39 }