2022-08-02 23:07:28 +00:00
|
|
|
// GoPass
|
|
|
|
// Author: Maximilian Patterson
|
|
|
|
package main
|
2022-08-02 22:37:06 +00:00
|
|
|
|
|
|
|
import (
|
2023-02-07 14:06:22 +00:00
|
|
|
"crypto/rand"
|
2022-08-02 22:37:06 +00:00
|
|
|
"fmt"
|
2023-01-30 05:30:21 +00:00
|
|
|
"math/big"
|
2022-08-02 22:37:06 +00:00
|
|
|
"os"
|
2023-02-14 22:12:02 +00:00
|
|
|
"runtime"
|
2022-08-02 22:37:06 +00:00
|
|
|
"strconv"
|
2023-02-07 14:06:22 +00:00
|
|
|
"strings"
|
2022-08-02 22:37:06 +00:00
|
|
|
)
|
|
|
|
|
2022-12-19 23:41:54 +00:00
|
|
|
var allowedCharacters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+[]\\{}|;':,./<>?")
|
2022-08-02 22:37:06 +00:00
|
|
|
|
2023-02-07 14:06:22 +00:00
|
|
|
const (
|
2024-11-01 19:39:24 +00:00
|
|
|
Version = "1.3.3"
|
2023-02-14 22:12:02 +00:00
|
|
|
symbols = "`~!@#$%^&*()_+[]\\{}|;':,./<>?"
|
|
|
|
chunkSize = 16 // The size of each chunk of the password to be generated by the worker goroutines
|
2023-02-07 14:06:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func matchArguments(args []string) string {
|
|
|
|
// If there are no arguments
|
|
|
|
if len(args) == 0 {
|
|
|
|
return "No password length specified! (ex: gopass 16)"
|
2022-12-19 16:21:06 +00:00
|
|
|
}
|
2022-08-02 22:37:06 +00:00
|
|
|
|
2023-02-07 14:06:22 +00:00
|
|
|
// First argument is special, must be an integer, -v, or -h
|
|
|
|
var size = 0 // Password length
|
2023-02-14 22:12:02 +00:00
|
|
|
switch args[0] {
|
|
|
|
case "-v":
|
2023-02-07 14:06:22 +00:00
|
|
|
return "GoPass version " + Version
|
2023-02-14 22:12:02 +00:00
|
|
|
case "-h":
|
2023-02-07 14:06:22 +00:00
|
|
|
return "GoPass - A simple password generator written in Go\n" +
|
|
|
|
"Usage: gopass [length] [disallowed characters] [optional remove symbols -s]\n" +
|
|
|
|
" Example: gopass 16\n" +
|
|
|
|
" Example: gopass 16 -r=abc123!@#\n" +
|
|
|
|
" Example: gopass 16 -s\n" +
|
|
|
|
"\nFor help (this output): gopass -h\n" +
|
|
|
|
"For version: gopass -v\n"
|
2023-02-14 22:12:02 +00:00
|
|
|
default:
|
|
|
|
err := error(nil)
|
|
|
|
size, err = strconv.Atoi(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return "Invalid first argument (\"" + args[0] + "\") supplied! (Type gopass -h for help)"
|
|
|
|
}
|
2022-12-19 23:41:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:06:22 +00:00
|
|
|
for i := 1; i < len(args); i++ {
|
|
|
|
v := args[i]
|
2023-02-14 22:12:02 +00:00
|
|
|
switch {
|
|
|
|
case v == "-s":
|
2023-02-07 14:06:22 +00:00
|
|
|
removeDisallowed([]rune(symbols))
|
2023-02-14 22:12:02 +00:00
|
|
|
case strings.HasPrefix(v, "-r="):
|
2023-02-07 14:06:22 +00:00
|
|
|
// Remove all characters after the = until next whitespace
|
|
|
|
removeDisallowed([]rune(v[2:]))
|
2023-02-14 22:12:02 +00:00
|
|
|
default:
|
2023-02-07 14:06:22 +00:00
|
|
|
return "Invalid argument (\"" + v + "\") supplied! (Type gopass -h for help)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if size <= 0 {
|
|
|
|
return "No/invalid password length specified! (ex: gopass 16)"
|
|
|
|
} else {
|
|
|
|
return generatePassword(size)
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 23:41:54 +00:00
|
|
|
|
2023-02-07 14:06:22 +00:00
|
|
|
// Remove all disallowed characters from the allowedCharacters slice
|
|
|
|
func removeDisallowed(disallowed []rune) {
|
2023-02-14 22:12:02 +00:00
|
|
|
disallowedMap := make(map[rune]bool, len(disallowed))
|
2023-02-07 14:06:22 +00:00
|
|
|
for _, r := range disallowed {
|
2023-02-14 22:12:02 +00:00
|
|
|
disallowedMap[r] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, v := range allowedCharacters {
|
|
|
|
if !disallowedMap[v] {
|
|
|
|
allowedCharacters[i] = v
|
|
|
|
i++
|
2022-12-19 23:41:54 +00:00
|
|
|
}
|
2022-08-02 22:37:06 +00:00
|
|
|
}
|
2023-02-14 22:12:02 +00:00
|
|
|
allowedCharacters = allowedCharacters[:i]
|
2023-02-07 14:06:22 +00:00
|
|
|
}
|
2022-08-02 22:37:06 +00:00
|
|
|
|
2023-02-07 14:06:22 +00:00
|
|
|
func generatePassword(size int) string {
|
2022-08-02 22:37:06 +00:00
|
|
|
// Make empty array of runes with size of size
|
|
|
|
pass := make([]rune, size)
|
|
|
|
|
2023-02-14 22:12:02 +00:00
|
|
|
// Create a channel to receive chunks of the password
|
|
|
|
passChan := make(chan []rune)
|
|
|
|
|
|
|
|
// Determine the number of worker goroutines to use
|
|
|
|
numWorkers := runtime.NumCPU()
|
|
|
|
|
|
|
|
// Launch the worker goroutines
|
|
|
|
for i := 0; i < numWorkers; i++ {
|
|
|
|
go func() {
|
|
|
|
allowedLen := len(allowedCharacters)
|
|
|
|
for {
|
|
|
|
// Generate a chunk of the password
|
|
|
|
chunk := make([]rune, chunkSize)
|
|
|
|
for i := range chunk {
|
|
|
|
index, err := rand.Int(rand.Reader, big.NewInt(int64(allowedLen)))
|
|
|
|
if err != nil {
|
|
|
|
println("Error securely generating random character chunk!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
chunk[i] = allowedCharacters[index.Int64()]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the chunk of the password to the main goroutine
|
|
|
|
passChan <- chunk
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect the chunks of the password from the channel
|
|
|
|
for i := 0; i < size; i += chunkSize {
|
|
|
|
chunk := <-passChan
|
|
|
|
copy(pass[i:], chunk)
|
2022-08-02 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:06:22 +00:00
|
|
|
return string(pass)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Process arguments
|
|
|
|
fmt.Println(matchArguments(os.Args[1:]))
|
2022-08-02 22:37:06 +00:00
|
|
|
}
|