mirror of
https://github.com/drakkan/sftpgo.git
synced 2024-11-25 00:50:31 +00:00
remove fallback if rand.Reader fails
Failing to read from rand.Reader essentially can't happen, and if it does is not possible to fallback securely, so just panic Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
parent
f13eab1caf
commit
f363d037a7
1 changed files with 4 additions and 11 deletions
|
@ -48,7 +48,6 @@ import (
|
|||
|
||||
"github.com/google/uuid"
|
||||
"github.com/lithammer/shortuuid/v3"
|
||||
"github.com/rs/xid"
|
||||
"golang.org/x/crypto/ssh"
|
||||
|
||||
"github.com/drakkan/sftpgo/v2/internal/logger"
|
||||
|
@ -576,23 +575,17 @@ func createDirPathIfMissing(file string, perm os.FileMode) error {
|
|||
func GenerateRandomBytes(length int) []byte {
|
||||
b := make([]byte, length)
|
||||
_, err := io.ReadFull(rand.Reader, b)
|
||||
if err == nil {
|
||||
return b
|
||||
if err != nil {
|
||||
PanicOnError(fmt.Errorf("failed to read random data (see https://go.dev/issue/66821): %w", err))
|
||||
}
|
||||
|
||||
b = xid.New().Bytes()
|
||||
for len(b) < length {
|
||||
b = append(b, xid.New().Bytes()...)
|
||||
}
|
||||
|
||||
return b[:length]
|
||||
return b
|
||||
}
|
||||
|
||||
// GenerateUniqueID retuens an unique ID
|
||||
func GenerateUniqueID() string {
|
||||
u, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return xid.New().String()
|
||||
PanicOnError(fmt.Errorf("failed to read random data (see https://go.dev/issue/66821): %w", err))
|
||||
}
|
||||
return shortuuid.DefaultEncoder.Encode(u)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue