mirror of
https://github.com/goharbor/harbor
synced 2024-09-21 10:27:46 +00:00
replace math/rand with crypto/rand
This commit is contained in:
parent
f1d8310ed8
commit
da19700a1e
|
@ -15,8 +15,8 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -65,11 +65,15 @@ func ParseRepository(repository string) (project, rest string) {
|
||||||
// GenerateRandomString generates a random string
|
// GenerateRandomString generates a random string
|
||||||
func GenerateRandomString() string {
|
func GenerateRandomString() string {
|
||||||
length := 32
|
length := 32
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
|
||||||
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
|
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
l := len(chars)
|
||||||
result := make([]byte, length)
|
result := make([]byte, length)
|
||||||
|
_, err := rand.Read(result)
|
||||||
|
if err != nil {
|
||||||
|
log.Warningf("Error reading random bytes: %v", err)
|
||||||
|
}
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
result[i] = chars[rand.Intn(len(chars))]
|
result[i] = chars[int(result[i])%l]
|
||||||
}
|
}
|
||||||
return string(result)
|
return string(result)
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,6 +140,10 @@ func TestGenerateRandomString(t *testing.T) {
|
||||||
if len(str) != 32 {
|
if len(str) != 32 {
|
||||||
t.Errorf("unexpected length: %d != %d", len(str), 32)
|
t.Errorf("unexpected length: %d != %d", len(str), 32)
|
||||||
}
|
}
|
||||||
|
str2 := GenerateRandomString()
|
||||||
|
if str2 == str {
|
||||||
|
t.Errorf("Two identical random strings in a row: %s", str)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseLink(t *testing.T) {
|
func TestParseLink(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user