package password_generator

import (
	"crypto/rand"
	"math/big"
)

const (
	charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)

type generator struct {
	length int
}

func NewGenerator(length int) IPasswordGenerator {
	return &generator{
		length: length,
	}
}

func (g *generator) Generate() (string, error) {
	password := make([]byte, g.length)
	for i := range password {
		num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
		if err != nil {
			return "", err
		}
		password[i] = charset[num.Int64()]
	}
	return string(password), nil
}
