2014-12-07 08:41:24 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-12-07 08:41:24 -07:00
|
|
|
|
2016-05-26 00:02:56 -07:00
|
|
|
// Package rand implements functions similar to math/rand in the standard
|
|
|
|
// library, but on top of a secure random number generator.
|
|
|
|
package rand
|
2014-12-07 08:41:24 -07:00
|
|
|
|
|
|
|
import (
|
2021-03-02 11:17:20 -07:00
|
|
|
"io"
|
2014-12-07 08:41:24 -07:00
|
|
|
mathRand "math/rand"
|
2019-05-29 00:56:40 -07:00
|
|
|
"reflect"
|
2021-09-26 03:15:39 -07:00
|
|
|
"strings"
|
2014-12-07 08:41:24 -07:00
|
|
|
)
|
|
|
|
|
2021-03-02 11:17:20 -07:00
|
|
|
// Reader is the standard crypto/rand.Reader with added buffering.
|
|
|
|
var Reader = defaultSecureSource
|
|
|
|
|
|
|
|
func Read(p []byte) (int, error) {
|
|
|
|
return io.ReadFull(defaultSecureSource, p)
|
|
|
|
}
|
2016-05-26 00:02:56 -07:00
|
|
|
|
2020-03-30 14:26:28 -07:00
|
|
|
// randomCharset contains the characters that can make up a rand.String().
|
2016-05-09 02:43:40 -07:00
|
|
|
const randomCharset = "2345679abcdefghijkmnopqrstuvwxyzACDEFGHJKLMNPQRSTUVWXYZ"
|
2014-12-07 08:41:24 -07:00
|
|
|
|
2016-05-24 23:38:38 -07:00
|
|
|
var (
|
2020-06-17 01:43:58 -07:00
|
|
|
// defaultSecureSource is a concurrency-safe, cryptographically secure
|
|
|
|
// math/rand.Source.
|
2020-03-30 14:26:28 -07:00
|
|
|
defaultSecureSource = newSecureSource()
|
2016-05-24 23:38:38 -07:00
|
|
|
|
|
|
|
// defaultSecureRand is a math/rand.Rand based on the secure source.
|
2020-03-30 14:26:28 -07:00
|
|
|
defaultSecureRand = mathRand.New(defaultSecureSource)
|
2016-05-24 23:38:38 -07:00
|
|
|
)
|
|
|
|
|
2020-06-17 01:43:58 -07:00
|
|
|
// String returns a cryptographically secure random string of characters
|
|
|
|
// (taken from randomCharset) of the specified length. The returned string
|
|
|
|
// contains ~5.8 bits of entropy per character, due to the character set used.
|
2016-05-26 00:02:56 -07:00
|
|
|
func String(l int) string {
|
2021-09-26 03:15:39 -07:00
|
|
|
var sb strings.Builder
|
|
|
|
sb.Grow(l)
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
sb.WriteByte(randomCharset[defaultSecureRand.Intn(len(randomCharset))])
|
2014-12-07 08:41:24 -07:00
|
|
|
}
|
2021-09-26 03:15:39 -07:00
|
|
|
return sb.String()
|
2014-12-07 08:41:24 -07:00
|
|
|
}
|
|
|
|
|
2020-06-17 01:43:58 -07:00
|
|
|
// Int63 returns a cryptographically secure random int63.
|
2016-05-26 00:02:56 -07:00
|
|
|
func Int63() int64 {
|
2020-03-30 14:26:28 -07:00
|
|
|
return defaultSecureSource.Int63()
|
2016-05-26 00:02:56 -07:00
|
|
|
}
|
|
|
|
|
2020-06-17 01:43:58 -07:00
|
|
|
// Uint64 returns a cryptographically secure strongly random uint64.
|
|
|
|
func Uint64() uint64 {
|
|
|
|
return defaultSecureSource.Uint64()
|
2016-05-26 00:02:56 -07:00
|
|
|
}
|
|
|
|
|
2020-06-17 01:43:58 -07:00
|
|
|
// Intn returns, as an int, a cryptographically secure non-negative
|
|
|
|
// random number in [0,n). It panics if n <= 0.
|
2016-05-26 00:02:56 -07:00
|
|
|
func Intn(n int) int {
|
|
|
|
return defaultSecureRand.Intn(n)
|
2014-12-07 08:41:24 -07:00
|
|
|
}
|
|
|
|
|
2020-03-30 14:26:28 -07:00
|
|
|
// Shuffle the order of elements in slice.
|
2019-05-29 00:56:40 -07:00
|
|
|
func Shuffle(slice interface{}) {
|
|
|
|
rv := reflect.ValueOf(slice)
|
|
|
|
swap := reflect.Swapper(slice)
|
|
|
|
length := rv.Len()
|
|
|
|
if length < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defaultSecureRand.Shuffle(length, swap)
|
|
|
|
}
|