add --input-type flag

This commit is contained in:
akulij 2025-03-02 23:23:07 +00:00
parent b6be7799b9
commit 181ac703ae

View File

@ -33,6 +33,7 @@ Options:
-o, --output OUTPUT Write the result to the file at path OUTPUT. -o, --output OUTPUT Write the result to the file at path OUTPUT.
--raw-output Print stripped keys (without additional text or comments) --raw-output Print stripped keys (without additional text or comments)
--entropy-level VALUE Manages required strenght of password (more info down below) --entropy-level VALUE Manages required strenght of password (more info down below)
--input-type TYPE Type of input from stdin. Can be 'password' (default), 'hash', 'raw'
Mostly similar to age-keygen Mostly similar to age-keygen
Required password strenght can be changes via --entropy-level flag. Possible values Required password strenght can be changes via --entropy-level flag. Possible values
@ -46,10 +47,19 @@ Each word or number is mapped following this list:
- stupid - no limit - stupid - no limit
` `
type InputType int
const (
InputPassword InputType = iota
InputHash
InputRaw
)
type Flags struct { type Flags struct {
RawOutput bool RawOutput bool
OutputFile string OutputFile string
EntropyLevel int EntropyLevel int
InputType InputType
} }
func main() { func main() {
@ -123,12 +133,14 @@ func parseFlags() (*Flags, error) {
rawOutput bool rawOutput bool
outputFile string outputFile string
entropyLevel string entropyLevel string
inputType string
) )
flag.BoolVar(&rawOutput, "raw-output", false, "Print stripped keys (without additional text or comments)") flag.BoolVar(&rawOutput, "raw-output", false, "Print stripped keys (without additional text or comments)")
flag.StringVar(&outputFile, "o", "", "Write the result to the file at path OUTPUT") flag.StringVar(&outputFile, "o", "", "Write the result to the file at path OUTPUT")
flag.StringVar(&outputFile, "output", "", "Write the result to the file at path OUTPUT") flag.StringVar(&outputFile, "output", "", "Write the result to the file at path OUTPUT")
flag.StringVar(&entropyLevel, "entropy-level", "medium", "Manages required strenght of password. Read more in --help") flag.StringVar(&entropyLevel, "entropy-level", "medium", "Manages required strenght of password. Read more in --help")
flag.StringVar(&inputType, "input-type", "password", "Type of input from stdin. Can be 'password' (default), 'hash', 'raw'")
flag.Parse() flag.Parse()
eLevel, err := parseEntropyLevel(entropyLevel) eLevel, err := parseEntropyLevel(entropyLevel)
@ -136,13 +148,34 @@ func parseFlags() (*Flags, error) {
return nil, err return nil, err
} }
iType, err := parseInputType(inputType)
if err != nil {
return nil, err
}
return &Flags{ return &Flags{
RawOutput: rawOutput, RawOutput: rawOutput,
OutputFile: outputFile, OutputFile: outputFile,
EntropyLevel: eLevel, EntropyLevel: eLevel,
InputType: iType,
}, nil }, nil
} }
func parseInputType(inputType string) (InputType, error) {
m := map[string]InputType{
"password": InputPassword,
"hash": InputHash,
"raw": InputRaw,
}
iType, ok := m[inputType]
if !ok {
return InputPassword, errors.New("wrong input type")
}
return iType, nil
}
func parseEntropyLevel(entropyLevel string) (int, error) { func parseEntropyLevel(entropyLevel string) (int, error) {
if i, err := strconv.Atoi(entropyLevel); err == nil { if i, err := strconv.Atoi(entropyLevel); err == nil {
if i == 0 { if i == 0 {