mirror of
https://github.com/lejianwen/rustdesk-api.git
synced 2026-02-17 14:04:51 +08:00
feat: Use crypto/rand for secure random string generation (#293)
This commit is contained in:
@@ -2,9 +2,9 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
crand "crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -69,8 +69,12 @@ func RandomString(n int) string {
|
|||||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
length := len(letterBytes)
|
length := len(letterBytes)
|
||||||
b := make([]byte, n)
|
b := make([]byte, n)
|
||||||
for i := range b {
|
randomBytes := make([]byte, n)
|
||||||
b[i] = letterBytes[rand.Intn(length)]
|
if _, err := crand.Read(randomBytes); err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for i, rb := range randomBytes {
|
||||||
|
b[i] = letterBytes[int(rb)%length]
|
||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user