diff --git a/.gitignore b/.gitignore index 8365624..ba39ef7 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ _testmain.go *.exe *.test + +.idea/ diff --git a/README.md b/README.md index 174cd04..713d1de 100644 --- a/README.md +++ b/README.md @@ -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 } diff --git a/pwgen.go b/pwgen.go index a5d1a7a..093c101 100644 --- a/pwgen.go +++ b/pwgen.go @@ -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 { @@ -26,6 +28,16 @@ func Num(length int) string { return New(length, num) } +// Alpha generates a random string of the given length out of alphabetic characters +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) diff --git a/pwgen_test.go b/pwgen_test.go index d14f40e..f9e3b4f 100644 --- a/pwgen_test.go +++ b/pwgen_test.go @@ -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++ {