| 1 |
// Copyright 2018 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package randutil contains internal randomness utilities for various |
| 6 |
// crypto packages. |
| 7 |
package randutil |
| 8 |
|
| 9 |
import ( |
| 10 |
"io" |
| 11 |
"math/rand/v2" |
| 12 |
) |
| 13 |
|
| 14 |
// MaybeReadByte reads a single byte from r with 50% probability. This is used |
| 15 |
// to ensure that callers do not depend on non-guaranteed behaviour, e.g. |
| 16 |
// assuming that rsa.GenerateKey is deterministic w.r.t. a given random stream. |
| 17 |
// |
| 18 |
// This does not affect tests that pass a stream of fixed bytes as the random |
| 19 |
// source (e.g. a zeroReader). |
| 20 |
func MaybeReadByte(r io.Reader) { |
| 21 |
if rand.Uint64()&1 == 1 { |
| 22 |
return |
| 23 |
} |
| 24 |
var buf [1]byte |
| 25 |
r.Read(buf[:]) |
| 26 |
} |