2024-03-12 20:59:07 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021-2024 Winni Neessen <wn@neessen.dev>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-04-18 11:49:44 +02:00
|
|
|
package apg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2024-03-12 20:31:27 +01:00
|
|
|
"os"
|
|
|
|
"regexp"
|
2023-04-18 11:49:44 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// 7 bits to represent a letter index
|
|
|
|
letterIdxBits = 7
|
|
|
|
// All 1-bits, as many as letterIdxBits
|
|
|
|
letterIdxMask = 1<<letterIdxBits - 1
|
|
|
|
// # of letter indices fitting in 63 bits)
|
|
|
|
letterIdxMax = 63 / letterIdxBits
|
|
|
|
)
|
|
|
|
|
2024-03-07 22:50:47 +01:00
|
|
|
// maxInt32 is the maximum positive value for a int32 number type
|
|
|
|
const maxInt32 = 2147483647
|
|
|
|
|
2023-04-18 11:49:44 +02:00
|
|
|
var (
|
|
|
|
// ErrInvalidLength is returned if the provided maximum number is equal or less than zero
|
|
|
|
ErrInvalidLength = errors.New("provided length value cannot be zero or less")
|
|
|
|
// ErrLengthMismatch is returned if the number of generated bytes does not match the expected length
|
|
|
|
ErrLengthMismatch = errors.New("number of generated random bytes does not match the expected length")
|
|
|
|
// ErrInvalidCharRange is returned if the given range of characters is not valid
|
|
|
|
ErrInvalidCharRange = errors.New("provided character range is not valid or empty")
|
|
|
|
)
|
|
|
|
|
2023-08-06 18:55:47 +02:00
|
|
|
// CoinFlip performs a simple coinflip based on the rand library and returns 1 or 0
|
|
|
|
func (g *Generator) CoinFlip() int64 {
|
2024-03-08 10:22:34 +01:00
|
|
|
coinFlip, _ := g.RandNum(2)
|
|
|
|
return coinFlip
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CoinFlipBool performs a simple coinflip based on the rand library and returns true or false
|
|
|
|
func (g *Generator) CoinFlipBool() bool {
|
|
|
|
return g.CoinFlip() == 1
|
|
|
|
}
|
|
|
|
|
2023-08-05 18:10:11 +02:00
|
|
|
// Generate generates a password based on all the different config flags and returns
|
|
|
|
// it as string type. If the generation fails, an error will be thrown
|
|
|
|
func (g *Generator) Generate() (string, error) {
|
2023-08-06 18:55:47 +02:00
|
|
|
switch g.config.Algorithm {
|
2024-03-12 18:28:01 +01:00
|
|
|
case AlgoPronounceable:
|
|
|
|
return g.generatePronounceable()
|
2023-08-06 18:55:47 +02:00
|
|
|
case AlgoCoinFlip:
|
|
|
|
return g.generateCoinFlip()
|
|
|
|
case AlgoRandom:
|
|
|
|
return g.generateRandom()
|
|
|
|
case AlgoUnsupported:
|
|
|
|
return "", fmt.Errorf("unsupported algorithm")
|
2023-08-05 18:10:11 +02:00
|
|
|
}
|
2023-08-06 18:55:47 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
2023-08-05 18:10:11 +02:00
|
|
|
|
2024-03-08 17:07:55 +01:00
|
|
|
// GetCharRangeFromConfig checks the Mode from the Config and returns a
|
|
|
|
// list of all possible characters that are supported by these Mode
|
|
|
|
func (g *Generator) GetCharRangeFromConfig() string {
|
|
|
|
charRange := strings.Builder{}
|
|
|
|
if MaskHasMode(g.config.Mode, ModeLowerCase) {
|
|
|
|
switch MaskHasMode(g.config.Mode, ModeHumanReadable) {
|
|
|
|
case true:
|
|
|
|
charRange.WriteString(CharRangeAlphaLowerHuman)
|
|
|
|
default:
|
|
|
|
charRange.WriteString(CharRangeAlphaLower)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if MaskHasMode(g.config.Mode, ModeNumeric) {
|
|
|
|
switch MaskHasMode(g.config.Mode, ModeHumanReadable) {
|
|
|
|
case true:
|
|
|
|
charRange.WriteString(CharRangeNumericHuman)
|
|
|
|
default:
|
|
|
|
charRange.WriteString(CharRangeNumeric)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if MaskHasMode(g.config.Mode, ModeSpecial) {
|
|
|
|
switch MaskHasMode(g.config.Mode, ModeHumanReadable) {
|
|
|
|
case true:
|
|
|
|
charRange.WriteString(CharRangeSpecialHuman)
|
|
|
|
default:
|
|
|
|
charRange.WriteString(CharRangeSpecial)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if MaskHasMode(g.config.Mode, ModeUpperCase) {
|
|
|
|
switch MaskHasMode(g.config.Mode, ModeHumanReadable) {
|
|
|
|
case true:
|
|
|
|
charRange.WriteString(CharRangeAlphaUpperHuman)
|
|
|
|
default:
|
|
|
|
charRange.WriteString(CharRangeAlphaUpper)
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 20:31:27 +01:00
|
|
|
if g.config.ExcludeChars != "" {
|
|
|
|
rex, err := regexp.Compile("[" + regexp.QuoteMeta(g.config.ExcludeChars) + "]")
|
|
|
|
if err == nil {
|
|
|
|
newRange := rex.ReplaceAllLiteralString(charRange.String(), "")
|
|
|
|
charRange.Reset()
|
|
|
|
charRange.WriteString(newRange)
|
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "failed to exclude characters: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 17:07:55 +01:00
|
|
|
return charRange.String()
|
|
|
|
}
|
|
|
|
|
2023-08-06 18:55:47 +02:00
|
|
|
// GetPasswordLength returns the password length based on the given config
|
|
|
|
// parameters
|
|
|
|
func (g *Generator) GetPasswordLength() (int64, error) {
|
|
|
|
if g.config.FixedLength > 0 {
|
|
|
|
return g.config.FixedLength, nil
|
|
|
|
}
|
2024-03-07 22:50:47 +01:00
|
|
|
minLength := g.config.MinLength
|
|
|
|
maxLength := g.config.MaxLength
|
|
|
|
if minLength > maxLength {
|
|
|
|
maxLength = minLength
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
2024-03-07 22:50:47 +01:00
|
|
|
diff := maxLength - minLength + 1
|
|
|
|
randNum, err := g.RandNum(diff)
|
2023-08-05 18:10:11 +02:00
|
|
|
if err != nil {
|
2023-08-06 18:55:47 +02:00
|
|
|
return 0, err
|
2023-08-05 18:10:11 +02:00
|
|
|
}
|
2024-03-07 22:50:47 +01:00
|
|
|
length := minLength + randNum
|
|
|
|
if length <= 0 {
|
2023-08-06 18:55:47 +02:00
|
|
|
return 1, nil
|
|
|
|
}
|
2024-03-07 22:50:47 +01:00
|
|
|
return length, nil
|
2023-08-05 18:10:11 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 22:50:47 +01:00
|
|
|
// RandomBytes returns a byte slice of random bytes with given length that got generated by
|
2023-04-18 11:49:44 +02:00
|
|
|
// the crypto/rand generator
|
2024-03-07 22:50:47 +01:00
|
|
|
func (g *Generator) RandomBytes(length int64) ([]byte, error) {
|
|
|
|
if length < 1 {
|
2023-04-18 11:49:44 +02:00
|
|
|
return nil, ErrInvalidLength
|
|
|
|
}
|
2024-03-07 22:50:47 +01:00
|
|
|
bytes := make([]byte, length)
|
|
|
|
numBytes, err := rand.Read(bytes)
|
|
|
|
if int64(numBytes) != length {
|
2023-04-18 11:49:44 +02:00
|
|
|
return nil, ErrLengthMismatch
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-07 22:50:47 +01:00
|
|
|
return bytes, nil
|
2023-04-18 11:49:44 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 18:55:47 +02:00
|
|
|
// RandNum generates a random, non-negative number with given maximum value
|
2024-03-07 22:50:47 +01:00
|
|
|
func (g *Generator) RandNum(max int64) (int64, error) {
|
|
|
|
if max < 1 {
|
2023-08-06 18:55:47 +02:00
|
|
|
return 0, ErrInvalidLength
|
|
|
|
}
|
2024-03-07 22:50:47 +01:00
|
|
|
max64 := big.NewInt(max)
|
|
|
|
randNum, err := rand.Int(rand.Reader, max64)
|
2023-08-06 18:55:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("random number generation failed: %w", err)
|
|
|
|
}
|
2024-03-07 22:50:47 +01:00
|
|
|
return randNum.Int64(), nil
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
|
|
|
|
2023-08-04 16:02:58 +02:00
|
|
|
// RandomStringFromCharRange returns a random string of length l based of the range of characters given.
|
2023-04-18 11:49:44 +02:00
|
|
|
// The method makes use of the crypto/random package and therfore is
|
|
|
|
// cryptographically secure
|
2024-03-07 22:50:47 +01:00
|
|
|
func (g *Generator) RandomStringFromCharRange(length int64, charRange string) (string, error) {
|
|
|
|
if length < 1 {
|
2023-04-18 11:49:44 +02:00
|
|
|
return "", ErrInvalidLength
|
|
|
|
}
|
2024-03-07 22:50:47 +01:00
|
|
|
if len(charRange) < 1 {
|
2023-04-18 11:49:44 +02:00
|
|
|
return "", ErrInvalidCharRange
|
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
randString := strings.Builder{}
|
2023-08-06 18:55:47 +02:00
|
|
|
|
|
|
|
// As long as the length is smaller than the max. int32 value let's grow
|
|
|
|
// the string builder to the actual size, so we need less allocations
|
2024-03-07 22:50:47 +01:00
|
|
|
if length <= maxInt32 {
|
2024-03-07 23:22:28 +01:00
|
|
|
randString.Grow(int(length))
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 22:50:47 +01:00
|
|
|
charRangeLength := len(charRange)
|
2023-04-18 11:49:44 +02:00
|
|
|
|
2024-03-07 23:22:28 +01:00
|
|
|
randPool := make([]byte, 8)
|
|
|
|
_, err := rand.Read(randPool)
|
2023-04-18 11:49:44 +02:00
|
|
|
if err != nil {
|
2024-03-07 23:22:28 +01:00
|
|
|
return randString.String(), err
|
2023-04-18 11:49:44 +02:00
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
for idx, char, rest := length-1, binary.BigEndian.Uint64(randPool), letterIdxMax; idx >= 0; {
|
|
|
|
if rest == 0 {
|
|
|
|
_, err = rand.Read(randPool)
|
2023-04-18 11:49:44 +02:00
|
|
|
if err != nil {
|
2024-03-07 23:22:28 +01:00
|
|
|
return randString.String(), err
|
2023-04-18 11:49:44 +02:00
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
char, rest = binary.BigEndian.Uint64(randPool), letterIdxMax
|
2023-04-18 11:49:44 +02:00
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
if i := int(char & letterIdxMask); i < charRangeLength {
|
|
|
|
randString.WriteByte(charRange[i])
|
|
|
|
idx--
|
2023-04-18 11:49:44 +02:00
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
char >>= letterIdxBits
|
|
|
|
rest--
|
2023-04-18 11:49:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 23:22:28 +01:00
|
|
|
return randString.String(), nil
|
2023-04-18 11:49:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:07:55 +01:00
|
|
|
// checkMinimumRequirements checks if a password meets the minimum requirements specified in the
|
|
|
|
// generator's configuration. It returns true if the password meets the requirements, otherwise it
|
|
|
|
// returns false.
|
|
|
|
//
|
|
|
|
// The minimum requirements for each character type (lowercase, numeric, special, uppercase) are
|
|
|
|
// checked independently. For each character type, the corresponding character range is determined
|
|
|
|
// based on the generator's configuration. The password is then checked for the presence of each
|
|
|
|
// character in the character range, and a count is maintained.
|
2024-03-07 23:22:28 +01:00
|
|
|
func (g *Generator) checkMinimumRequirements(password string) bool {
|
2023-08-06 18:55:47 +02:00
|
|
|
ok := true
|
|
|
|
if g.config.MinLowerCase > 0 {
|
2024-03-07 23:22:28 +01:00
|
|
|
var charRange string
|
2023-08-06 18:55:47 +02:00
|
|
|
switch MaskHasMode(g.config.Mode, ModeHumanReadable) {
|
|
|
|
case true:
|
2024-03-07 23:22:28 +01:00
|
|
|
charRange = CharRangeAlphaLowerHuman
|
2023-08-06 18:55:47 +02:00
|
|
|
default:
|
2024-03-07 23:22:28 +01:00
|
|
|
charRange = CharRangeAlphaLower
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
2023-04-18 11:49:44 +02:00
|
|
|
|
2024-03-07 23:22:28 +01:00
|
|
|
count := 0
|
|
|
|
for _, char := range charRange {
|
|
|
|
count += strings.Count(password, string(char))
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
if int64(count) < g.config.MinLowerCase {
|
2023-08-06 18:55:47 +02:00
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if g.config.MinNumeric > 0 {
|
2024-03-07 23:22:28 +01:00
|
|
|
var charRange string
|
2023-08-06 18:55:47 +02:00
|
|
|
switch MaskHasMode(g.config.Mode, ModeHumanReadable) {
|
|
|
|
case true:
|
2024-03-07 23:22:28 +01:00
|
|
|
charRange = CharRangeNumericHuman
|
2023-08-06 18:55:47 +02:00
|
|
|
default:
|
2024-03-07 23:22:28 +01:00
|
|
|
charRange = CharRangeNumeric
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
2023-08-05 15:04:34 +02:00
|
|
|
|
2024-03-07 23:22:28 +01:00
|
|
|
count := 0
|
|
|
|
for _, char := range charRange {
|
|
|
|
count += strings.Count(password, string(char))
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
if int64(count) < g.config.MinNumeric {
|
2023-08-06 18:55:47 +02:00
|
|
|
ok = false
|
|
|
|
}
|
2023-08-05 15:04:34 +02:00
|
|
|
}
|
2023-08-06 18:55:47 +02:00
|
|
|
if g.config.MinSpecial > 0 {
|
2024-03-07 23:22:28 +01:00
|
|
|
var charRange string
|
2023-08-06 18:55:47 +02:00
|
|
|
switch MaskHasMode(g.config.Mode, ModeHumanReadable) {
|
|
|
|
case true:
|
2024-03-07 23:22:28 +01:00
|
|
|
charRange = CharRangeSpecialHuman
|
2023-08-06 18:55:47 +02:00
|
|
|
default:
|
2024-03-07 23:22:28 +01:00
|
|
|
charRange = CharRangeSpecial
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 23:22:28 +01:00
|
|
|
count := 0
|
|
|
|
for _, char := range charRange {
|
|
|
|
count += strings.Count(password, string(char))
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
if int64(count) < g.config.MinSpecial {
|
2023-08-06 18:55:47 +02:00
|
|
|
ok = false
|
|
|
|
}
|
2023-08-05 15:04:34 +02:00
|
|
|
}
|
2023-08-06 18:55:47 +02:00
|
|
|
if g.config.MinUpperCase > 0 {
|
2024-03-07 23:22:28 +01:00
|
|
|
var charRange string
|
2023-08-06 18:55:47 +02:00
|
|
|
switch MaskHasMode(g.config.Mode, ModeHumanReadable) {
|
|
|
|
case true:
|
2024-03-07 23:22:28 +01:00
|
|
|
charRange = CharRangeAlphaUpperHuman
|
2023-08-06 18:55:47 +02:00
|
|
|
default:
|
2024-03-07 23:22:28 +01:00
|
|
|
charRange = CharRangeAlphaUpper
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 23:22:28 +01:00
|
|
|
count := 0
|
|
|
|
for _, char := range charRange {
|
|
|
|
count += strings.Count(password, string(char))
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
if int64(count) < g.config.MinUpperCase {
|
2023-08-06 18:55:47 +02:00
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateCoinFlip is executed when Generate() is called with Algorithm set
|
|
|
|
// to AlgoCoinFlip
|
|
|
|
func (g *Generator) generateCoinFlip() (string, error) {
|
2024-03-07 23:22:28 +01:00
|
|
|
if g.CoinFlipBool() {
|
2023-08-06 18:55:47 +02:00
|
|
|
return "Heads", nil
|
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
return "Tails", nil
|
2023-08-06 18:55:47 +02:00
|
|
|
}
|
|
|
|
|
2024-03-12 18:28:01 +01:00
|
|
|
// generatePronounceable is executed when Generate() is called with Algorithm set
|
|
|
|
// to AlgoPronounceable
|
|
|
|
func (g *Generator) generatePronounceable() (string, error) {
|
2024-03-08 17:07:55 +01:00
|
|
|
var password string
|
2024-03-12 18:28:01 +01:00
|
|
|
g.syllables = make([]string, 0)
|
2024-03-08 17:07:55 +01:00
|
|
|
|
|
|
|
length, err := g.GetPasswordLength()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to calculate password length: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-03-12 18:28:01 +01:00
|
|
|
characterSet := KoremutakeSyllables
|
|
|
|
characterSet = append(characterSet, strings.Split(CharRangeNumericHuman, "")...)
|
2024-03-08 17:07:55 +01:00
|
|
|
characterSet = append(characterSet, strings.Split(CharRangeSpecialHuman, "")...)
|
|
|
|
characterSetLength := len(characterSet)
|
|
|
|
for int64(len(password)) < length {
|
|
|
|
randNum, err := g.RandNum(int64(characterSetLength))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to generate a random number for Koremutake syllable generation: %s",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
nextSyllable := characterSet[randNum]
|
|
|
|
if g.CoinFlipBool() {
|
|
|
|
syllableLength := len(nextSyllable)
|
|
|
|
characterPosition, err := g.RandNum(int64(syllableLength))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to generate a random number for Koremutake syllable generation: %s",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
randomChar := string(nextSyllable[characterPosition])
|
|
|
|
nextSyllable = strings.ReplaceAll(nextSyllable, randomChar, strings.ToUpper(randomChar))
|
|
|
|
}
|
|
|
|
password += nextSyllable
|
2024-03-12 18:28:01 +01:00
|
|
|
g.syllables = append(g.syllables, nextSyllable)
|
2024-03-08 17:07:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return password, nil
|
|
|
|
}
|
|
|
|
|
2023-08-06 18:55:47 +02:00
|
|
|
// generateRandom is executed when Generate() is called with Algorithm set
|
|
|
|
// to AlgoRandmom
|
|
|
|
func (g *Generator) generateRandom() (string, error) {
|
2024-03-07 23:22:28 +01:00
|
|
|
length, err := g.GetPasswordLength()
|
2023-08-05 15:04:34 +02:00
|
|
|
if err != nil {
|
2023-08-06 18:55:47 +02:00
|
|
|
return "", fmt.Errorf("failed to calculate password length: %w", err)
|
2023-08-05 15:04:34 +02:00
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
charRange := g.GetCharRangeFromConfig()
|
|
|
|
var password string
|
2023-08-06 18:55:47 +02:00
|
|
|
var ok bool
|
|
|
|
for !ok {
|
2024-03-07 23:22:28 +01:00
|
|
|
password, err = g.RandomStringFromCharRange(length, charRange)
|
2023-08-06 18:55:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-03-07 23:22:28 +01:00
|
|
|
ok = g.checkMinimumRequirements(password)
|
2023-08-05 15:04:34 +02:00
|
|
|
}
|
2023-08-06 18:55:47 +02:00
|
|
|
|
2024-03-07 23:22:28 +01:00
|
|
|
return password, nil
|
2023-08-05 15:04:34 +02:00
|
|
|
}
|