<commit_before>package gonanoid

import (
	"time"
	"math/rand"
)

const (
	defaultAlphabet = "_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // URL friendly alphabet
	defaultSize = 22 // default size of Nanoid
	defaultBits = 6 // default bits needed to index default alphabet
	defaultMask = 1 << defaultBits - 1 // default mask for given alphabet
	defaultMax = 63/defaultBits // default number of values available from 63 random bits
)

var (
	src = rand.NewSource(time.Now().UnixNano()) // Source of randomness
	alphabet = defaultAlphabet // alphabet
	size = defaultSize // id size
	bits uint64 = defaultBits // bits needed to represent index in alphabet
	mask int64 = defaultMask // mask
	max uint64 = defaultMax // available values from 63 random bits
)

// Set gonanoid alphabet
func SetAlphabet(newAlphabet string){
	alphabet = newAlphabet
	bits = computeBits(len(alphabet))
	mask = 1 << bits - 1
	max = 63/bits
}

// Set generated ids size
func SetSize(newSize int){
	size = newSize
}

// Generates new ID 22 characters by default
func Generate() string {
	b := make([]byte, size)
	for i, cache, remain := size-1, src.Int63(), max; i >= 0; {
		if remain == 0 {
			cache, remain = src.Int63(), max
		}
		if idx := int(cache & mask); idx < len(alphabet) {
			b[i] = alphabet[idx]
			i--
		}
		cache >>= bits
		remain--
	}

	return string(b)
}

// compute bits needed to represent index in array of given size
func computeBits(size int) uint64{
	size--
	var bits uint64 = 0
	for ;size != 0;size >>= 1 {
		bits++;
	}
	return bits
}<commit_msg>switched to crypto<commit_after>package gonanoid

import (
	"crypto/rand"
	"log"
)

const (
	defaultAlphabet = "_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // URL friendly alphabet
	defaultSize = 22 // default size of Nanoid
	defaultBits = 6 // default bits needed to index default alphabet
	defaultMask = 1 << defaultBits - 1 // default mask for given alphabet
	defaultMax = 63/defaultBits // default number of values available from 63 random bits
)

var (
	alphabet = defaultAlphabet // alphabet
	size = defaultSize // id size
	bits uint64 = defaultBits // bits needed to represent index in alphabet
	mask byte = defaultMask // mask
	max uint64 = defaultMax // available values from 63 random bits
)

// Set gonanoid alphabet
func SetAlphabet(newAlphabet string){
	alphabet = newAlphabet
	bits = computeBits(len(alphabet))
	mask = 1 << bits - 1
	max = 63/bits
}

// Set generated ids size
func SetSize(newSize int){
	size = newSize
}

// compute bits needed to represent index in array of given size
func computeBits(size int) uint64{
	size--
	var bits uint64 = 0
	for ;size != 0;size >>= 1 {
		bits++;
	}
	return bits
}

func Generate() string {
	result := make([]byte, size)
	bufferSize := int(float64(size)*1.3)
	for i, j, randomBytes := 0, 0, []byte{}; i < size; j++ {
		if j%bufferSize == 0 {
			randomBytes = secureRandomBytes(bufferSize)
		}
		if idx := int(randomBytes[j%size] & mask); idx < len(alphabet) {
			result[i] = alphabet[idx]
			i++
		}
	}

	return string(result)
}

// SecureRandomBytes returns the requested number of bytes using crypto/rand
func secureRandomBytes(length int) []byte {
	var randomBytes = make([]byte, length)
	_, err := rand.Read(randomBytes)
	if err != nil {
		log.Fatal("Unable to generate random bytes")
	}
	return randomBytes
}
{'fraction_non_alphanumeric': 0.10932380101280906, 'fraction_numerical': 0.02680965147453083, 'mean_word_length': 3.689944134078212, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2035908', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1076, 'n_words': 371}