Refactor Package structure

This commit cleans up the package structure, to make two new top-level
packages `internal` (for internal-use packages) and `pkg` (for general
shared utility code).
This commit is contained in:
Tom Wiesing 2022-09-12 14:46:18 +02:00
parent 487ce09979
commit a360324f62
No known key found for this signature in database
124 changed files with 97 additions and 101 deletions

46
pkg/password/password.go Normal file
View file

@ -0,0 +1,46 @@
// Package password allows generating random passwords
package password
import (
"crypto/rand"
"math/big"
"strings"
)
// NOTE(twiesing): A bunch of scripts cannot properly handle the extra characters in the password.
// For now it is disabled, but it should be re-enabled later.
const PasswordCharSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // + "!@#$%&*"
var passwordCharCount = big.NewInt(int64(len(PasswordCharSet)))
// Password returns a randomly generated string with the provided length.
// It consists of alphanumeric characters only.
//
// When an error occurs, it is guaranteed to return "", err.
// [rand.Reader] is used as the source of randomness.
func Password(length int) (string, error) {
if length < 0 {
panic("length < 0")
}
// create a buffer to write the string to!
var password strings.Builder
password.Grow(length)
for i := 0; i < length; i++ {
// grab a random bIndex!
bIndex, err := rand.Int(rand.Reader, passwordCharCount)
if err != nil {
return "", err
}
// and use that index!
index := int(bIndex.Int64())
if err := password.WriteByte(PasswordCharSet[index]); err != nil {
return "", err
}
}
// return the password!
return password.String(), nil
}