From 86571c7758d48d92f8d1422eca5e2a2e80350587 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 10 May 2015 19:05:34 +0200 Subject: [PATCH] Fix: Blacklisted characters were not excluded from passwords --- lib/generator.go | 7 ++++++- lib/generator_test.go | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/generator.go b/lib/generator.go index d32228a..ffb96ee 100644 --- a/lib/generator.go +++ b/lib/generator.go @@ -69,9 +69,14 @@ func (s *SecurePassword) GeneratePassword(length int, special bool) (string, err password := "" rand.Seed(time.Now().UnixNano()) for { + char := string(characterTable[rand.Intn(len(characterTable))]) + if strings.Contains(strings.Join(s.badCharacters, ""), char) { + continue + } + password = fmt.Sprintf("%s%s", password, - string(characterTable[rand.Intn(len(characterTable))]), + char, ) if len(password) == length { if s.CheckPasswordSecurity(password, special) { diff --git a/lib/generator_test.go b/lib/generator_test.go index ad731b2..dd72c8c 100644 --- a/lib/generator_test.go +++ b/lib/generator_test.go @@ -1,6 +1,9 @@ package securepassword -import "testing" +import ( + "strings" + "testing" +) func TestInsecurePasswords(t *testing.T) { passwords := map[string]string{ @@ -119,6 +122,23 @@ func TestImpossiblePasswords(t *testing.T) { } } +func TestBadCharacters(t *testing.T) { + badCharacters := []string{"I", "l", "0", "O", "B", "8"} + + for i := 0; i < 500; i++ { + pwd, err := NewSecurePassword().GeneratePassword(20, false) + if err != nil { + t.Errorf("An error occured: %s", err) + } + for _, char := range badCharacters { + if strings.Contains(pwd, char) { + t.Errorf("Password '%s' contained blacklisted character: '%s'", pwd, char) + return + } + } + } +} + func BenchmarkGeneratePasswords8Char(b *testing.B) { pwd := NewSecurePassword() for i := 0; i < b.N; i++ {