Skip to content

Commit

Permalink
Added fixed-length and ignoredChars flags
Browse files Browse the repository at this point in the history
Also, now the program checks the parameters before it generates the password
  • Loading branch information
Bollos00 committed Dec 6, 2021
1 parent 2443bb0 commit 2465242
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 23 deletions.
Empty file added README.md
Empty file.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Bollos00/passwgen-go
module github.com/Bollos00/passwgen-go

go 1.17

Expand Down
42 changes: 20 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
package main

import (
"Bollos00/passwgen-go/passwgen"
"fmt"

"github.com/Bollos00/passwgen-go/src/passwgen"
flag "github.com/ogier/pflag"
)

func Unused(vals ...interface{}) {
for _, val := range vals {
_ = val
}
}

func FindItemInSlice(slice []byte, val byte) bool {
for _, v := range slice {
if v == val {
return true
}
}
return false
}

func initFlags() passwgen.PasswGen {

// Create the flags of the program
var characters *string = flag.StringP("characters", "c", "1aA%", "Type of characters on the password")
var characters *string = flag.StringP("characters", "c", "1aA%",
"Allowed characters type on the password")
var min *int = flag.IntP("min", "m", 8, "Minimum size of the password")
var max *int = flag.IntP("max", "M", 16, "Maximum size of the password")
var length *int = flag.IntP("length", "l", -1,
"Fixed length of the password (overwrites 'min' and 'max' flags)")

var ignoredChars *string = flag.StringP("ignored", "i", "",
"Characters ignored in the creation of the password")

flag.Parse()

pssgenType := passwgen.PasswGenCharType(0)
pssgenType := passwgen.PasswGenCharType(passwgen.None)

// Detect the allowed characters type of the password
for _, v := range []byte(*characters) {

if FindItemInSlice(passwgen.NumeralChars(), v) {
if passwgen.FindItemInSlice(passwgen.NumeralChars(), v) != -1 {
pssgenType = pssgenType | passwgen.Numeral
} else if FindItemInSlice(passwgen.LowerLetterChars(), v) {
} else if passwgen.FindItemInSlice(passwgen.LowerLetterChars(), v) != -1 {
pssgenType = pssgenType | passwgen.LowerLetter
} else if FindItemInSlice(passwgen.UpperLetterChars(), v) {
} else if passwgen.FindItemInSlice(passwgen.UpperLetterChars(), v) != -1 {
pssgenType = pssgenType | passwgen.UpperLetter
} else if FindItemInSlice(passwgen.SpecialChars(), v) {
} else if passwgen.FindItemInSlice(passwgen.SpecialChars(), v) != -1 {
pssgenType = pssgenType | passwgen.Special
}
}
Expand All @@ -50,10 +42,16 @@ func initFlags() passwgen.PasswGen {
*max = *min
}

if *length >= 0 {
*max = *length
*min = *length
}

passgen_instance := passwgen.PasswGen{
CharactersType: pssgenType,
MinSize: *min,
MaxSize: *max,
IgnoredChars: []byte(*ignoredChars),
}

return passgen_instance
Expand Down
46 changes: 46 additions & 0 deletions passwgen/passwgen.go → src/passwgen/passwgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,55 @@ func SpecialChars() []byte {
'}', '~', ' '}
}

func RemoveInSlice(slice []byte, index int) []byte {
slice[index] = slice[len(slice)-1]
return slice[:len(slice)-1]
}

func Unused(vals ...interface{}) {
for _, val := range vals {
_ = val
}
}

func FindItemInSlice(slice []byte, val byte) int {
for i, v := range slice {
if v == val {
return i
}
}

return -1
}

type PasswGen struct {
CharactersType PasswGenCharType
MinSize int
MaxSize int
IgnoredChars []byte
}

func (psg PasswGen) Validate() PasswGen {
if psg.MinSize <= 0 || psg.MaxSize <= 0 {
fmt.Printf("The size of the password should be a positive integrer.\n")
fmt.Printf("Changing the password length to a random number between 8 and 16.\n\n")
psg.MinSize = 8
psg.MaxSize = 16
}
if psg.CharactersType == None {
fmt.Printf("No valid characters to the password found. Changing to default.\n\n")
psg.CharactersType = Numeral | UpperLetter | LowerLetter | Special
}

return psg
}

func (psg PasswGen) Generate() string {

availableChars := []byte{}

psg = psg.Validate()

if psg.CharactersType&Numeral != 0 {
availableChars = append(availableChars, NumeralChars()...)
}
Expand All @@ -77,6 +116,13 @@ func (psg PasswGen) Generate() string {
availableChars = append(availableChars, SpecialChars()...)
}

for _, v := range psg.IgnoredChars {
index := FindItemInSlice(availableChars, v)
if index != -1 {
availableChars = RemoveInSlice(availableChars, index)
}
}

fmt.Printf("Available characters: %c\n\n", availableChars)

rand.Seed(time.Now().UnixNano())
Expand Down

0 comments on commit 2465242

Please sign in to comment.