Skip to content

Commit

Permalink
🔄 Sync from monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
mojo-machine[bot] committed Feb 21, 2024
1 parent b04ea36 commit 5f12fb8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions lib/cryptorand/cryptorand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package cryptorand
import (
"crypto/rand"
"encoding/binary"
mathrand "math/rand"

mathrand "math/rand/v2"
)

// TODO: could this be removed entirely now that we have math/rand/v2?
// the docs do still say "it should not be used for security-sensitive work"

func New() *mathrand.Rand {
//nolint:gosec // this is incorrect - it is using crypto/rand
return mathrand.New(NewSource())
}

Expand All @@ -19,12 +22,11 @@ func NewSource() mathrand.Source {

func (source) Seed(_ int64) {}

func (source) Int63() int64 {
func (source) Uint64() uint64 {
var data [8]byte
if _, err := rand.Read(data[:]); err != nil {
panic(err)
}

// mask off sign bit to ensure positive number
return int64(binary.LittleEndian.Uint64(data[:]) & (1<<63 - 1))
return binary.LittleEndian.Uint64(data[:])
}
8 changes: 4 additions & 4 deletions lib/ksuid/instance_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package ksuid
import (
"bytes"
"context"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"net"
"os"

"github.com/wearemojo/mojo-public-go/lib/cryptorand"
"github.com/wearemojo/mojo-public-go/lib/merr"
)

var random = cryptorand.New()

const (
ErrNoHardwareAddress = merr.Code("no_hardware_address")
ErrNotDockerized = merr.Code("not_dockerized")
Expand Down Expand Up @@ -102,7 +100,9 @@ func getDockerID(ctx context.Context) ([]byte, error) {
// NewRandomID returns a RandomID initialized by a PRNG.
func NewRandomID() InstanceID {
tmp := make([]byte, 8)
random.Read(tmp)
if _, err := rand.Read(tmp); err != nil {
panic(err)
}

var b [8]byte
copy(b[:], tmp)
Expand Down

0 comments on commit 5f12fb8

Please sign in to comment.