mirror of
https://codeberg.org/cunicu/cunicu.git
synced 2025-09-26 21:01:14 +08:00
fix(deps): Internalize pion/randutil
Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user