mirror of
https://codeberg.org/cunicu/cunicu.git
synced 2025-09-27 05:06:02 +08:00
fix(deps): Internalize pion/randutil
Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
2
go.mod
2
go.mod
@@ -27,7 +27,6 @@ require (
|
|||||||
github.com/mitchellh/mapstructure v1.5.0
|
github.com/mitchellh/mapstructure v1.5.0
|
||||||
github.com/pion/ice/v4 v4.0.3
|
github.com/pion/ice/v4 v4.0.3
|
||||||
github.com/pion/logging v0.2.2
|
github.com/pion/logging v0.2.2
|
||||||
github.com/pion/randutil v0.1.0
|
|
||||||
github.com/pion/stun/v3 v3.0.0
|
github.com/pion/stun/v3 v3.0.0
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
@@ -77,6 +76,7 @@ require (
|
|||||||
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
||||||
github.com/pion/dtls/v3 v3.0.4 // indirect
|
github.com/pion/dtls/v3 v3.0.4 // indirect
|
||||||
github.com/pion/mdns/v2 v2.0.7 // 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/transport/v3 v3.0.7 // indirect
|
||||||
github.com/pion/turn/v4 v4.0.0 // indirect
|
github.com/pion/turn/v4 v4.0.0 // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
|
@@ -4,7 +4,10 @@
|
|||||||
// Package crypto implements basic crypto primitives used in the project
|
// Package crypto implements basic crypto primitives used in the project
|
||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
import "crypto/rand"
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
func GetNonce(length int) (Nonce, error) {
|
func GetNonce(length int) (Nonce, error) {
|
||||||
nonce := make(Nonce, length)
|
nonce := make(Nonce, length)
|
||||||
@@ -16,3 +19,17 @@ func GetNonce(length int) (Nonce, error) {
|
|||||||
|
|
||||||
return nonce, nil
|
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
|
||||||
|
}
|
||||||
|
@@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"cunicu.li/cunicu/pkg/crypto"
|
"cunicu.li/cunicu/pkg/crypto"
|
||||||
|
"cunicu.li/cunicu/pkg/tty"
|
||||||
"cunicu.li/cunicu/test"
|
"cunicu.li/cunicu/test"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
@@ -19,11 +20,20 @@ func TestSuite(t *testing.T) {
|
|||||||
RunSpecs(t, "Crypto Suite")
|
RunSpecs(t, "Crypto Suite")
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ = Describe("nonce", func() {
|
var _ = Describe("generate", func() {
|
||||||
It("can generate a valid nonce", func() {
|
It("nonce", func() {
|
||||||
nonce, err := crypto.GetNonce(100)
|
nonce, err := crypto.GetNonce(100)
|
||||||
Expect(err).To(Succeed())
|
Expect(err).To(Succeed())
|
||||||
Expect(nonce).To(HaveLen(100))
|
Expect(nonce).To(HaveLen(100))
|
||||||
Expect([]byte(nonce)).To(test.BeRandom())
|
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")
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@@ -10,8 +10,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pion/ice/v4"
|
"github.com/pion/ice/v4"
|
||||||
"github.com/pion/randutil"
|
|
||||||
|
|
||||||
|
"cunicu.li/cunicu/pkg/crypto"
|
||||||
"cunicu.li/cunicu/pkg/log"
|
"cunicu.li/cunicu/pkg/log"
|
||||||
"cunicu.li/cunicu/pkg/tty"
|
"cunicu.li/cunicu/pkg/tty"
|
||||||
)
|
)
|
||||||
@@ -43,12 +43,12 @@ func NewConnectionState(cs ice.ConnectionState) ConnectionState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewCredentials() *Credentials {
|
func NewCredentials() *Credentials {
|
||||||
ufrag, err := randutil.GenerateCryptoRandomString(lenUFrag, tty.RunesAlpha)
|
ufrag, err := crypto.GetRandomString(lenUFrag, tty.RunesAlpha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pwd, err := randutil.GenerateCryptoRandomString(lenPwd, tty.RunesAlpha)
|
pwd, err := crypto.GetRandomString(lenPwd, tty.RunesAlpha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user