1
0
Fork 0
mirror of https://github.com/Luzifer/password.git synced 2024-12-20 04:41:17 +00:00

[#6] Add optional separator to XKCD passwords

Squashed commit of the following:

commit ef74c84e5b4826c2c58012cd4b0ea975d1dd7260
Author: Knut Ahlers <knut@ahlers.me>
Date:   Sun Dec 1 13:09:53 2019 +0100

    Minor cleamup

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

commit 0b57aa8c8abb2fd118c71fdfd8d51cb707861821
Author: Knut Ahlers <knut@ahlers.me>
Date:   Sun Dec 1 13:09:17 2019 +0100

    [#6] Add optional separator to XKCD passwords

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-12-01 13:10:20 +01:00
parent 3d83799a91
commit aa39ad3413
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
4 changed files with 20 additions and 6 deletions

View file

@ -9,6 +9,5 @@ commands:
environment: environment:
CGO_ENABLED: 0 CGO_ENABLED: 0
GHUSER: Luzifer GHUSER: Luzifer
GO111MODULE: on
PACKAGES: github.com/Luzifer/password/cmd/password PACKAGES: github.com/Luzifer/password/cmd/password
REPO: password REPO: password

View file

@ -1,9 +1,8 @@
language: go language: go
go: go:
- 1.10.x
- 1.11.x
- 1.12.x - 1.12.x
- 1.13.x
- tip - tip
install: go get -v github.com/Luzifer/password/lib install: go get -v github.com/Luzifer/password/lib

View file

@ -9,7 +9,10 @@ import (
"github.com/Luzifer/go_helpers/v2/str" "github.com/Luzifer/go_helpers/v2/str"
) )
type XKCD struct{} type XKCD struct {
// Separator to be used between words
Separator string
}
var ( var (
// ErrTooFewWords represents an error thrown if the password will // ErrTooFewWords represents an error thrown if the password will
@ -48,9 +51,8 @@ func (x XKCD) GeneratePassword(length int, addDate bool) (string, error) {
continue continue
} }
password = password + word
usedWords = append(usedWords, word) usedWords = append(usedWords, word)
} }
return password, nil return password + strings.Join(usedWords, x.Separator), nil
} }

View file

@ -37,6 +37,20 @@ func TestXKCDDatePrepend(t *testing.T) {
} }
} }
func TestXKCDSeparator(t *testing.T) {
gen := NewXKCDGenerator()
gen.Separator = "-"
pwd, err := gen.GeneratePassword(4, false)
if err != nil {
t.Fatalf("Generated had an error: %s", err)
}
if !regexp.MustCompile(`^(?:[A-Z][a-z]+-){3}(?:[A-Z][a-z]+)$`).MatchString(pwd) {
t.Errorf("Password %q did not match expected RegEx", pwd)
}
}
func BenchmarkGeneratePasswords4Words(b *testing.B) { func BenchmarkGeneratePasswords4Words(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
DefaultXKCD.GeneratePassword(4, false) DefaultXKCD.GeneratePassword(4, false)