Skip to content

Commit

Permalink
Add Alpha and Symbol generation as 2 separate utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kishaningithub committed Dec 18, 2018
1 parent b78db41 commit ec77ff7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func main() {
// The following functions will generate 20 chars long passwords out of
fmt.Print(pwgen.New(20, "ab")) // a's and b's
fmt.Print(pwgen.Num(20)) // numbers
fmt.Print(pwgen.Alpha(20)) // alphabets
fmt.Print(pwgen.Symbols(20)) // symbols
fmt.Print(pwgen.AlphaNum(20)) // alphanumeric characters
fmt.Print(pwgen.AlphaNumSymbols(20)) // alphanumeric characters and symbols
}
Expand Down
16 changes: 14 additions & 2 deletions pwgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

// Characters the password can contain
var num = "0123456789"
var alphaNum = num + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var alphaNumSymbols = alphaNum + "_-?!.,@#$%^&*()=[]{}<>"
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var symbols = "_-?!.,@#$%^&*()=[]{}<>"
var alphaNum = num + alpha
var alphaNumSymbols = alphaNum + symbols

// New generates a random string of the given length out of the characters in char
func New(length int, chars string) string {
Expand All @@ -26,6 +28,16 @@ func Num(length int) string {
return New(length, num)
}

// Alpha generates a random string of the given length out of alphabets
func Alpha(length int) string {
return New(length, alpha)
}

// Symbols generates a random string of the given length out of symbols
func Symbols(length int) string {
return New(length, symbols)
}

// AlphaNum generates a random string of the given length out of alphanumeric characters
func AlphaNum(length int) string {
return New(length, alphaNum)
Expand Down
20 changes: 20 additions & 0 deletions pwgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ func TestNum(t *testing.T) {
}
}

func TestAlpha(t *testing.T) {
var password string
for i := 1; i < 21; i++ {
password = Alpha(i)
if len(password) != i {
t.Errorf("Expected length %d, got %d\n", i, len(password))
}
}
}

func TestSymbols(t *testing.T) {
var password string
for i := 1; i < 21; i++ {
password = Symbols(i)
if len(password) != i {
t.Errorf("Expected length %d, got %d\n", i, len(password))
}
}
}

func TestAlphaNum(t *testing.T) {
var password string
for i := 1; i < 21; i++ {
Expand Down

0 comments on commit ec77ff7

Please sign in to comment.