fix(deps): Internalize pion/randutil

Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
Steffen Vogel
2024-12-21 21:44:11 +01:00
parent fdec251167
commit 4aea08bdaf
4 changed files with 34 additions and 7 deletions

2
go.mod
View File

@@ -27,7 +27,6 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/pion/ice/v4 v4.0.3
github.com/pion/logging v0.2.2
github.com/pion/randutil v0.1.0
github.com/pion/stun/v3 v3.0.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
@@ -77,6 +76,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pion/dtls/v3 v3.0.4 // indirect
github.com/pion/mdns/v2 v2.0.7 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/transport/v3 v3.0.7 // indirect
github.com/pion/turn/v4 v4.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect

View File

@@ -4,7 +4,10 @@
// Package crypto implements basic crypto primitives used in the project
package crypto
import "crypto/rand"
import (
"crypto/rand"
"math/big"
)
func GetNonce(length int) (Nonce, error) {
nonce := make(Nonce, length)
@@ -16,3 +19,17 @@ func GetNonce(length int) (Nonce, error) {
return nonce, nil
}
// GetRandomString generates a random string for cryptographic usage.
func GetRandomString(n int, runes string) (string, error) {
letters := []rune(runes)
b := make([]rune, n)
for i := range b {
v, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
return "", err
}
b[i] = letters[v.Int64()]
}
return string(b), nil
}

View File

@@ -7,6 +7,7 @@ import (
"testing"
"cunicu.li/cunicu/pkg/crypto"
"cunicu.li/cunicu/pkg/tty"
"cunicu.li/cunicu/test"
. "github.com/onsi/ginkgo/v2"
@@ -19,11 +20,20 @@ func TestSuite(t *testing.T) {
RunSpecs(t, "Crypto Suite")
}
var _ = Describe("nonce", func() {
It("can generate a valid nonce", func() {
var _ = Describe("generate", func() {
It("nonce", func() {
nonce, err := crypto.GetNonce(100)
Expect(err).To(Succeed())
Expect(nonce).To(HaveLen(100))
Expect([]byte(nonce)).To(test.BeRandom())
})
It("random string", func() {
for i := 0; i < 10000; i++ {
s, err := crypto.GetRandomString(10, tty.RunesAlpha)
Expect(err).To(Succeed())
Expect(s).To(HaveLen(10))
Expect(s).To(MatchRegexp(`^[a-zA-Z]+$`), "Generator returned unexpected character")
}
})
})

View File

@@ -10,8 +10,8 @@ import (
"strings"
"github.com/pion/ice/v4"
"github.com/pion/randutil"
"cunicu.li/cunicu/pkg/crypto"
"cunicu.li/cunicu/pkg/log"
"cunicu.li/cunicu/pkg/tty"
)
@@ -43,12 +43,12 @@ func NewConnectionState(cs ice.ConnectionState) ConnectionState {
}
func NewCredentials() *Credentials {
ufrag, err := randutil.GenerateCryptoRandomString(lenUFrag, tty.RunesAlpha)
ufrag, err := crypto.GetRandomString(lenUFrag, tty.RunesAlpha)
if err != nil {
panic(err)
}
pwd, err := randutil.GenerateCryptoRandomString(lenPwd, tty.RunesAlpha)
pwd, err := crypto.GetRandomString(lenPwd, tty.RunesAlpha)
if err != nil {
panic(err)
}