dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit e5b23d81f4dbcc2c33f6ac3072646940bbbdbf82
parent cf2e642848f5131522bcfe4eea6b66a88081c513
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue, 12 Dec 2023 18:49:11 -0500

use crypto rand for deck shuffle

Diffstat:
Mpkg/utils/utils.go | 19+++++++++++++++++++
Mpkg/web/handlers/poker/poker.go | 4+++-
2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go @@ -596,6 +596,10 @@ func Shuffle[T any](s []T) { rand.Shuffle(len(s), func(i, j int) { s[i], s[j] = s[j], s[i] }) } +func Shuffle1[T any](r *rand.Rand, s []T) { + r.Shuffle(len(s), func(i, j int) { s[i], s[j] = s[j], s[i] }) +} + type Ints interface { int | int64 } @@ -1156,3 +1160,18 @@ func Slice2Set[T any, U comparable](s []T, f func(T) U) *hashset.HashSet[U] { } return h } + +type CryptoRandSource struct{} + +func NewCryptoRandSource() CryptoRandSource { + return CryptoRandSource{} +} + +func (_ CryptoRandSource) Int63() int64 { + var b [8]byte + _, _ = cryptoRand.Read(b[:]) + // mask off sign bit to ensure positive number + return int64(binary.LittleEndian.Uint64(b[:]) & (1<<63 - 1)) +} + +func (_ CryptoRandSource) Seed(_ int64) {} diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -10,6 +10,7 @@ import ( "github.com/chehsunliu/poker" "github.com/labstack/echo" "github.com/sirupsen/logrus" + "math/rand" "net/http" "sort" "strconv" @@ -424,7 +425,8 @@ func NewOngoing(g *PokerGame) *Ongoing { "A♣", "2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "Q♣", "K♣", "A♦", "2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "Q♦", "K♦", } - utils.Shuffle(deck) + r := rand.New(utils.NewCryptoRandSource()) + utils.Shuffle1(r, deck) players := make([]*PokerPlayer, 0) g.PlayersMtx.RLock()