Migrat pkg/password to using pkglib package
This commit is contained in:
parent
010fd536ea
commit
aa3580c248
11 changed files with 56 additions and 158 deletions
|
|
@ -1,96 +0,0 @@
|
|||
package password
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CommonPasswordError
|
||||
type CommonPasswordError struct {
|
||||
CommonPassword
|
||||
}
|
||||
|
||||
func (cpe CommonPasswordError) Error() string {
|
||||
return fmt.Sprintf("%q from %q", cpe.Password, cpe.Source)
|
||||
}
|
||||
|
||||
type CommonPassword struct {
|
||||
Password string
|
||||
Source string
|
||||
}
|
||||
|
||||
//go:embed common
|
||||
var commonEmbed embed.FS
|
||||
|
||||
// CommonPasswords returns a channel that contains all passwords.
|
||||
// The caller must drain the channel.
|
||||
func CommonPasswords() <-chan CommonPassword {
|
||||
pChan := make(chan CommonPassword, 10)
|
||||
go func() {
|
||||
defer close(pChan)
|
||||
|
||||
fs.WalkDir(commonEmbed, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// get the full path
|
||||
if d.IsDir() || !strings.HasSuffix(path, ".txt") {
|
||||
return nil
|
||||
}
|
||||
|
||||
// open it
|
||||
file, err := commonEmbed.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// scan it line by line
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "//") {
|
||||
continue
|
||||
}
|
||||
pChan <- CommonPassword{
|
||||
Password: line,
|
||||
Source: path,
|
||||
}
|
||||
}
|
||||
|
||||
return scanner.Err()
|
||||
})
|
||||
}()
|
||||
return pChan
|
||||
}
|
||||
|
||||
// CheckCommonPassword checks if a password is a common password.
|
||||
//
|
||||
// check is called with each candidate password to perform the check.
|
||||
// check should return a boolean indicating if the password in question corresponds to the candidate.
|
||||
//
|
||||
// CheckCommonPassword returns one of three error values.
|
||||
//
|
||||
// - a CommonPasswordError (when a password matches a common password)
|
||||
// - an error returned by check (assuming some check went wrong)
|
||||
// - or nil (when a password is not a common password
|
||||
func CheckCommonPassword(check func(candidate string) (bool, error)) error {
|
||||
for commmon := range CommonPasswords() {
|
||||
ok, err := check(commmon.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// password validation passed
|
||||
if ok {
|
||||
return CommonPasswordError{
|
||||
CommonPassword: commmon,
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,2 +0,0 @@
|
|||
// This file contains a list of common WissKI Passwords
|
||||
W1ssk1.
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
// Package password allows generating random passwords
|
||||
package password
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
|
||||
"github.com/tkw1536/pkglib/pools"
|
||||
)
|
||||
|
||||
// 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!
|
||||
password := pools.GetBuilder()
|
||||
defer pools.ReleaseBuilder(password)
|
||||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue