2022-08-02 23:07:28 +00:00
|
|
|
// GoPass
|
|
|
|
// Author: Maximilian Patterson
|
2023-01-30 05:37:46 +00:00
|
|
|
// Version: 1.2.2
|
2022-08-02 23:07:28 +00:00
|
|
|
package main
|
2022-08-02 22:37:06 +00:00
|
|
|
|
|
|
|
import (
|
2022-10-09 20:57:01 +00:00
|
|
|
cryptorand "crypto/rand"
|
|
|
|
"encoding/binary"
|
2022-08-02 22:37:06 +00:00
|
|
|
"fmt"
|
2023-01-30 05:30:21 +00:00
|
|
|
"math/big"
|
2022-10-09 20:57:01 +00:00
|
|
|
mathrand "math/rand"
|
2022-08-02 22:37:06 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2022-12-19 23:41:54 +00:00
|
|
|
var allowedCharacters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+[]\\{}|;':,./<>?")
|
2022-08-02 22:37:06 +00:00
|
|
|
|
|
|
|
func main() {
|
2022-12-19 23:41:54 +00:00
|
|
|
// Take in all OS arg
|
2022-12-19 16:21:06 +00:00
|
|
|
args := os.Args[1:]
|
2022-12-19 23:41:54 +00:00
|
|
|
if len(args) < 1 {
|
2023-01-29 22:16:44 +00:00
|
|
|
println("No password length specified! (ex: ./gopass 16)")
|
|
|
|
return
|
2022-12-19 16:21:06 +00:00
|
|
|
}
|
2022-08-02 22:37:06 +00:00
|
|
|
|
|
|
|
// Convert String arg to int
|
2022-12-19 16:21:06 +00:00
|
|
|
size, err := strconv.Atoi(args[0])
|
2022-08-02 22:37:06 +00:00
|
|
|
if err != nil {
|
2023-01-29 22:16:44 +00:00
|
|
|
println("First argument supplied must be an integer! (ex: 16)")
|
|
|
|
return
|
2022-12-19 23:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grab second argument (if it exists) and use it as a disallowed character(s)
|
|
|
|
var disallowed []rune
|
|
|
|
if len(args) == 2 {
|
|
|
|
// Break apart the string into a slice of runes
|
|
|
|
disallowed = []rune(args[1])
|
|
|
|
|
|
|
|
// Remove all disallowed characters from the allowedCharacters slice
|
|
|
|
for _, r := range disallowed {
|
|
|
|
for i, v := range allowedCharacters {
|
|
|
|
if v == r {
|
|
|
|
allowedCharacters = append(allowedCharacters[:i], allowedCharacters[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make empty array of runes with size of size
|
|
|
|
pass := make([]rune, size)
|
|
|
|
|
|
|
|
// Seed rand with time
|
2022-10-09 20:57:01 +00:00
|
|
|
var b [8]byte
|
|
|
|
_, err = cryptorand.Read(b[:])
|
|
|
|
if err != nil {
|
2023-01-29 22:16:44 +00:00
|
|
|
println("Error securely seeding crypto/rand!")
|
|
|
|
return
|
2022-10-09 20:57:01 +00:00
|
|
|
}
|
|
|
|
mathrand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
|
2022-08-02 22:37:06 +00:00
|
|
|
|
2022-12-19 23:41:54 +00:00
|
|
|
// Assign every slot of pass to a random allowedCharacter
|
2022-08-02 22:37:06 +00:00
|
|
|
for i := range pass {
|
2022-12-19 23:41:54 +00:00
|
|
|
// Generate a random int greater than 0 and not to exceed the length of allowedCharacters
|
2023-01-30 05:30:21 +00:00
|
|
|
index, err := cryptorand.Int(cryptorand.Reader, big.NewInt(int64(len(allowedCharacters))))
|
|
|
|
if err != nil {
|
|
|
|
println("Error securely generating random character!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pass[i] = allowedCharacters[index.Int64()]
|
2022-08-02 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 23:41:54 +00:00
|
|
|
// Print the password
|
2022-08-02 22:37:06 +00:00
|
|
|
fmt.Println(string(pass))
|
|
|
|
}
|