Skip to content

Commit

Permalink
The passwords are generated with success now
Browse files Browse the repository at this point in the history
  • Loading branch information
Bollos00 committed Nov 19, 2021
1 parent 9a7b791 commit 2443bb0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
21 changes: 14 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ func FindItemInSlice(slice []byte, val byte) bool {
return false
}

func initFlags() {
func initFlags() passwgen.PasswGen {

var characters *string = flag.StringP("characters", "c", "aA1!", "Type of characters on the password")
// Create the flags of the program
var characters *string = flag.StringP("characters", "c", "1aA%", "Type of characters 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")

Expand All @@ -40,22 +41,28 @@ func initFlags() {
pssgenType = pssgenType | passwgen.LowerLetter
} else if FindItemInSlice(passwgen.UpperLetterChars(), v) {
pssgenType = pssgenType | passwgen.UpperLetter
} else if FindItemInSlice(passwgen.SpecialLetterChars(), v) {
} else if FindItemInSlice(passwgen.SpecialChars(), v) {
pssgenType = pssgenType | passwgen.Special
}
}

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

passgen_instance := passwgen.PasswGen{
CharactersType: pssgenType,
MinSize: *min,
MaxSize: *max,
}
fmt.Println(passgen_instance)

Unused(characters, min, max, passgen_instance, pssgenType)

return passgen_instance
}

func main() {

initFlags()
passgen_instace := initFlags()

password := passgen_instace.Generate()
fmt.Printf("Generated password:\n%v\n", password)
}
41 changes: 37 additions & 4 deletions passwgen/passwgen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package passwgen

import "fmt"
import (
"fmt"
"math/rand"
"time"
)

type PasswGenCharType int

Expand Down Expand Up @@ -39,7 +43,7 @@ func UpperLetterChars() []byte {
'Z'}
}

func SpecialLetterChars() []byte {
func SpecialChars() []byte {
return []byte{
'!', '"', '#', '$', '%',
'&', '\'', '(', ')', '*',
Expand All @@ -56,6 +60,35 @@ type PasswGen struct {
MaxSize int
}

func main() {
fmt.Printf("%v\n", SpecialLetterChars())
func (psg PasswGen) Generate() string {

availableChars := []byte{}

if psg.CharactersType&Numeral != 0 {
availableChars = append(availableChars, NumeralChars()...)
}
if psg.CharactersType&UpperLetter != 0 {
availableChars = append(availableChars, LowerLetterChars()...)
}
if psg.CharactersType&LowerLetter != 0 {
availableChars = append(availableChars, UpperLetterChars()...)
}
if psg.CharactersType&Special != 0 {
availableChars = append(availableChars, SpecialChars()...)
}

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

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

password_size := psg.MinSize + rand.Intn(psg.MaxSize-psg.MinSize+1)
fmt.Printf("Password size: %v\n\n", password_size)

password := make([]byte, password_size)

for i := range password {
password[i] = availableChars[rand.Intn(int(len(availableChars)))]
}

return string(password)
}

0 comments on commit 2443bb0

Please sign in to comment.