mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-01 21:52:02 +08:00
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
/*
|
|
* Teleport
|
|
* Copyright (C) 2023 Gravitational, Inc.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package utils
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"math/big"
|
|
"time"
|
|
|
|
"github.com/gravitational/trace"
|
|
)
|
|
|
|
// CryptoRandomHex returns a hex-encoded random string generated
|
|
// with a crypto-strong pseudo-random generator. The length parameter
|
|
// controls how many random bytes are generated, and the returned
|
|
// hex string will be twice the length. An error is returned when
|
|
// fewer bytes were generated than length.
|
|
func CryptoRandomHex(length int) (string, error) {
|
|
randomBytes := make([]byte, length)
|
|
if _, err := rand.Read(randomBytes); err != nil {
|
|
return "", trace.Wrap(err)
|
|
}
|
|
return hex.EncodeToString(randomBytes), nil
|
|
}
|
|
|
|
// RandomDuration returns a duration in a range [0, max)
|
|
func RandomDuration(max time.Duration) time.Duration {
|
|
randomVal, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
|
if err != nil {
|
|
return max / 2
|
|
}
|
|
return time.Duration(randomVal.Int64())
|
|
}
|