Full refactor, remove dead code, remove math/rand, add version arg, remove symbol arg, help arg, and improve remove disallowed character argument
This commit is contained in:
parent
3978c9ad47
commit
83c7065d2a
86
main.go
86
main.go
@ -1,42 +1,66 @@
|
|||||||
// GoPass
|
// GoPass
|
||||||
// Author: Maximilian Patterson
|
// Author: Maximilian Patterson
|
||||||
// Version: 1.2.2
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
cryptorand "crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
mathrand "math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var allowedCharacters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+[]\\{}|;':,./<>?")
|
var allowedCharacters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+[]\\{}|;':,./<>?")
|
||||||
|
|
||||||
func main() {
|
const (
|
||||||
// Take in all OS arg
|
Version = "1.3.0"
|
||||||
args := os.Args[1:]
|
symbols = "`~!@#$%^&*()_+[]\\{}|;':,./<>?"
|
||||||
if len(args) < 1 {
|
)
|
||||||
println("No password length specified! (ex: ./gopass 16)")
|
|
||||||
return
|
func matchArguments(args []string) string {
|
||||||
|
// If there are no arguments
|
||||||
|
if len(args) == 0 {
|
||||||
|
return "No password length specified! (ex: gopass 16)"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert String arg to int
|
// First argument is special, must be an integer, -v, or -h
|
||||||
size, err := strconv.Atoi(args[0])
|
var size = 0 // Password length
|
||||||
if err != nil {
|
err := error(nil)
|
||||||
println("First argument supplied must be an integer! (ex: 16)")
|
if size, err = strconv.Atoi(args[0]); err == nil { // If first argument is an integer
|
||||||
return
|
} else if args[0] == "-v" {
|
||||||
|
return "GoPass version " + Version
|
||||||
|
} else if args[0] == "-h" {
|
||||||
|
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"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab second argument (if it exists) and use it as a disallowed character(s)
|
for i := 1; i < len(args); i++ {
|
||||||
var disallowed []rune
|
v := args[i]
|
||||||
if len(args) == 2 {
|
if v == "-s" {
|
||||||
// Break apart the string into a slice of runes
|
removeDisallowed([]rune(symbols))
|
||||||
disallowed = []rune(args[1])
|
} else if strings.HasPrefix(v, "-r=") { // If argument starts with -r=
|
||||||
|
// Remove all characters after the = until next whitespace
|
||||||
|
removeDisallowed([]rune(v[2:]))
|
||||||
|
} else {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Remove all disallowed characters from the allowedCharacters slice
|
// Remove all disallowed characters from the allowedCharacters slice
|
||||||
|
func removeDisallowed(disallowed []rune) {
|
||||||
for _, r := range disallowed {
|
for _, r := range disallowed {
|
||||||
for i, v := range allowedCharacters {
|
for i, v := range allowedCharacters {
|
||||||
if v == r {
|
if v == r {
|
||||||
@ -46,29 +70,25 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generatePassword(size int) string {
|
||||||
// Make empty array of runes with size of size
|
// Make empty array of runes with size of size
|
||||||
pass := make([]rune, size)
|
pass := make([]rune, size)
|
||||||
|
|
||||||
// Seed rand with time
|
|
||||||
var b [8]byte
|
|
||||||
_, err = cryptorand.Read(b[:])
|
|
||||||
if err != nil {
|
|
||||||
println("Error securely seeding crypto/rand!")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mathrand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
|
|
||||||
|
|
||||||
// Assign every slot of pass to a random allowedCharacter
|
// Assign every slot of pass to a random allowedCharacter
|
||||||
for i := range pass {
|
for i := range pass {
|
||||||
// Generate a random int greater than 0 and not to exceed the length of allowedCharacters
|
// Generate a random int greater than 0 and not to exceed the length of allowedCharacters
|
||||||
index, err := cryptorand.Int(cryptorand.Reader, big.NewInt(int64(len(allowedCharacters))))
|
index, err := rand.Int(rand.Reader, big.NewInt(int64(len(allowedCharacters))))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error securely generating random character!")
|
println("Error securely generating random character!")
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
pass[i] = allowedCharacters[index.Int64()]
|
pass[i] = allowedCharacters[index.Int64()]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the password
|
return string(pass)
|
||||||
fmt.Println(string(pass))
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Process arguments
|
||||||
|
fmt.Println(matchArguments(os.Args[1:]))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user