All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vendor.github.com.pion.randutil.crypto.go Maven / Gradle / Ivy

There is a newer version: 2.9.1
Show newest version
package randutil

import (
	crand "crypto/rand"
	"encoding/binary"
	"math/big"
)

// GenerateCryptoRandomString generates a random string for cryptographic usage.
func GenerateCryptoRandomString(n int, runes string) (string, error) {
	letters := []rune(runes)
	b := make([]rune, n)
	for i := range b {
		v, err := crand.Int(crand.Reader, big.NewInt(int64(len(letters))))
		if err != nil {
			return "", err
		}
		b[i] = letters[v.Int64()]
	}
	return string(b), nil
}

// CryptoUint64 returns cryptographic random uint64.
func CryptoUint64() (uint64, error) {
	var v uint64
	if err := binary.Read(crand.Reader, binary.LittleEndian, &v); err != nil {
		return 0, err
	}
	return v, nil
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy