github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/util/random.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package util 7 8 import ( 9 "math/rand" 10 "time" 11 ) 12 13 func GenerateRandomString() string { 14 rand.Seed(time.Now().UnixNano()) 15 16 result := make([]byte, randomLength) 17 sourceLen := len(sourceString) 18 19 for i := 0; i < randomLength; i++ { 20 randomIndex := rand.Intn(sourceLen) 21 result[i] = sourceString[randomIndex] 22 } 23 24 return string(result) 25 } 26 27 func GenerateRandomStringWithLength(length int) string { 28 rand.Seed(time.Now().UnixNano()) 29 30 result := make([]byte, length) 31 sourceLen := len(sourceString) 32 33 for i := 0; i < length; i++ { 34 randomIndex := rand.Intn(sourceLen) 35 result[i] = sourceString[randomIndex] 36 } 37 38 return string(result) 39 } 40 41 const ( 42 sourceString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 43 randomLength = 64 44 )