Skip to content

Commit 0b5e0e9

Browse files
authored
chore: use math/rand/v2 instead of math/rand (#363)
* use math/rand/v2 instead of math/rand * fill the contents by manually encoding pseudo-random numbers * fix variable declaration
1 parent d2a7286 commit 0b5e0e9

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package certmagic
1616

1717
import (
1818
"fmt"
19-
weakrand "math/rand"
19+
weakrand "math/rand/v2"
2020
"strings"
2121
"sync"
2222
"time"
@@ -244,7 +244,7 @@ func (certCache *Cache) unsyncedCacheCertificate(cert Certificate) {
244244
// map with less code, that is a heavily skewed eviction
245245
// strategy; generating random numbers is cheap and
246246
// ensures a much better distribution.
247-
rnd := weakrand.Intn(cacheSize)
247+
rnd := weakrand.IntN(cacheSize)
248248
i := 0
249249
for _, randomCert := range certCache.cache {
250250
if i >= rnd && randomCert.managed { // don't evict manually-loaded certs

certificates.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24-
"math/rand"
24+
"math/rand/v2"
2525
"net"
2626
"os"
2727
"strings"
@@ -128,7 +128,7 @@ func (cfg *Config) certNeedsRenewal(leaf *x509.Certificate, ari acme.RenewalInfo
128128
if selectedTime.IsZero() &&
129129
(!ari.SuggestedWindow.Start.IsZero() && !ari.SuggestedWindow.End.IsZero()) {
130130
start, end := ari.SuggestedWindow.Start.Unix()+1, ari.SuggestedWindow.End.Unix()
131-
selectedTime = time.Unix(rand.Int63n(end-start)+start, 0).UTC()
131+
selectedTime = time.Unix(rand.Int64N(end-start)+start, 0).UTC()
132132
logger.Warn("no renewal time had been selected with ARI; chose an ephemeral one for now",
133133
zap.Time("ephemeral_selected_time", selectedTime))
134134
}

config.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"errors"
2929
"fmt"
3030
"io/fs"
31-
weakrand "math/rand"
31+
weakrand "math/rand/v2"
3232
"net"
3333
"net/http"
3434
"net/url"
@@ -1236,11 +1236,20 @@ func (cfg *Config) checkStorage(ctx context.Context) error {
12361236
}
12371237
key := fmt.Sprintf("rw_test_%d", weakrand.Int())
12381238
contents := make([]byte, 1024*10) // size sufficient for one or two ACME resources
1239-
_, err := weakrand.Read(contents)
1240-
if err != nil {
1241-
return err
1242-
}
1243-
err = cfg.Storage.Store(ctx, key, contents)
1239+
// This is how ChaCha8.Read works, without handling the case where the slice length is not a multiple of 8.
1240+
// This also avoids the use of a mutex and an import.
1241+
for i := 0; i < len(contents); i += 8 {
1242+
v := weakrand.Uint64()
1243+
contents[i] = byte(v)
1244+
contents[i+1] = byte(v >> 8)
1245+
contents[i+2] = byte(v >> 16)
1246+
contents[i+3] = byte(v >> 24)
1247+
contents[i+4] = byte(v >> 32)
1248+
contents[i+5] = byte(v >> 40)
1249+
contents[i+6] = byte(v >> 48)
1250+
contents[i+7] = byte(v >> 56)
1251+
}
1252+
err := cfg.Storage.Store(ctx, key, contents)
12441253
if err != nil {
12451254
return err
12461255
}

0 commit comments

Comments
 (0)